Source code for submission s1090

Go to diff to previous submission

grasshop.c

  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. int R,C, Lr, Lc, Gr, Gc;
  5.  
  6. int get_dist(int a, int b, int x, int y)
  7. {
  8. return fabs(x-a) + fabs(y-b);
  9. }
  10.  
  11. int main()
  12. {
  13. while(scanf("%d %d %d %d %d %d", &R,&C,&Gr,&Gc,&Lr,&Lc) == 6)
  14. {
  15. int d = get_dist(Gr, Gc, Lr, Lc);
  16. int x = fabs(Gr-Lr);
  17. int y = fabs(Gc-Lc);
  18. int result = 0;
  19. if (d == 0) {
  20. printf("0\n");
  21. }
  22. else if (R == 1 || C == 1) {
  23. printf("impossible\n");
  24. } else if (R == 2 || C == 2) {
  25. int t = (x > y) ? x : y;
  26. if (Gr == Lr || Gc == Gc) {
  27. if ((t+2) % 4 == 0) {
  28. printf("%d\n", t/2);
  29. } else printf("impossible\n");
  30. } else {
  31. if (t % 4 == 0) {
  32. printf("%d\n", t/2);
  33. } else printf("impossible\n");
  34. }
  35. } else if (R == 3 && C == 3) {
  36. if (Lr == 2 && Lc == 2) printf("impossible\n");
  37. else {
  38. switch(d) {
  39. case 1:
  40. printf("3\n");
  41. break;
  42. case 3:
  43. printf("1\n");
  44. break;
  45. case 4:
  46. printf("4\n");
  47. break;
  48. case 5:
  49. printf("3\n");
  50. break;
  51. }
  52. }
  53. } else {
  54. while (x > 2 || y > 2 ) {
  55. if (x > y) {
  56. x -= 2;
  57. y--;
  58. if (y<0) y = 2;
  59. } else {
  60. y -= 2;
  61. x--;
  62. if (x<0) x = 2;
  63. }
  64. result++;
  65. }
  66.  
  67. switch(x+y) {
  68. case 1:
  69. printf("%d\n", result+3);
  70. break;
  71. case 2:
  72. printf("%d\n", result+2);
  73. break;
  74. case 3:
  75. printf("%d\n", result+1);
  76. break;
  77. case 4:
  78. printf("%d\n", result+4);
  79. break;
  80. }
  81. }
  82. }
  83. return 0;
  84. }
  85.  

Diff to submission s1052

grasshop.c

--- c4.s1052.cteam074.grasshop.c.0.grasshop.c
+++ c4.s1090.cteam074.grasshop.c.0.grasshop.c
@@ -37,45 +37,45 @@
                         else {
                                 switch(d) {
-                                case 1:
-                                        printf("3\n");
-                                        break;
-                                case 3:
-                                        printf("1\n");
-                                        break;
-                                case 4:
-                                        printf("4\n");
-                                        break;
-                                case 5:
-                                        printf("3\n");
-                                        break;
-                        }
+                                        case 1:
+                                                printf("3\n");
+                                                break;
+                                        case 3:
+                                                printf("1\n");
+                                                break;
+                                        case 4:
+                                                printf("4\n");
+                                                break;
+                                        case 5:
+                                                printf("3\n");
+                                                break;
+                                }
                         }
-                } else if (x != 0 && x*2 < y) {
-                        result = y/2;
-                        result += (y%2 > 0) ? 1 : 0;
-                        printf("%d\n", result);
-                } else if (y != 0 && y*2 < x) {
-                        result = x/2;
-                        result += (x%2 > 0) ? 1 : 0;
-                        printf("%d\n", result);
                 } else {
-                        if (x < 4 && y < 4)
-                        switch(d) {
+                        while (x > 2 || y > 2 ) {
+                                if (x > y) {
+                                        x -= 2;
+                                        y--;
+                                        if (y<0) y = 2;
+                                } else {
+                                        y -= 2;
+                                        x--;
+                                        if (x<0) x = 2;
+                                }
+                                result++;
+                        }
+                        
+                        switch(x+y) {
                                 case 1:
-                                        printf("3\n");
+                                        printf("%d\n", result+3);
                                         break;
                                 case 2:
-                                        printf("2\n");
+                                        printf("%d\n", result+2);
                                         break;
                                 case 3:
-                                        printf("1\n");
+                                        printf("%d\n", result+1);
                                         break;
                                 case 4:
-                                        printf("3\n");
+                                        printf("%d\n", result+4);
                                         break;
-                        } else {
-                                result += (d % 3) > 0 ? 1 : 0;
-                                result += d/3;
-                                printf("%d\n", result);
                         }
                 }