Source code for submission s581

grasshop.cpp

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <queue>
  5.  
  6. using namespace std;
  7.  
  8. typedef unsigned long int ulint;
  9.  
  10. ulint R, C, GR, GC, LR, LC;
  11.  
  12. static ulint visited[150][150];
  13.  
  14. struct point
  15. {
  16. point () {}
  17. point (ulint x, ulint y) : x(x), y(y) {}
  18. ulint x;
  19. ulint y;
  20. };
  21.  
  22. static int drow[] = {1,1,2,2,-1,-1,-2,-2};
  23. static int dcol[] = {2,-2,1,-1,2,-2,1,-1};
  24.  
  25. inline void solve()
  26. {
  27. visited[GR][GC] = 1;
  28.  
  29. queue<point> bfsq;
  30. bfsq.push(point(GR, GC));
  31.  
  32. point curr;
  33. while (!bfsq.empty())
  34. {
  35. curr = bfsq.front();
  36. bfsq.pop();
  37.  
  38. ulint nx;
  39. ulint ny;
  40. for (uint d = 0; d < 8; ++d)
  41. {
  42. nx = curr.x + drow[d];
  43. ny = curr.y + dcol[d];
  44.  
  45. if (nx > 0 && nx < R && ny > 0 && ny < C && visited[nx][ny] == 0)
  46. {
  47. visited[nx][ny] = visited[curr.x][curr.y] + 1;
  48. bfsq.push(point(nx, ny));
  49. }
  50. }
  51. }
  52.  
  53. if (visited[LR][LC] > 0)
  54. {
  55. printf("%lu\n", visited[LR][LC] - 1);
  56. }
  57. else
  58. {
  59. printf("impossible\n");
  60. }
  61. }
  62.  
  63. int main()
  64. {
  65. while (scanf("%lu %lu %lu %lu %lu %lu", &R, &C, &GR, &GC, &LR, &LC) != EOF)
  66. {
  67. memset(visited, 0, sizeof(visited));
  68. solve();
  69. }
  70. }