Source code for submission s595

Go to diff to previous submission

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. /*
  54.   for (uint i = 1; i<=R; i++) {
  55.   for (uint j=1; j<=C; j++) {
  56.   printf("%lu ",visited[i][j]);
  57.   }
  58.   printf("\n");
  59.   }
  60.   */
  61.  
  62. if (visited[LR][LC] > 0)
  63. {
  64. printf("%lu\n", visited[LR][LC] - 1);
  65. }
  66. else
  67. {
  68. printf("impossible\n");
  69. }
  70. }
  71.  
  72. int main()
  73. {
  74. while (scanf("%lu %lu %lu %lu %lu %lu", &R, &C, &GR, &GC, &LR, &LC) != EOF)
  75. {
  76. memset(visited, 0, sizeof(visited));
  77. solve();
  78. }
  79. }

Diff to submission s581

grasshop.cpp

--- c4.s581.cteam097.grasshop.cpp.0.grasshop.cpp
+++ c4.s595.cteam097.grasshop.cpp.0.grasshop.cpp
@@ -43,5 +43,5 @@
             ny = curr.y + dcol[d];
             
-            if (nx > 0 && nx < R && ny > 0 && ny < C && visited[nx][ny] == 0)
+            if (nx > 0 && nx <= R && ny > 0 && ny <= C && visited[nx][ny] == 0)
             {
                 visited[nx][ny] = visited[curr.x][curr.y] + 1;
@@ -51,4 +51,13 @@
     }
     
+    /*
+    for (uint i = 1; i<=R; i++) {
+        for (uint j=1; j<=C; j++) {
+            printf("%lu ",visited[i][j]);
+        }
+        printf("\n");
+    }
+    */
+    
     if (visited[LR][LC] > 0)
     {