Source code for submission s1015

grasshop.cpp

  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. using namespace std;
  6.  
  7. struct Node {
  8. int r;
  9. int c;
  10. int value;
  11. };
  12.  
  13. bool canGo(int GR, int GC, int R, int C)
  14. {
  15. bool can = false;
  16. if(GR >= 1 && GR <= R)
  17. can = true;
  18. if(GC >= 1 && GC <= C)
  19. can = true;
  20. else
  21. can = false;
  22.  
  23. return can;
  24. }
  25.  
  26. int isIn(int r, int c, Node* end,int Rgride, int Cgride, int LR, int LC)
  27. {
  28. if(LR == r && LC == c)
  29. return 0;
  30. for(int i = 0; i < 20; i++)
  31. {
  32. if( end[i].r == r && end[i].c == c )
  33. {
  34. if( canGo(end[i].r ,end[i].c,Rgride,Cgride) && (
  35. canGo(end[i].r+2 ,end[i].c+2,Rgride,Cgride) ||
  36. canGo(end[i].r+2 ,end[i].c-2,Rgride,Cgride) ||
  37. canGo(end[i].r-2 ,end[i].c+2,Rgride,Cgride) ||
  38. canGo(end[i].r-2 ,end[i].c+2,Rgride,Cgride) ) )
  39. return end[i].value;
  40. else
  41. return -1;
  42. }
  43. }
  44. return -1;
  45. }
  46.  
  47. bool isMax(int a, int b){
  48. if(a > b)
  49. return true;
  50. else
  51. return false;
  52. }
  53.  
  54. void createEnd( Node* end , int lr, int lc )
  55. {
  56. end[0].r = lr - 2;
  57. end[0].c = lc - 1;
  58. end[0].value = 1;
  59.  
  60. end[1].r = lr - 2;
  61. end[1].c = lc - 0;
  62. end[1].value = 2;
  63.  
  64. end[2].r = lr - 2;
  65. end[2].c = lc + 1;
  66. end[2].value = 1;
  67. //---
  68. end[3].r = lr - 1;
  69. end[3].c = lc - 2;
  70. end[3].value = 1;
  71.  
  72. end[4].r = lr - 1;
  73. end[4].c = lc - 1;
  74. end[4].value = 2;
  75.  
  76. end[5].r = lr - 1;
  77. end[5].c = lc - 0;
  78. end[5].value = 3;
  79.  
  80. end[6].r = lr - 1;
  81. end[6].c = lc + 1;
  82. end[6].value = 2;
  83.  
  84. end[7].r = lr - 1;
  85. end[7].c = lc + 2;
  86. end[7].value = 1;
  87. //---
  88. end[8].r = lr + 0;
  89. end[8].c = lc - 2;
  90. end[8].value = 2;
  91.  
  92. end[9].r = lr + 0;
  93. end[9].c = lc - 1;
  94. end[9].value = 3;
  95.  
  96. end[10].r = lr + 0;
  97. end[10].c = lc + 1;
  98. end[10].value = 3;
  99.  
  100. end[11].r = lr + 0;
  101. end[11].c = lc + 2;
  102. end[11].value = 2;
  103. //---
  104. end[12].r = lr + 1;
  105. end[12].c = lc - 2;
  106. end[12].value = 1;
  107.  
  108. end[13].r = lr + 1;
  109. end[13].c = lc - 1;
  110. end[13].value = 2;
  111.  
  112. end[14].r = lr + 1;
  113. end[14].c = lc - 0;
  114. end[14].value = 3;
  115.  
  116. end[15].r = lr + 1;
  117. end[15].c = lc + 1;
  118. end[15].value = 2;
  119.  
  120. end[16].r = lr + 1;
  121. end[16].c = lc + 2;
  122. end[16].value = 1;
  123. //---
  124.  
  125. end[17].r = lr + 2;
  126. end[17].c = lc - 1;
  127. end[17].value = 1;
  128.  
  129. end[18].r = lr + 2;
  130. end[18].c = lc - 0;
  131. end[18].value = 2;
  132.  
  133. end[19].r = lr + 2;
  134. end[19].c = lc + 1;
  135. end[19].value = 1;
  136. }
  137.  
  138. void setBoolean(int GR, int GC,int LR,int LC, bool&right, bool&top, bool&convert )
  139. {
  140. if(GR < LR) top = false;
  141. else top = true;
  142. if(GC < LC) right = true;
  143. else right = false;
  144.  
  145. if( top && right ) // nahoru doprava
  146. convert = isMax( GR-LR, GC-LC ); // true: nahoru 2x, doprava 1x; false: nahoru 1x, doprava 2x;
  147. else if( top && !right ) // nahoru doleva
  148. convert = isMax( GR-LR, GC-LC ); // true: nahoru 2x, doleva 1x; false: nahoru 1x, doleva 2x;
  149. else if( !top && right ) // dolu doprava
  150. convert = isMax( GR-LR, GC-LC ); // true: dolu 2x, doprava 1x; false: dolu 1x, doprava 2x;
  151. else if( !top && !right ) // dolu doleva
  152. convert = isMax( GR-LR, GC-LC ); // true: dolu 2x, doleva 1x; false: dolu 1x, doleva 2x;
  153. }
  154.  
  155. int main(void) {
  156. int R,C,GR,GC,LR,LC;
  157. int pomGR,pomGC;
  158. int inR, inC;
  159. bool right,top,convert,posible;
  160. int pomVal;
  161. int counter = 0;
  162. Node* end;
  163. while(scanf("%d %d %d %d %d %d",&R,&C,&GR,&GC,&LR,&LC) == 6)
  164. {
  165. posible = true;
  166. counter = 0;
  167. inR = R;
  168. inC = C;
  169. pomGR = GR;
  170. pomGC = GC;
  171. setBoolean(GR,GC,LR,LC,right,top,convert);
  172.  
  173. end = new Node[20];
  174. createEnd(end, LR, LC);
  175.  
  176. while( (pomVal = isIn(pomGR, pomGC, end, R, C, LR, LC)) == -1 ) {
  177. setBoolean(pomGR,pomGC,LR,LC,right,top,convert);
  178. if( top && right && convert )
  179. {
  180. pomGR = pomGR - 2;
  181. pomGC = pomGC + 1;
  182. }
  183. else if( top && right && !convert )
  184. {
  185. pomGR = pomGR - 1;
  186. pomGC = pomGC + 2;
  187. }
  188. //--------------
  189. else if( top && !right && convert )
  190. {
  191. pomGR = pomGR - 2;
  192. pomGC = pomGC - 1;
  193. }
  194. else if( top && !right && !convert )
  195. {
  196. pomGR = pomGR - 1;
  197. pomGC = pomGC - 2;
  198. }
  199. //--------------
  200. else if( !top && right && convert )
  201. {
  202. pomGR = pomGR + 2;
  203. pomGC = pomGC + 1;
  204. }
  205. else if( !top && right && !convert )
  206. {
  207. pomGR = pomGR + 1;
  208. pomGC = pomGC + 2;
  209. }
  210. //--------------
  211. else if( !top && !right && convert )
  212. {
  213. pomGR = pomGR + 2;
  214. pomGC = pomGC - 1;
  215. }
  216. else if( !top && !right && !convert )
  217. {
  218. pomGR = pomGR + 1;
  219. pomGC = pomGC - 2;
  220. }
  221. if( !canGo(pomGR,pomGC,R,C) )
  222. {
  223. posible = false;
  224. break;
  225. }
  226.  
  227. counter ++;
  228. }
  229. counter += pomVal;
  230.  
  231. if(posible)
  232. cout << counter << endl;
  233. else
  234. cout << "impossible" << endl;
  235. }
  236.  
  237.  
  238. return 0;
  239. }
  240.