Source code for submission s863

grasshop.cpp

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