Source code for submission s1003

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. printf("test %d\n", t);
  27. if (Gr == Lr || Gc == Gc) {
  28. if ((t+2) % 4 == 0) {
  29. printf("%d\n", t/2);
  30. } else printf("impossible\n");
  31. } else {
  32. if (t % 4 == 0) {
  33. printf("%d\n", t/2);
  34. } else printf("impossible\n");
  35. }
  36. } else if (R == 3 && C == 3) {
  37. if (d == 2) printf("impossible\n");
  38. else if (Lr == 2 && Lc == 2) printf("impossible\n");
  39. else {
  40. switch(d) {
  41. case 1:
  42. printf("3\n");
  43. break;
  44. case 3:
  45. printf("1\n");
  46. break;
  47. case 4:
  48. printf("4\n");
  49. break;
  50. case 5:
  51. printf("3\n");
  52. break;
  53. }
  54. }
  55. } else if (x*2 < y) {
  56. result = y/2;
  57. result += (y%2 > 0) ? 1 : 0;
  58. printf("%d\n", result);
  59. } else if (y*2 < x) {
  60. result = x/2;
  61. result += (x%2 > 0) ? 1 : 0;
  62. printf("%d\n", result);
  63. } else {
  64. switch(d) {
  65. case 1:
  66. printf("3\n");
  67. break;
  68. case 2:
  69. printf("2\n");
  70. break;
  71. case 3:
  72. printf("1\n");
  73. break;
  74. case 4:
  75. printf("3\n");
  76. break;
  77. case 5:
  78. printf("3\n");
  79. break;
  80. }
  81. if (d > 5) {
  82. result += (d % 3) > 0 ? 1 : 0;
  83. result += d/3;
  84. printf("%d\n", result);
  85. }
  86. }
  87. }
  88. return 0;
  89. }
  90.  

Diff to submission s893

grasshop.c

--- c4.s893.cteam074.grasshop.c.0.grasshop.c
+++ c4.s1003.cteam074.grasshop.c.0.grasshop.c
@@ -23,7 +23,15 @@
                         printf("impossible\n");
                 } else if (R == 2 || C == 2) {
-                        if (d % 3 == 0) {
-                                printf("%d\n", d/2);
-                        } else printf("impossible\n");
+                        int t = (x > y) ? x : y;
+                        printf("test %d\n", t);
+                        if (Gr == Lr || Gc == Gc) {
+                                if ((t+2) % 4 == 0) {
+                                        printf("%d\n", t/2);
+                                } else printf("impossible\n");
+                        } else {
+                                if (t % 4 == 0) {
+                                        printf("%d\n", t/2);
+                                } else printf("impossible\n");
+                        }
                 } else if (R == 3 && C == 3) {
                         if (d == 2) printf("impossible\n");
@@ -65,5 +73,5 @@
                                         break;
                                 case 4:
-                                        printf("4\n");
+                                        printf("3\n");
                                         break;
                                 case 5: