Source code for submission s1299

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

Diff to submission s1271

grasshop.c

--- c4.s1271.cteam074.grasshop.c.0.grasshop.c
+++ c4.s1299.cteam074.grasshop.c.0.grasshop.c
@@ -35,4 +35,5 @@
                 } else if (R == 3 && C == 3) {
                         if (Lr == 2 && Lc == 2) printf("impossible\n");
+                        else if (Gr == 2 && Gc == 2) printf("impossible\n");
                         else {
                                 switch(d) {
@@ -40,4 +41,7 @@
                                                 printf("3\n");
                                                 break;
+                                        case 2:
+                                                printf("2\n");
+                                                break;
                                         case 3:
                                                 printf("1\n");
@@ -56,9 +60,9 @@
                                         x -= 2;
                                         y--;
-                                        if (y<0) y = 2;
+                                        if (y<0) y = 1;
                                 } else {
                                         y -= 2;
                                         x--;
-                                        if (x<0) x = 2;
+                                        if (x<0) x = 1;
                                 }
                                 result++;