Advertisement
DaniDori

Untitled

Oct 17th, 2020
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include<string>
  4. using namespace std;
  5. vector<vector<char>>map;
  6. vector<vector<int>>used;
  7. int coutmin,n;
  8. void step(int x1, int y1,int x2,int y2, int count){
  9. if (used[x1][y1] == 1) {
  10. return;
  11. }
  12. used[x1][y1] = 1;
  13. if (x1 == x2 && y1 == y2)
  14. if (coutmin > count)
  15. coutmin = count;
  16. if (map[x1][y1] == 'x')
  17. return;
  18. if(map[x1][y1] == 'p'){
  19. if (x1 + 1 < n)
  20. step(x1 + 1, y1, x2, y2, count + 1);
  21. if (x1 - 1 >= 0)
  22. step(x1 - 1, y1, x2, y2, count + 1);
  23. if (y1 + 1 < n)
  24. step(x1, y1+1, x2, y2, count + 1);
  25. if (y1-1 >= 0)
  26. step(x1, y1-1, x2, y2, count + 1);
  27. }
  28. if(map[x1][y1] == 'x'){
  29. if (x1 + 2 < n && y1+1 < n)
  30. step(x1 + 2, y1+1, x2, y2, count + 1);
  31. if (x1 + 2 < n && y1 - 1 >= 0)
  32. step(x1 + 2, y1-1, x2, y2, count + 1);
  33. if (x1 - 2 >= 0 && y1 + 1 < n)
  34. step(x1 - 2, y1 + 1, x2, y2, count + 1);
  35. if (x1 - 2 >= 0 && y1 -1 >= 0)
  36. step(x1 -2, y1 -1, x2, y2, count + 1);
  37. if (x1 + 1 < n && y1 + 2 < n)
  38. step(x1 +1, y1 + 2, x2, y2, count + 1);
  39. if (x1 -1 >= 0 && y1 + 2 < n)
  40. step(x1 - 1, y1 + 2, x2, y2, count + 1);
  41. if (x1 + 1 < n && y1 - 2 < n)
  42. step(x1 + 1, y1 - 2, x2, y2, count + 1);
  43. if (x1 - 1 >= 0 && y1 + 2 < n)
  44. step(x1 - 1, y1 + 2, x2, y2, count + 1);
  45. }
  46. }
  47. int main(){
  48.  
  49. int x1,y1,x2,y2;
  50. cin >> n;
  51. coutmin = n * n * n;
  52. map.assign(n, vector<char>(n));
  53. for (int i = 0; i < n; i++)
  54. for (int j = 0; j < n; j++)
  55. cin >> map[i][j];
  56. cin >> x1 >> y1 >> x2 >> y2;
  57. step(x1, y1, x2, y2, 0);
  58. if (coutmin == n * n * n) {
  59. cout << "-1";
  60. return 0;
  61. }
  62. cout << coutmin;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement