Source code for submission s674

grasshop.cpp

  1. #include <queue>
  2. #include <cstdio>
  3. #include <utility>
  4. using namespace std;
  5.  
  6. int jumps[8][2] = {{1,2},{2,1},{-1,2},{-2,1},{1,-2},{2,-1},{-1,-2},{-2,-1}};
  7. int main(void){
  8. int r,c,gr,gc,lr,lc;
  9. int pole[100][100];
  10. while (scanf("%d %d %d %d %d %d",&r,&c,&gr,&gc,&lr,&lc)==6){
  11. queue<pair<int,int> > Q;
  12. gr--;
  13. gc--;
  14. lc--;
  15. lr--;
  16. Q.push(pair<int,int>(gr,gc));
  17. for (int i = 0; i<r; i++){
  18. for (int j = 0; j<c; j++){
  19. pole[i][j]=100000;
  20. }
  21. }
  22.  
  23. pole[gr][gc]=0;
  24. while(!Q.empty()){
  25. pair<int,int> p = Q.front();
  26. Q.pop();
  27. for (int i=0; i<9; i++){
  28. int x =p.first+jumps[i][0];
  29. int y =p.second+jumps[i][1];
  30. if ((x>=0 && y >=0 && x <r && y <c)&& (pole[x][y]>pole[p.first][p.second]+1)){
  31. Q.push(pair<int,int>(x,y));
  32. pole[x][y]=pole[p.first][p.second]+1;
  33. if (x == lr && y == lc)
  34. goto konec;
  35. }
  36. }
  37.  
  38. }
  39. konec:
  40. if (pole[lr][lc]== 100000)
  41. printf("impossible\n");
  42. else
  43. printf("%d\n", pole[lr][lc]);
  44. }
  45.  
  46. }
  47.