Source code for submission s760

Go to diff to previous submission

grasshop.cpp

  1. //
  2. // File: grasshop.cc
  3. // Author: cteam053
  4. //
  5. // Created on October 27, 2012, 10:55 AM
  6. //
  7.  
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <cstdio>
  11. #include <cmath>
  12. #include <climits>
  13. #include <iostream>
  14. #include <list>
  15.  
  16. using namespace std;
  17.  
  18. int xmax, ymax;
  19.  
  20. bool code(int x, int y, int& ret){
  21. if(x <= 0 || y <= 0 || x > xmax || y > ymax)
  22. return false;
  23. ret = 1000*x + y;
  24. return true;
  25. }
  26.  
  27.  
  28.  
  29.  
  30. int main(int argc, char** argv) {
  31.  
  32. int R, C, Gr, Gc, Lr, Lc;
  33. list<int> sData;
  34. list<int> len;
  35. int length = -1;
  36. int tmp, x, y, ret, request;
  37.  
  38. while(1){
  39. int ret = scanf("%d%d%d%d%d%d", &R, &C, &Gr, &Gc, &Lr, &Lc);
  40. if(ret != 6)
  41. return 0;
  42.  
  43. xmax = R;
  44. ymax = C;
  45. code(Lr, Lc, request);
  46.  
  47. code(Gr, Gc, ret);
  48. len.clear();
  49. len.push_back(0);
  50. sData.clear();
  51. sData.push_back(ret);
  52. while(!sData.empty()){
  53. tmp = *(sData.begin());
  54. sData.pop_front();
  55. length = *(len.begin());
  56. len.pop_front();
  57. if(tmp == request)
  58. break;
  59. x = tmp / 1000;
  60. y = tmp % 1000;
  61.  
  62. if(abs(x - Gr) > abs(Lr - Gr) + 4)
  63. continue;
  64. if(abs(y - Gc) > abs(Lc - Gc) + 4)
  65. continue;
  66.  
  67. if(code(x-2, y-1, ret)){
  68. sData.push_back(ret);
  69. len.push_back(length+1);
  70. }
  71.  
  72. if(code(x-2, y+1, ret)){
  73. len.push_back(length+1);
  74. sData.push_back(ret);}
  75.  
  76. if(code(x-1, y+2, ret)){
  77. len.push_back(length+1);
  78. sData.push_back(ret);}
  79.  
  80. if(code(x+1, y+2, ret)){
  81. len.push_back(length+1);
  82. sData.push_back(ret);}
  83.  
  84. if(code(x+2, y+1, ret)){
  85. len.push_back(length+1);
  86. sData.push_back(ret);}
  87.  
  88. if(code(x+2, y-1, ret)){
  89. len.push_back(length+1);
  90. sData.push_back(ret);}
  91.  
  92. if(code(x+1, y-2, ret)){
  93. len.push_back(length+1);
  94. sData.push_back(ret);}
  95.  
  96. if(code(x-1, y-2, ret)){
  97. len.push_back(length+1);
  98. sData.push_back(ret);}
  99. }
  100. if(length == 0)
  101. printf("impossible\n");
  102. else
  103. printf("%d\n", length);
  104. }
  105.  
  106.  
  107.  
  108. return (0);
  109. }
  110.  
  111.  

Diff to submission s735

grasshop.cpp

--- c4.s735.cteam053.grasshop.cpp.0.grasshop.cpp
+++ c4.s760.cteam053.grasshop.cpp.0.grasshop.cpp
@@ -59,4 +59,9 @@
             x = tmp / 1000;
             y = tmp % 1000;
+            
+            if(abs(x - Gr) > abs(Lr - Gr) + 4)
+                continue;
+            if(abs(y - Gc) > abs(Lc - Gc) + 4)
+                continue;
 
             if(code(x-2, y-1, ret)){