Advertisement
Md_Ahad

Untitled

Jun 4th, 2025
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4.  
  5. signed main() {
  6.     ios_base::sync_with_stdio(0); cin.tie(0);
  7.     ll h, w; cin >> h >> w;
  8.    
  9.     string s[h];
  10.     for (auto &u : s) cin >> u;
  11.  
  12.     ll cost[h][w];
  13.     for (ll i = 0; i < h; i++) for (ll j = 0; j < w; j++) cost[i][j] = 1e18;
  14.  
  15.     auto isValid = [&](ll i, ll j) {
  16.         return (i >= 0 and i < h and j >= 0 and j < w and s[i][j] == '.');
  17.     };
  18.    
  19.     char ans[h][w];
  20.     queue<pair<ll, ll>> q;
  21.  
  22.     bool visited[h][w];
  23.     memset(visited, false, sizeof(visited));
  24.  
  25.     for (ll i = 0; i < h; i++) {
  26.         for (ll j = 0; j < w; j++) {
  27.             if (s[i][j] != '.') ans[i][j] = s[i][j];
  28.             if (s[i][j] != 'E') continue;
  29.             q.push({i, j});
  30.             cost[i][j] = 0;
  31.             visited[i][j] = true;
  32.         }
  33.     }
  34.    
  35.     while (!q.empty()) {
  36.         auto [i, j] = q.front();
  37.         q.pop();
  38.         pair<ll, ll> d[4] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
  39.        
  40.         for (auto &[dx, dy] : d) {
  41.             ll x = i+dx, y = j+dy;
  42.             if (isValid(x, y) and !visited[x][y] and cost[x][y] > cost[i][j]+1) {
  43.                 cost[x][y] = cost[i][j]+1;
  44.                 if (i == x) {
  45.                     if (j < y) ans[x][y] = '<';
  46.                     else ans[x][y] = '>';
  47.                 }
  48.                 else {
  49.                     if (i < x) ans[x][y] = '^';
  50.                     else ans[x][y] = 'v';
  51.                 }
  52.                 q.push({x, y});
  53.                 visited[x][y] = true;
  54.             }
  55.         }
  56.     }
  57.    
  58.     for (auto &u : ans) {
  59.         for (auto &v : u) cout << v;
  60.         cout << "\n";
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement