Source code for submission s801

Go to diff to previous submission

grasshop.cpp

  1. #include <stdio.h>
  2. #include <queue>
  3. #include <vector>
  4. using namespace std;
  5. bool done=false;
  6. bool inboard(int i, int j,int x,int y){
  7. if(i>0 && i<=x && j>0 && j<=y)
  8. return true;
  9. else
  10. return false;
  11. }
  12. class uzol{
  13. public:
  14. int step;
  15. int x,y;
  16. };
  17.  
  18. int step(int x, int y,int step,queue<uzol> & fronta,vector<vector<int> > & board){
  19. if(board[x][y]==-2){
  20. printf("%d\n",step+1);
  21. done=true;
  22. return 0; //koniec
  23. }else if(board[x][y]==-1){
  24. board[x][y]=step+1;
  25. uzol r;
  26. r.x=x;
  27. r.y=y;
  28. r.step=step+1;
  29. fronta.push(r);
  30. } else{
  31.  
  32. }
  33.  
  34. return 1;
  35. }
  36.  
  37. int main(){
  38. int a,b,c,d,e,f;
  39. vector<vector<int> > board;
  40. board.resize(101);
  41. for(int i=0;i<101;i++)
  42. board[i].resize(101);
  43.  
  44.  
  45. while(scanf("%d %d %d %d %d %d",&a,&b,&c,&d,&e,&f)==6){
  46. done=false;
  47. queue<uzol> fronta;
  48. for(int i=0;i<=a;i++){
  49. for(int j=0;j<=b;j++){
  50. board[i][j]=-1;
  51. }
  52. }
  53. board[e][f]=-2;
  54. board[c][d]=0;
  55. uzol f;
  56. f.step=0;
  57. f.x=c;
  58. f.y=d;
  59. fronta.push(f);
  60. while(fronta.size()>0){
  61. uzol node=fronta.front();
  62. fronta.pop();
  63. if(inboard(node.x+2, node.y+1,a,b)){
  64. if(step(node.x+2, node.y+1,node.step,fronta,board)==0)break;
  65. }
  66. if(inboard(node.x+2, node.y-1,a,b)){
  67. if(step(node.x+2, node.y-1,node.step,fronta,board)==0)break;
  68. }
  69. if(inboard(node.x+1, node.y+2,a,b)){
  70. if(step(node.x+1, node.y+2,node.step,fronta,board)==0)break;
  71. }
  72. if(inboard(node.x+1, node.y-2,a,b)){
  73. if(step(node.x+1, node.y-2,node.step,fronta,board)==0)break;
  74. }
  75. if(inboard(node.x-2, node.y+1,a,b)){
  76. if(step(node.x-2, node.y+1,node.step,fronta,board)==0)break;
  77. }
  78. if(inboard(node.x-2, node.y-1,a,b)){
  79. if(step(node.x-2, node.y-1,node.step,fronta,board)==0)break;
  80. }
  81. if(inboard(node.x-1, node.y-2,a,b)){
  82. if(step(node.x-1, node.y-2,node.step,fronta,board)==0)break;
  83. }
  84. if(inboard(node.x-1, node.y+2,a,b)){
  85. if(step(node.x-1, node.y+2,node.step,fronta,board)==0)break;
  86. }
  87.  
  88.  
  89. }
  90. if(done==false)
  91. printf("impossible\n");
  92.  
  93. }
  94.  
  95. return 0;
  96. }
  97.  

Diff to submission s654

grasshop.cpp

--- c4.s654.cteam008.grasshop.cpp.0.grasshop.cpp
+++ c4.s801.cteam008.grasshop.cpp.0.grasshop.cpp
@@ -19,17 +19,17 @@
         if(board[x][y]==-2){
                 printf("%d\n",step+1);
-done=true;
+                done=true;
                 return 0;               //koniec
         }else if(board[x][y]==-1){
                 board[x][y]=step+1;
+                uzol r;
+                r.x=x;
+                r.y=y;  
+                r.step=step+1;
+                fronta.push(r);
         } else{
-                if(board[x][y]>step+1)
-                        board[x][y]=step+1;
+                
         }
-        uzol r;
-        r.x=x;
-        r.y=y;  
-        r.step=step+1;
-        fronta.push(r);
+        
 return 1;
 }
@@ -48,5 +48,5 @@
                 for(int i=0;i<=a;i++){
                         for(int j=0;j<=b;j++){
-                                board[a][b]=-1;
+                                board[i][j]=-1;
                         }
                 }