Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define int long long
- #define SZ(X) ((int)(X).size())
- pair<int,int> que[10000];
- int head=0, tail=0;
- bool grid[40][40];
- char out[40][40];
- pair<int,int> back[40][40];
- int dx[] = {0,1,0,-1};
- int dy[] = {1,0,-1,0};
- int32_t main(){
- int n;
- cin >> n;
- pair<int,int> s,e;
- for(int i = 0; i < n; ++i){
- for(int j = 0; j < n; ++j){
- char c;
- cin >> c;
- if(c == 'X')s = make_pair(i,j);
- else if(c == '@')e = make_pair(i,j);
- if(c == 'O')grid[i][j] = 1;
- else grid[i][j] = 0;
- out[i][j] = c;
- }
- }
- back[s.first][s.second] = make_pair(-1,-1);
- grid[s.first][s.second] = 1;
- que[tail++] = s;
- bool yes = 0;
- while(tail - head > 0 && !yes){
- pair<int,int> tmd = que[head++];
- if(tmd == e)break;
- for(int i = 0; i < 4 && !yes; ++i){
- int nx = tmd.first+dx[i];
- int ny = tmd.second+dy[i];
- if(0 <= nx && nx < n && 0 <= ny && ny < n && !grid[nx][ny]){
- back[nx][ny] = tmd;
- que[tail++] = make_pair(nx,ny);
- grid[nx][ny] = 1;
- if(make_pair(nx, ny) == e)yes = 1;
- }
- }
- }
- if(yes){
- cout << 'Y' << '\n';
- while(e != s){
- e = back[e.first][e.second];
- out[e.first][e.second] = '+';
- }
- for(int i = 0; i < n; ++i){
- for(int j = 0; j < n; ++j)cout << out[i][j];
- cout << '\n';
- }
- }else cout << 'N' << '\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment