Source code for submission s1353

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 if ((R == 4 || C == 4) && (x == 0 || y == 0) && d == 5) {
  58. printf("5\n");
  59. } else {
  60. while (x > 2 || y > 2 ) {
  61. if (x > y) {
  62. x -= 2;
  63. y--;
  64. if (y<0) y = 1;
  65. } else {
  66. y -= 2;
  67. x--;
  68. if (x<0) x = 1;
  69. }
  70. result++;
  71. }
  72. switch(x+y) {
  73. case 1:
  74. printf("%d\n", result+3);
  75. break;
  76. case 2:
  77. printf("%d\n", result+2);
  78. break;
  79. case 3:
  80. printf("%d\n", result+1);
  81. break;
  82. case 4:
  83. printf("%d\n", result+4);
  84. break;
  85. }
  86. }
  87. }
  88. return 0;
  89. }
  90.  

Diff to submission s1299

grasshop.c

--- c4.s1299.cteam074.grasshop.c.0.grasshop.c
+++ c4.s1353.cteam074.grasshop.c.0.grasshop.c
@@ -55,4 +55,6 @@
                                 }
                         }
+                } else if ((R == 4 || C == 4) && (x == 0 || y == 0) && d == 5) {
+                        printf("5\n");
                 } else {
                         while (x > 2 || y > 2 ) {
@@ -68,5 +70,4 @@
                                 result++;
                         }
-                        
                         switch(x+y) {
                                 case 1: