Source code for submission s1157

Go to diff to previous submission

grasshop.c

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. #define NOT_VISITED -1
  5.  
  6. int R, C, GR, GC, LR, LC;
  7. /* R, C ... area size */
  8. int area[102][102]; /* R-major */
  9.  
  10. typedef struct {int r, c;} point;
  11. typedef struct {point list[102*102]; int len;} data;
  12.  
  13. data aa, bb;
  14.  
  15. int in_grid(point x)
  16. {
  17. int rv = (x.r >= 1 && x.r <= R && x.c >= 1 && x.c <= C);
  18. /*printf("%d,%d = %d (R=%d, C=%d)\n", x.r, x.c, rv, R, C);*/
  19. return rv;
  20. }
  21.  
  22. int main(void)
  23. {
  24. int i, step, row, col, j;
  25. point *p;
  26. int *len_ptr;
  27. point dest[8];
  28. int flag;
  29. data *a, *b, *tmp;
  30.  
  31. while (scanf("%d %d %d %d %d %d ", &R, &C, &GR, &GC, &LR, &LC) == 6) {
  32. a = &aa;
  33. b = &bb;
  34. for (i = 0; i < 102*102; ++i)
  35. ((int*)(area))[i] = NOT_VISITED;
  36.  
  37. if (GR == LR && GC == LC) {
  38. printf("0\n");
  39. continue;
  40. }
  41.  
  42. area[GR][GC] = 0;
  43.  
  44. a->len = 0;
  45. b->len = 0;
  46.  
  47. a->list[0].r = GR;
  48. a->list[0].c = GC;
  49. a->len = 1;
  50.  
  51. for (step = 1; ; ++step)
  52. {
  53. b->len = 0;
  54. flag = 0;
  55. for (i = 0; i < a->len; ++i)
  56. {
  57. row = a->list[i].r;
  58. col = a->list[i].c;
  59. /*
  60. dest = { {row+1, col+2}, {row+1, col-2}, {row-1, col+2}, {row-1, col-2},
  61. {row+2, col+1}, {row+2, col-1}, {row-2, col+1}, {row-2, col-1} };
  62. */
  63. dest[0].r = row+1;
  64. dest[0].c = col+2;
  65.  
  66. dest[1].r = row-1;
  67. dest[1].c = col+2;
  68.  
  69. dest[2].r = row+1;
  70. dest[2].c = col-2;
  71.  
  72. dest[3].r = row-1;
  73. dest[3].c = col-2;
  74.  
  75. dest[4].r = row+2;
  76. dest[4].c = col+1;
  77.  
  78. dest[5].r = row-2;
  79. dest[5].c = col+1;
  80.  
  81. dest[6].r = row+2;
  82. dest[6].c = col-1;
  83.  
  84. dest[7].r = row-2;
  85. dest[7].c = col-1;
  86.  
  87.  
  88. for (j = 0; j < 8; ++j)
  89. {
  90. if (in_grid(dest[j]))
  91. {
  92. if (area[ dest[j].r ][ dest[j].c ] == NOT_VISITED) {
  93. /*printf("setting flag\n");*/
  94. area[ dest[j].r ][ dest[j].c ] = step;
  95. flag = 1;
  96. b->list[ b->len++ ] = dest[j];
  97. /*printf("r=%d, c=%d, LR=%d, LC=%d\n", dest[j].r, dest[j].c, LR, LC);*/
  98. if (dest[j].r == LR && dest[j].c == LC) {
  99. printf("%d\n", step);
  100. goto endloop;
  101. }
  102. }
  103. }
  104. }
  105.  
  106. }
  107. if (flag == 0) {
  108. printf("impossible\n");
  109. goto endloop;
  110. }
  111. tmp = b;
  112. b = a;
  113. a = tmp;
  114. }
  115. endloop:
  116. ;
  117. }
  118. return 0;
  119. }
  120.  
  121.  

Diff to submission s1104

grasshop.c

--- c4.s1104.cteam007.grasshop.c.0.grasshop.c
+++ c4.s1157.cteam007.grasshop.c.0.grasshop.c
@@ -8,5 +8,5 @@
 int area[102][102]; /* R-major */
 
-typedef struct {unsigned char r, c;} point;
+typedef struct {int r, c;} point;
 typedef struct {point list[102*102]; int len;} data;
 
@@ -15,5 +15,7 @@
 int in_grid(point x)
 {
-        return x.r >= 1 && x.r <= R && x.c >= 1 && x.c <= C;
+        int rv = (x.r >= 1 && x.r <= R && x.c >= 1 && x.c <= C);
+        /*printf("%d,%d = %d (R=%d, C=%d)\n", x.r, x.c, rv, R, C);*/
+        return rv;
 }
 
@@ -32,4 +34,10 @@
                 for (i = 0; i < 102*102; ++i)
                         ((int*)(area))[i] = NOT_VISITED;
+                
+                if (GR == LR && GC == LC) {
+                        printf("0\n");
+                        continue;
+                }
+                
                 area[GR][GC] = 0;
                 
@@ -80,9 +88,12 @@
                                 for (j = 0; j < 8; ++j)
                                 {
-                                        if (in_grid(dest[j])) {
+                                        if (in_grid(dest[j]))
+                                        {
                                                 if (area[ dest[j].r ][ dest[j].c ] == NOT_VISITED) {
+                                                        /*printf("setting flag\n");*/
                                                         area[ dest[j].r ][ dest[j].c ] = step;
                                                         flag = 1;
                                                         b->list[ b->len++ ] = dest[j];
+                                                        /*printf("r=%d, c=%d, LR=%d, LC=%d\n", dest[j].r, dest[j].c, LR, LC);*/
                                                         if (dest[j].r == LR && dest[j].c == LC) {
                                                                 printf("%d\n", step);