Source code for submission s755

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. }
  25. else if (R == 2 || C == 2) {
  26. if (d % 3 == 0) {
  27. printf("%d\n", d/2);
  28. } else printf("impossible\n");
  29. } else if (x*2 < y) {
  30. result = y/2;
  31. result += (y%2 > 0) ? 1 : 0;
  32. printf("%d\n", result);
  33. } else if (y*2 < x) {
  34. result = x/2;
  35. result += (x%2 > 0) ? 1 : 0;
  36. printf("%d\n", result);
  37. } else {
  38. switch(d) {
  39. case 1:
  40. printf("3\n");
  41. break;
  42. case 2:
  43. printf("2\n");
  44. break;
  45. case 3:
  46. printf("1\n");
  47. break;
  48. case 4:
  49. printf("4\n");
  50. break;
  51. case 5:
  52. printf("3\n");
  53. break;
  54. }
  55. if (d > 5) {
  56. result += (d % 3) > 0 ? 1 : 0;
  57. result += d/3;
  58. printf("%d\n", result);
  59. }
  60. }
  61. }
  62. return 0;
  63. }
  64.  

Diff to submission s685

grasshop.c

--- c4.s685.cteam074.grasshop.c.0.grasshop.c
+++ c4.s755.cteam074.grasshop.c.0.grasshop.c
@@ -14,4 +14,7 @@
         {
                 int d = get_dist(Gr, Gc, Lr, Lc);
+                int x = fabs(Gr-Lr);
+                int y = fabs(Gc-Lc);
+                int result = 0;
                 if (d == 0) {
                         printf("0\n");
@@ -22,6 +25,14 @@
                 else if (R == 2 || C == 2) {
                         if (d % 3 == 0) {
-                                printf("%d\n", d/3);
+                                printf("%d\n", d/2);
                         } else printf("impossible\n");
+                } else if (x*2 < y) {
+                        result = y/2;
+                        result += (y%2 > 0) ? 1 : 0;
+                        printf("%d\n", result);
+                } else if (y*2 < x) {
+                        result = x/2;
+                        result += (x%2 > 0) ? 1 : 0;
+                        printf("%d\n", result);
                 } else {
                         switch(d) {
@@ -43,5 +54,5 @@
                         }
                         if (d > 5) {
-                                int result = (d % 3) > 0 ? 1 : 0;
+                                result += (d % 3) > 0 ? 1 : 0;
                                 result += d/3;
                                 printf("%d\n", result);