Source code for submission s569

Go to diff to previous submission

grasshop.cpp

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define MAXW 200
  5. #define MAXH 200
  6.  
  7. typedef struct ITEM
  8. {
  9. int x;
  10. int y;
  11.  
  12. ITEM() {}
  13. ITEM(int x, int y): x(x), y(y) {}
  14. }
  15. ITEM;
  16.  
  17. ITEM Move[8] =
  18. {
  19. (ITEM) {2, 1},
  20. (ITEM) {-2, 1},
  21. (ITEM) {2, -1},
  22. (ITEM) {-2, -1},
  23. (ITEM) {1, 2},
  24. (ITEM) {-1, 2},
  25. (ITEM) {1, -2},
  26. (ITEM) {-1, -2}
  27. };
  28.  
  29. int Dist[MAXW][MAXH];
  30. ITEM Queue[MAXW * MAXH];
  31. int QueueBeg;
  32. int QueueEnd;
  33.  
  34. int Inside(int x, int y, int w, int h)
  35. {
  36. return ((x >= 0) && (x < w) && (y >= 0) && (y < h));
  37. }
  38.  
  39. void Print(int w, int h)
  40. {
  41. for(int y = 0; y < h; y++)
  42. {
  43. for(int x = 0; x < w; x++)
  44. {
  45. printf("%3d", Dist[x][y]);
  46. }
  47.  
  48. printf("\n");
  49. }
  50. }
  51.  
  52. int main()
  53. {
  54. int w, h, x1, y1, x2, y2;
  55. while(scanf("%d%d%d%d%d%d", &w, &h, &x1, &y1, &x2, &y2) > 0)
  56. {
  57. if((x1 == x2) && (y1 == y2))
  58. {
  59. printf("%d\n", 0);
  60. goto OK;
  61. }
  62.  
  63. x1--;
  64. y1--;
  65. x2--;
  66. y2--;
  67.  
  68. for(int i = 0; i < w; i++)
  69. {
  70. for(int j = 0; j < h; j++)
  71. {
  72. Dist[i][j] = -1;
  73. }
  74. }
  75.  
  76. QueueBeg = 0;
  77. QueueEnd = 0;
  78.  
  79. Dist[x1][y1] = 0;
  80. Queue[QueueEnd++] = ITEM(x1, y1);
  81.  
  82. while(QueueBeg < QueueEnd)
  83. {
  84. ITEM cur = Queue[QueueBeg++];
  85.  
  86. for(int i = 0; i < 8; i++)
  87. {
  88. int x = cur.x + Move[i].x;
  89. int y = cur.y + Move[i].y;
  90. int d = Dist[cur.x][cur.y] + 1;
  91.  
  92. if(Inside(x, y, w, h))
  93. {
  94. if((x == x2) && (y == y2))
  95. {
  96. printf("%d\n", d);
  97. goto OK;
  98. }
  99.  
  100. if(Dist[x][y] == -1)
  101. {
  102. Queue[QueueEnd++] = ITEM(x, y);
  103. Dist[x][y] = d;
  104. }
  105. }
  106. }
  107. }
  108.  
  109. printf("impossible\n");
  110. OK:;
  111.  
  112. //Print(w, h);
  113. }
  114.  
  115. return 0;
  116. }
  117.  

Diff to submission s568

grasshop.cpp

--- c4.s568.cteam096.grasshop.cpp.0.grasshop.cpp
+++ c4.s569.cteam096.grasshop.cpp.0.grasshop.cpp
@@ -55,4 +55,10 @@
         while(scanf("%d%d%d%d%d%d", &w, &h, &x1, &y1, &x2, &y2) > 0)
         {
+                if((x1 == x2) && (y1 == y2))
+                {
+                        printf("%d\n", 0);
+                        goto OK;
+                }
+                
                 x1--;
                 y1--;