Source code for submission s809

grasshop.cpp

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. int main()
  6. {
  7. int a,b,c,d,e,f;
  8. while(scanf("%i %i %i %i %i %i",&a,&b,&c,&d,&e,&f)==6)
  9. {
  10. int dx = abs(c-e);
  11. int dy = abs(f-d);
  12.  
  13. if (dx == 0 && dy == 0)
  14. {
  15. printf("0\n");
  16. continue;
  17. }
  18.  
  19. if (a < 3 && b < 3)
  20. {
  21. printf("impossible\n");
  22. continue;
  23. }
  24.  
  25. if (a==3 && b == 3 && c == 2 && d == 2)
  26. {
  27. printf("impossible\n");
  28. continue;
  29. }
  30.  
  31. if (a==3 && b == 3 && e == 2 && f == 2)
  32. {
  33. printf("impossible\n");
  34. continue;
  35. }
  36.  
  37. if (a == 2)
  38. {
  39. if (dy%2 != 0)
  40. {
  41. printf("impossible\n");
  42. continue;
  43. }
  44. if ((dy%4 == 0 && dx == 0) || (dy%4 != 0 && dx == 1))
  45. {
  46. printf("%i\n", dy/2);
  47. continue;
  48. }
  49. printf("impossible\n");
  50. continue;
  51. }
  52.  
  53. if (b == 2)
  54. {
  55. if (dx%2 != 0)
  56. {
  57. printf("impossible\n");
  58. continue;
  59. }
  60. if ((dx%4 == 0 && dy == 0) || (dx%4 != 0 && dy == 1))
  61. {
  62. printf("%i\n", dx/2);
  63. continue;
  64. }
  65. printf("impossible\n");
  66. continue;
  67. }
  68.  
  69. int n = 0;
  70. while (dx > 2 || dy > 2)
  71. {
  72. if (dx > dy)
  73. {
  74. dx = abs(dx- 2);
  75. dy = abs(dy -1);
  76. }
  77. else
  78. {
  79. dx = abs(dx- 2);
  80. dy = abs(dy -1);
  81. }
  82. n++;
  83. }
  84.  
  85.  
  86. if (dx == 0 && dy == 0) n += 0;
  87. if (dx == 0 && dy == 1) n += 3;
  88. if (dx == 0 && dy == 2)
  89. {
  90. if ((a == 3) && ((f == 1) || (f == b-1)) && (e == 2))
  91. n+= 4;
  92. else
  93. n += 2;
  94. }
  95.  
  96. if (dx == 1 && dy == 0) n += 3;
  97. if (dx == 1 && dy == 1)
  98. {
  99. if ((e == a-1) || (f == b-1) || (e==1) || (f == 1) )
  100. n += 4;
  101. else
  102. n += 2;
  103. }
  104. if (dx == 1 && dy == 2) n += 1;
  105.  
  106. if (dx == 2 && dy == 0)
  107. {
  108. if ((b == 3) && ((e == 1) || (e == a-1)) && (f == 2))
  109. n+= 4;
  110. else
  111. n += 2;
  112. }
  113. if (dx == 2 && dy == 1) n += 1;
  114. if (dx == 2 && dy == 2) n += 4;
  115.  
  116. printf("%i\n",n);
  117. continue;
  118.  
  119. }
  120. return 0;
  121. }