Source code for submission s720

grassshop.cpp

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. int pole[100][100];
  5. int posSx, posSy;
  6. int posFx, posFy;
  7. int sizex, sizey;
  8.  
  9. void check(int x, int y, int jump)
  10. {
  11. if(x > -1 && y > -1 && x < sizex && y < sizey)
  12. {
  13. if(pole[x][y]>jump)
  14. {
  15. pole[x][y] = jump;
  16. check(x+2, y+1,jump+1);
  17. check(x+2, y-1,jump+1);
  18. check(x-2, y+1,jump+1);
  19. check(x-2, y-1,jump+1);
  20. check(x+1, y+2,jump+1);
  21. check(x+1, y-2,jump+1);
  22. check(x-1, y+2,jump+1);
  23. check(x-1, y-2,jump+1);
  24. }
  25. }
  26.  
  27. }
  28.  
  29.  
  30. int main(void)
  31. {
  32.  
  33. while(scanf("%d %d %d %d %d %d", &sizex, &sizey, &posSx, &posSy, &posFx, &posFy) == 6)
  34. {
  35. for(int i = 0; i<sizex; i++)
  36. for(int j = 0; j<sizey; j++)
  37. pole[i][j] = 1000;
  38.  
  39. check(posSx-1, posSy-1, 0);
  40.  
  41.  
  42. if(pole[posFx-1][posFy-1]==1000)
  43. printf("impossible\n");
  44. else
  45. printf("%d\n", pole[posFx-1][posFy-1]);
  46. }
  47.  
  48. return 0;
  49.  
  50. }
  51.