Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int n, a[250][250], b[250][250], i, j, ma = 1e9, x, y, x1, x2, y1, y2, dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0}, i1, j1;
- char a1[251][251];
- vector<pair<int, int>> q;
- main() {
- for( cin >> n; i<n; ++i )
- for( j=0; j<n; ++j ) {
- cin >> a1[i][j];
- if( a1[i][j] == 'X' )
- a[i][j] = ma,
- x1 = i,
- y1 = j;
- if( a1[i][j] == 'O' )
- a[i][j] = -1;
- if( a1[i][j] == '@' )
- a[i][j] = 0,
- x2 = i,
- y2 = j;
- if( a1[i][j] == '.' )
- a[i][j] = ma;
- }
- q.push_back( { x2, y2 } );
- for( i=0; i<q.size(); ++i ) {
- x = q[i].first;
- y = q[i].second;
- for( j=0; j<4; ++j ) {
- int tx = x + dx[j];
- int ty = y + dy[j];
- if( tx < 0 || tx >= n || ty < 0 || ty >= n )
- continue;
- if( a[tx][ty] > a[x][y] + 1 )
- b[tx][ty] = j,
- a[tx][ty] = a[x][y] + 1,
- q.push_back( { tx, ty } );
- }
- }
- if( a[x1][y1] == ma ) {
- cout << "N";
- return 0;
- }
- cout << "Y\n";
- i = x1, j = y1;
- while( i - x2 || j - y2 )
- a1[i][j] = '+',
- i1 = dx[b[i][j]],
- j1 = dy[b[i][j]],
- i -= i1,
- j -= j1;
- a[x2][y2] = '@';
- for( i=0; i<n; ++i ) {
- for( j=0; j<n; ++j )
- cout << a1[i][j];
- cout << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment