Source code for submission s1071

Go to diff to previous submission

grasshop.cpp

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <queue>
  6. #include <utility>
  7.  
  8. using namespace std;
  9.  
  10. int grid[100][100];
  11. int r, s, m_x, m_y ,dest_x, dest_y;
  12. bool koniec;
  13. queue<pair<int,int> > fronta;
  14.  
  15. void skoc(int x, int y, int from_x, int from_y)
  16. {
  17. if(x<s && y<r && x>=0 && y>=0)
  18. {
  19. if(grid[x][y]==0)
  20. {
  21. grid[x][y]=grid[from_x][from_y]+1;
  22. //printf("x = %d y = %d n = %d --- from_x=%d, from_y=%d\n", x, y, grid[x][y], from_x, from_y);
  23. fronta.push(make_pair(x,y));
  24. if(x==m_x && y==m_y)
  25. {
  26. //printf("koniec=true\n");
  27. koniec=true;
  28. }
  29. }
  30. }
  31. }
  32.  
  33. int main()
  34. {
  35. pair<int,int> suradnice;
  36. int x,y;
  37. while(scanf("%d%d%d%d%d%d", &r, &s, &m_y, &m_x, &dest_y, &dest_x)==6)
  38. {
  39. memset(grid, 0, sizeof(int)*100*100);
  40. koniec=false;
  41. m_x--; m_y--;
  42. dest_x--; dest_y--;
  43. grid[dest_x][dest_y]=-1;
  44. while(!fronta.empty()) fronta.pop();
  45. //printf("fronta.push(%d,%d)\n", dest_x, dest_y);
  46. if(m_x==dest_x && m_y==dest_y)
  47. koniec=true;
  48. fronta.push(make_pair(dest_x,dest_y));
  49. while(!fronta.empty())
  50. {
  51. suradnice=fronta.front();
  52. fronta.pop();
  53. //printf("fronta.pop = (%d,%d)\n", suradnice.first, suradnice.second);
  54.  
  55. x=suradnice.first+2;
  56. y=suradnice.second+1;
  57. skoc(x, y, suradnice.first, suradnice.second);
  58.  
  59. x=suradnice.first+1;
  60. y=suradnice.second+2;
  61. skoc(x, y, suradnice.first, suradnice.second);
  62.  
  63. x=suradnice.first-1;
  64. y=suradnice.second+2;
  65. skoc(x, y, suradnice.first, suradnice.second);
  66.  
  67. x=suradnice.first-2;
  68. y=suradnice.second+1;
  69. skoc(x, y, suradnice.first, suradnice.second);
  70.  
  71. x=suradnice.first-2;
  72. y=suradnice.second-1;
  73. skoc(x, y, suradnice.first, suradnice.second);
  74.  
  75. x=suradnice.first-1;
  76. y=suradnice.second-2;
  77. skoc(x, y, suradnice.first, suradnice.second);
  78.  
  79. x=suradnice.first+1;
  80. y=suradnice.second-2;
  81. skoc(x, y, suradnice.first, suradnice.second);
  82.  
  83. x=suradnice.first+2;
  84. y=suradnice.second-1;
  85. skoc(x, y, suradnice.first, suradnice.second);
  86.  
  87. if(koniec) break;
  88. }
  89. if(!koniec)
  90. printf("impossible\n");
  91. else
  92. printf("%d\n", (grid[m_x][m_y]+1));
  93. }
  94. return 0;
  95. }
  96.  

Diff to submission s993

grasshop.cpp

--- c4.s993.cteam072.grasshop.cpp.0.grasshop.cpp
+++ c4.s1071.cteam072.grasshop.cpp.0.grasshop.cpp
@@ -23,5 +23,8 @@
                 fronta.push(make_pair(x,y));
                 if(x==m_x && y==m_y)
+                {
+                        //printf("koniec=true\n");
                         koniec=true;
+                }
         }
         }
@@ -41,4 +44,6 @@
                 while(!fronta.empty()) fronta.pop();
                 //printf("fronta.push(%d,%d)\n", dest_x, dest_y);
+                if(m_x==dest_x && m_y==dest_y)
+                        koniec=true;
                 fronta.push(make_pair(dest_x,dest_y));
                 while(!fronta.empty())
@@ -46,5 +51,5 @@
                         suradnice=fronta.front();
                         fronta.pop();
-                //      printf("fronta.pop = (%d,%d)\n", suradnice.first, suradnice.second);
+                        //printf("fronta.pop = (%d,%d)\n", suradnice.first, suradnice.second);
                         
                         x=suradnice.first+2;
@@ -82,5 +87,5 @@
                         if(koniec) break;
                 }
-                if(grid[m_x][m_y]==0)
+                if(!koniec)
                         printf("impossible\n");
                 else