Source code for submission s893

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

Diff to submission s804

grasshop.c

--- c4.s804.cteam074.grasshop.c.0.grasshop.c
+++ c4.s893.cteam074.grasshop.c.0.grasshop.c
@@ -28,4 +28,5 @@
                 } else if (R == 3 && C == 3) {
                         if (d == 2) printf("impossible\n");
+                        else if (Lr == 2 && Lc == 2) printf("impossible\n");
                         else {
                                 switch(d) {
@@ -33,7 +34,4 @@
                                         printf("3\n");
                                         break;
-                                case 2:
-                                        printf("2\n");
-                                        break;
                                 case 3:
                                         printf("1\n");