Source code for submission s993

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. koniec=true;
  26. }
  27. }
  28. }
  29.  
  30. int main()
  31. {
  32. pair<int,int> suradnice;
  33. int x,y;
  34. while(scanf("%d%d%d%d%d%d", &r, &s, &m_y, &m_x, &dest_y, &dest_x)==6)
  35. {
  36. memset(grid, 0, sizeof(int)*100*100);
  37. koniec=false;
  38. m_x--; m_y--;
  39. dest_x--; dest_y--;
  40. grid[dest_x][dest_y]=-1;
  41. while(!fronta.empty()) fronta.pop();
  42. //printf("fronta.push(%d,%d)\n", dest_x, dest_y);
  43. fronta.push(make_pair(dest_x,dest_y));
  44. while(!fronta.empty())
  45. {
  46. suradnice=fronta.front();
  47. fronta.pop();
  48. // printf("fronta.pop = (%d,%d)\n", suradnice.first, suradnice.second);
  49.  
  50. x=suradnice.first+2;
  51. y=suradnice.second+1;
  52. skoc(x, y, suradnice.first, suradnice.second);
  53.  
  54. x=suradnice.first+1;
  55. y=suradnice.second+2;
  56. skoc(x, y, suradnice.first, suradnice.second);
  57.  
  58. x=suradnice.first-1;
  59. y=suradnice.second+2;
  60. skoc(x, y, suradnice.first, suradnice.second);
  61.  
  62. x=suradnice.first-2;
  63. y=suradnice.second+1;
  64. skoc(x, y, suradnice.first, suradnice.second);
  65.  
  66. x=suradnice.first-2;
  67. y=suradnice.second-1;
  68. skoc(x, y, suradnice.first, suradnice.second);
  69.  
  70. x=suradnice.first-1;
  71. y=suradnice.second-2;
  72. skoc(x, y, suradnice.first, suradnice.second);
  73.  
  74. x=suradnice.first+1;
  75. y=suradnice.second-2;
  76. skoc(x, y, suradnice.first, suradnice.second);
  77.  
  78. x=suradnice.first+2;
  79. y=suradnice.second-1;
  80. skoc(x, y, suradnice.first, suradnice.second);
  81.  
  82. if(koniec) break;
  83. }
  84. if(grid[m_x][m_y]==0)
  85. printf("impossible\n");
  86. else
  87. printf("%d\n", (grid[m_x][m_y]+1));
  88. }
  89. return 0;
  90. }
  91.  

Diff to submission s863

grasshop.cpp

--- c4.s863.cteam072.grasshop.cpp.0.grasshop.cpp
+++ c4.s993.cteam072.grasshop.cpp.0.grasshop.cpp
@@ -4,4 +4,5 @@
 #include <cmath>
 #include <queue>
+#include <utility>
 
 using namespace std;
@@ -19,4 +20,5 @@
                 {
                 grid[x][y]=grid[from_x][from_y]+1;
+                //printf("x = %d y = %d n = %d --- from_x=%d, from_y=%d\n", x, y, grid[x][y], from_x, from_y);
                 fronta.push(make_pair(x,y));
                 if(x==m_x && y==m_y)
@@ -30,5 +32,5 @@
         pair<int,int> suradnice;
         int x,y;
-        while(scanf("%d%d%d%d%d%d", &r, &s, &m_x, &m_y, &dest_x, &dest_y)==6)
+        while(scanf("%d%d%d%d%d%d", &r, &s, &m_y, &m_x, &dest_y, &dest_x)==6)
         {
                 memset(grid, 0, sizeof(int)*100*100);
@@ -37,4 +39,6 @@
                 dest_x--; dest_y--;
                 grid[dest_x][dest_y]=-1;
+                while(!fronta.empty()) fronta.pop();
+                //printf("fronta.push(%d,%d)\n", dest_x, dest_y);
                 fronta.push(make_pair(dest_x,dest_y));
                 while(!fronta.empty())
@@ -42,4 +46,5 @@
                         suradnice=fronta.front();
                         fronta.pop();
+                //      printf("fronta.pop = (%d,%d)\n", suradnice.first, suradnice.second);
                         
                         x=suradnice.first+2;