Advertisement
clown1337

Untitled

Feb 11th, 2024
1,177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.34 KB | None | 0 0
  1. #include <algorithm>
  2. #include <bitset>
  3. #include <cassert>
  4. #include <cmath>
  5. #include <cstdint>
  6. #include <cstring>
  7. #include <deque>
  8. #include <fstream>
  9. #include <iomanip>
  10. #include <iostream>
  11. #include <limits>
  12. #include <map>
  13. #include <numeric>
  14. #include <ostream>
  15. #include <queue>
  16. #include <random>
  17. #include <set>
  18. #include <sstream>
  19. #include <stack>
  20. #include <string>
  21. #include <unordered_map>
  22. #include <unordered_set>
  23. #include <vector>
  24.  
  25. using namespace std;
  26. /// Pragmas ///
  27. /// define ///
  28. #define pii pair<int, int>
  29. #define pll pair<ll, ll>
  30. #define V vector
  31. #define MP make_pair
  32. #define vi vector<int>
  33. #define ff first
  34. #define ss second
  35. #define vl vector<long long>
  36. /// typedef ///
  37. typedef long long ll;
  38. typedef long double ld;
  39. typedef unsigned long long ull;
  40.  
  41. /// solve ///
  42. // consts
  43. const double PI = acos(-1);
  44. const ll MOD = 1000000007;
  45. const ll MOD2 = 1000000009;
  46. // const int p = 239;
  47. const ll p = 1'000'000;
  48. long long int inf = 1000009;
  49. const int N = 105;
  50. const double eps = 1e-18;
  51. int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
  52. int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
  53. #define int long long
  54. ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
  55.  
  56. ll lcm(ll a, ll b) { return a * (b / gcd(a, b)); }
  57.  
  58. V<V<char>> g(N, V<char>(N, '#'));
  59.  
  60. V<V<ll>> dist(N, V<ll>(N, inf));
  61. V<V<pll>> pr(N, V<pll>(N));
  62. V<V<bool>> used(N, V<bool>(N, false));
  63. void bfs(int start_x, int start_y) {
  64.   dist[start_x][start_y] = 0;
  65.   used[start_x][start_y] = true;
  66.   queue<pair<int, pair<int, int>>> q;
  67.   q.push({0, {start_x, start_y}});
  68.   while (q.size()) {
  69.     pair<int, pair<int, int>> vv = q.front();
  70.     pair<int, int> v = vv.ss;
  71.     q.pop();
  72.     for (size_t i = 0; i < 4; i++) {
  73.       if (g[v.ff + dx[i]][v.ss + dy[i]] == '#' ||
  74.           g[v.ff + dx[i]][v.ss + dy[i]] == 'A' ||
  75.           g[v.ff + dx[i]][v.ss + dy[i]] == 'B' ||
  76.           g[v.ff + dx[i]][v.ss + dy[i]] == 'a' ||
  77.           g[v.ff + dx[i]][v.ss + dy[i]] == 'b')
  78.         continue;
  79.       pair<int, int> u = {v.ff + dx[i], v.ss + dy[i]};
  80.       if (dist[u.ff][u.ss] >= dist[v.ff][v.ss] + 1 && !used[u.ff][u.ss]) {
  81.         used[u.ff][u.ss] = true;
  82.         dist[u.ff][u.ss] = dist[v.ff][v.ss] + 1;
  83.         pr[u.ff][u.ss] = v;
  84.         q.push({dist[u.ff][u.ss], u});
  85.       }
  86.     }
  87.   }
  88. }
  89.  
  90. void rs() {
  91.   dist.clear();
  92.   dist.resize(N);
  93.   for (size_t i = 0; i < N; i++) {
  94.     dist[i].resize(N, inf);
  95.   }
  96.   pr.clear();
  97.   pr.resize(N);
  98.   for (size_t i = 0; i < N; i++) {
  99.     pr[i].resize(N);
  100.   }
  101.   used.clear();
  102.   used.resize(N);
  103.   for (size_t i = 0; i < N; i++) {
  104.     used[i].resize(N, false);
  105.   }
  106. }
  107.  
  108. void solve() {
  109.   rs();
  110.   int n, m;
  111.   cin >> n >> m;
  112.   int start_xA, start_yA, start_xB, start_yB;
  113.   for (size_t i = 1; i <= n; i++) {
  114.     for (size_t j = 1; j <= m; j++) {
  115.       char x;
  116.       cin >> x;
  117.       g[i][j] = x;
  118.       if (x == 'A') {
  119.         start_xA = i;
  120.         start_yA = j;
  121.       } else if (x == 'B') {
  122.         start_xB = i;
  123.         start_yB = j;
  124.       }
  125.     }
  126.   }
  127.  
  128.   bfs(start_xB, start_yB);
  129.   int distB = dist[1][1];
  130.   rs();
  131.   bfs(start_xA, start_yA);
  132.   int distA = dist[1][1];
  133.   rs();
  134.   if (distA < distB) {
  135.     bfs(start_xA, start_yA);
  136.     pll pos = {1, 1};
  137.     while (pos != make_pair(start_xA, start_yA)) {
  138.       g[pos.ff][pos.ss] = 'a';
  139.       pos = pr[pos.ff][pos.ss];
  140.     }
  141.     rs();
  142.     bfs(start_xB, start_yB);
  143.     pos = {n, m};
  144.     while (pos != make_pair(start_xB, start_yB)) {
  145.       g[pos.ff][pos.ss] = 'b';
  146.       pos = pr[pos.ff][pos.ss];
  147.     }
  148.     for (size_t i = 1; i <= n; i++) {
  149.       for (size_t j = 1; j <= m; j++) {
  150.         cout << g[i][j];
  151.       }
  152.       cout << '\n';
  153.     }
  154.   } else {
  155.     bfs(start_xB, start_yB);
  156.     pll pos = {1, 1};
  157.     while (pos != make_pair(start_xB, start_yB)) {
  158.       g[pos.ff][pos.ss] = 'b';
  159.       pos = pr[pos.ff][pos.ss];
  160.     }
  161.     rs();
  162.     bfs(start_xA, start_yA);
  163.     pos = {n, m};
  164.     while (pos != make_pair(start_xA, start_yA)) {
  165.       g[pos.ff][pos.ss] = 'a';
  166.       pos = pr[pos.ff][pos.ss];
  167.     }
  168.     for (size_t i = 1; i <= n; i++) {
  169.       for (size_t j = 1; j <= m; j++) {
  170.         cout << g[i][j];
  171.       }
  172.       cout << '\n';
  173.     }
  174.   }
  175. }
  176.  
  177. signed main() {
  178.   ios_base::sync_with_stdio(false);
  179.   cin.tie(NULL);
  180.   cout.tie(NULL);
  181.   freopen("input.txt", "r", stdin);
  182.   freopen("output.txt", "w", stdout);
  183.   int t = 1;
  184.   cin >> t;
  185.   while (t--)
  186.     solve();
  187.   return 0;
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement