Source code for submission s804

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

Diff to submission s755

grasshop.c

--- c4.s755.cteam074.grasshop.c.0.grasshop.c
+++ c4.s804.cteam074.grasshop.c.0.grasshop.c
@@ -22,9 +22,29 @@
                 else if (R == 1 || C == 1) {
                         printf("impossible\n");
-                }
-                else if (R == 2 || C == 2) {
+                } else if (R == 2 || C == 2) {
                         if (d % 3 == 0) {
                                 printf("%d\n", d/2);
                         } else printf("impossible\n");
+                } else if (R == 3 && C == 3) {
+                        if (d == 2) printf("impossible\n");
+                        else {
+                                switch(d) {
+                                case 1:
+                                        printf("3\n");
+                                        break;
+                                case 2:
+                                        printf("2\n");
+                                        break;
+                                case 3:
+                                        printf("1\n");
+                                        break;
+                                case 4:
+                                        printf("4\n");
+                                        break;
+                                case 5:
+                                        printf("3\n");
+                                        break;
+                        }
+                        }
                 } else if (x*2 < y) {
                         result = y/2;