Source code for submission s1167

grasshop.c

  1. #include <stdio.h>
  2.  
  3. #define SIZE 100
  4.  
  5. int R, C, Gr, Gc, Lr, Lc, board[SIZE*SIZE], j, s1, s2, i, r1, r2;
  6.  
  7. int m[8][2] = {{-2, 1}, {-1, 2}, {1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}, {-2, -1}};
  8.  
  9.  
  10.  
  11. int main() {
  12.  
  13. while(scanf("%d %d %d %d %d %d", &R, &C, &Gr, &Gc, &Lr, &Lc) == 6) {
  14.  
  15. for(i = 0; i < SIZE*SIZE; i++) { board[i] = 0; }
  16.  
  17. board[(Gr-1)*R+Gc-1] = 1;
  18.  
  19. j = 1;
  20.  
  21. while (board[(Lr-1)*R+Lc-1] == 0 && j < SIZE) {
  22. for (s1 = 0; s1 < R; s1++) {
  23. for (s2 = 0; s2 < C; s2++) {
  24. if (board[s1*R+s2] == j) {
  25. for(i = 0; i < 8; i++) {
  26. r1 = s1+m[i][0];
  27. r2 = s2+m[i][1];
  28.  
  29. if ((r1 >= 0 && r1 < R && r2 >= 0 && r2 < C) && board[r1*R+r2] == 0) {
  30. board[r1*R+r2] = j+1;
  31. }
  32. }
  33. }
  34. }
  35. }
  36. j++;
  37. }
  38.  
  39. if (board[(Lr-1)*R+Lc-1] > 0)
  40. printf("%d\n", board[(Lr-1)*R+Lc-1]-1);
  41. else
  42. printf("impossible\n");
  43.  
  44. }
  45.  
  46. return 0;
  47.  
  48. }