Source code for submission s1052

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 if (x != 0 && x*2 < y) {
  54. result = y/2;
  55. result += (y%2 > 0) ? 1 : 0;
  56. printf("%d\n", result);
  57. } else if (y != 0 && y*2 < x) {
  58. result = x/2;
  59. result += (x%2 > 0) ? 1 : 0;
  60. printf("%d\n", result);
  61. } else {
  62. if (x < 4 && y < 4)
  63. switch(d) {
  64. case 1:
  65. printf("3\n");
  66. break;
  67. case 2:
  68. printf("2\n");
  69. break;
  70. case 3:
  71. printf("1\n");
  72. break;
  73. case 4:
  74. printf("3\n");
  75. break;
  76. } else {
  77. result += (d % 3) > 0 ? 1 : 0;
  78. result += d/3;
  79. printf("%d\n", result);
  80. }
  81. }
  82. }
  83. return 0;
  84. }
  85.  

Diff to submission s1003

grasshop.c

--- c4.s1003.cteam074.grasshop.c.0.grasshop.c
+++ c4.s1052.cteam074.grasshop.c.0.grasshop.c
@@ -24,5 +24,4 @@
                 } else if (R == 2 || C == 2) {
                         int t = (x > y) ? x : y;
-                        printf("test %d\n", t);
                         if (Gr == Lr || Gc == Gc) {
                                 if ((t+2) % 4 == 0) {
@@ -35,6 +34,5 @@
                         }
                 } else if (R == 3 && C == 3) {
-                        if (d == 2) printf("impossible\n");
-                        else if (Lr == 2 && Lc == 2) printf("impossible\n");
+                        if (Lr == 2 && Lc == 2) printf("impossible\n");
                         else {
                                 switch(d) {
@@ -53,13 +51,14 @@
                         }
                         }
-                } else if (x*2 < y) {
+                } else if (x != 0 && x*2 < y) {
                         result = y/2;
                         result += (y%2 > 0) ? 1 : 0;
                         printf("%d\n", result);
-                } else if (y*2 < x) {
+                } 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) {
                                 case 1:
@@ -75,9 +74,5 @@
                                         printf("3\n");
                                         break;
-                                case 5:
-                                        printf("3\n");
-                                        break;
-                        }
-                        if (d > 5) {
+                        } else {
                                 result += (d % 3) > 0 ? 1 : 0;
                                 result += d/3;