Apkawa

Lines

May 10th, 2020
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. 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;
  6.  
  7. char a1[251][251];
  8.  
  9. vector<pair<int, int>> q;
  10.  
  11. main() {
  12.     for( cin >> n; i<n; ++i )
  13.         for( j=0; j<n; ++j ) {
  14.             cin >> a1[i][j];
  15.             if( a1[i][j] == 'X' )
  16.                 a[i][j] = ma,
  17.                 x1 = i,
  18.                 y1 = j;
  19.             if( a1[i][j] == 'O' )
  20.                 a[i][j] = -1;
  21.             if( a1[i][j] == '@' )
  22.                 a[i][j] = 0,
  23.                 x2 = i,
  24.                 y2 = j;
  25.             if( a1[i][j] == '.' )
  26.                 a[i][j] = ma;
  27.         }
  28.     q.push_back( { x2, y2 } );
  29.     for( i=0; i<q.size(); ++i ) {
  30.         x = q[i].first;
  31.         y = q[i].second;
  32.         for( j=0; j<4; ++j ) {
  33.             int tx = x + dx[j];
  34.             int ty = y + dy[j];
  35.             if( tx < 0 || tx >= n || ty < 0 || ty >= n )
  36.                 continue;
  37.             if( a[tx][ty] > a[x][y] + 1 )
  38.                 b[tx][ty] = j,
  39.                 a[tx][ty] = a[x][y] + 1,
  40.                 q.push_back( { tx, ty } );
  41.         }
  42.     }
  43.     if( a[x1][y1] == ma ) {
  44.         cout << "N";
  45.         return 0;
  46.     }
  47.     cout << "Y\n";
  48.     i = x1, j = y1;
  49.     while( i - x2 || j - y2 )
  50.         a1[i][j] = '+',
  51.         i1 = dx[b[i][j]],
  52.         j1 = dy[b[i][j]],
  53.         i -= i1,
  54.         j -= j1;
  55.     a[x2][y2] = '@';
  56.     for( i=0; i<n; ++i ) {
  57.         for( j=0; j<n; ++j )
  58.             cout << a1[i][j];
  59.         cout << endl;
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment