Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<vector>
- #include<string>
- using namespace std;
- vector<vector<char>>map;
- vector<vector<int>>used;
- int coutmin,n;
- void step(int x1, int y1,int x2,int y2, int count){
- if (used[x1][y1] == 1) {
- return;
- }
- used[x1][y1] = 1;
- if (x1 == x2 && y1 == y2)
- if (coutmin > count)
- coutmin = count;
- if (map[x1][y1] == 'x')
- return;
- if(map[x1][y1] == 'p'){
- if (x1 + 1 < n)
- step(x1 + 1, y1, x2, y2, count + 1);
- if (x1 - 1 >= 0)
- step(x1 - 1, y1, x2, y2, count + 1);
- if (y1 + 1 < n)
- step(x1, y1+1, x2, y2, count + 1);
- if (y1-1 >= 0)
- step(x1, y1-1, x2, y2, count + 1);
- }
- if(map[x1][y1] == 'x'){
- if (x1 + 2 < n && y1+1 < n)
- step(x1 + 2, y1+1, x2, y2, count + 1);
- if (x1 + 2 < n && y1 - 1 >= 0)
- step(x1 + 2, y1-1, x2, y2, count + 1);
- if (x1 - 2 >= 0 && y1 + 1 < n)
- step(x1 - 2, y1 + 1, x2, y2, count + 1);
- if (x1 - 2 >= 0 && y1 -1 >= 0)
- step(x1 -2, y1 -1, x2, y2, count + 1);
- if (x1 + 1 < n && y1 + 2 < n)
- step(x1 +1, y1 + 2, x2, y2, count + 1);
- if (x1 -1 >= 0 && y1 + 2 < n)
- step(x1 - 1, y1 + 2, x2, y2, count + 1);
- if (x1 + 1 < n && y1 - 2 < n)
- step(x1 + 1, y1 - 2, x2, y2, count + 1);
- if (x1 - 1 >= 0 && y1 + 2 < n)
- step(x1 - 1, y1 + 2, x2, y2, count + 1);
- }
- }
- int main(){
- int x1,y1,x2,y2;
- cin >> n;
- coutmin = n * n * n;
- map.assign(n, vector<char>(n));
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n; j++)
- cin >> map[i][j];
- cin >> x1 >> y1 >> x2 >> y2;
- step(x1, y1, x2, y2, 0);
- if (coutmin == n * n * n) {
- cout << "-1";
- return 0;
- }
- cout << coutmin;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement