Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <bitset>
- #include <cassert>
- #include <cmath>
- #include <cstdint>
- #include <cstring>
- #include <deque>
- #include <fstream>
- #include <iomanip>
- #include <iostream>
- #include <limits>
- #include <map>
- #include <numeric>
- #include <ostream>
- #include <queue>
- #include <random>
- #include <set>
- #include <sstream>
- #include <stack>
- #include <string>
- #include <unordered_map>
- #include <unordered_set>
- #include <vector>
- using namespace std;
- /// Pragmas ///
- /// define ///
- #define pii pair<int, int>
- #define pll pair<ll, ll>
- #define V vector
- #define MP make_pair
- #define vi vector<int>
- #define ff first
- #define ss second
- #define vl vector<long long>
- /// typedef ///
- typedef long long ll;
- typedef long double ld;
- typedef unsigned long long ull;
- /// solve ///
- // consts
- const double PI = acos(-1);
- const ll MOD = 1000000007;
- const ll MOD2 = 1000000009;
- // const int p = 239;
- const ll p = 1'000'000;
- long long int inf = 1000009;
- const int N = 105;
- const double eps = 1e-18;
- int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
- int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
- #define int long long
- ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
- ll lcm(ll a, ll b) { return a * (b / gcd(a, b)); }
- V<V<char>> g(N, V<char>(N, '#'));
- V<V<ll>> dist(N, V<ll>(N, inf));
- V<V<pll>> pr(N, V<pll>(N));
- V<V<bool>> used(N, V<bool>(N, false));
- void bfs(int start_x, int start_y) {
- dist[start_x][start_y] = 0;
- used[start_x][start_y] = true;
- queue<pair<int, pair<int, int>>> q;
- q.push({0, {start_x, start_y}});
- while (q.size()) {
- pair<int, pair<int, int>> vv = q.front();
- pair<int, int> v = vv.ss;
- q.pop();
- for (size_t i = 0; i < 4; i++) {
- if (g[v.ff + dx[i]][v.ss + dy[i]] == '#' ||
- g[v.ff + dx[i]][v.ss + dy[i]] == 'A' ||
- g[v.ff + dx[i]][v.ss + dy[i]] == 'B' ||
- g[v.ff + dx[i]][v.ss + dy[i]] == 'a' ||
- g[v.ff + dx[i]][v.ss + dy[i]] == 'b')
- continue;
- pair<int, int> u = {v.ff + dx[i], v.ss + dy[i]};
- if (dist[u.ff][u.ss] >= dist[v.ff][v.ss] + 1 && !used[u.ff][u.ss]) {
- used[u.ff][u.ss] = true;
- dist[u.ff][u.ss] = dist[v.ff][v.ss] + 1;
- pr[u.ff][u.ss] = v;
- q.push({dist[u.ff][u.ss], u});
- }
- }
- }
- }
- void rs() {
- dist.clear();
- dist.resize(N);
- for (size_t i = 0; i < N; i++) {
- dist[i].resize(N, inf);
- }
- pr.clear();
- pr.resize(N);
- for (size_t i = 0; i < N; i++) {
- pr[i].resize(N);
- }
- used.clear();
- used.resize(N);
- for (size_t i = 0; i < N; i++) {
- used[i].resize(N, false);
- }
- }
- void solve() {
- rs();
- int n, m;
- cin >> n >> m;
- int start_xA, start_yA, start_xB, start_yB;
- for (size_t i = 1; i <= n; i++) {
- for (size_t j = 1; j <= m; j++) {
- char x;
- cin >> x;
- g[i][j] = x;
- if (x == 'A') {
- start_xA = i;
- start_yA = j;
- } else if (x == 'B') {
- start_xB = i;
- start_yB = j;
- }
- }
- }
- bfs(start_xB, start_yB);
- int distB = dist[1][1];
- rs();
- bfs(start_xA, start_yA);
- int distA = dist[1][1];
- rs();
- if (distA < distB) {
- bfs(start_xA, start_yA);
- pll pos = {1, 1};
- while (pos != make_pair(start_xA, start_yA)) {
- g[pos.ff][pos.ss] = 'a';
- pos = pr[pos.ff][pos.ss];
- }
- rs();
- bfs(start_xB, start_yB);
- pos = {n, m};
- while (pos != make_pair(start_xB, start_yB)) {
- g[pos.ff][pos.ss] = 'b';
- pos = pr[pos.ff][pos.ss];
- }
- for (size_t i = 1; i <= n; i++) {
- for (size_t j = 1; j <= m; j++) {
- cout << g[i][j];
- }
- cout << '\n';
- }
- } else {
- bfs(start_xB, start_yB);
- pll pos = {1, 1};
- while (pos != make_pair(start_xB, start_yB)) {
- g[pos.ff][pos.ss] = 'b';
- pos = pr[pos.ff][pos.ss];
- }
- rs();
- bfs(start_xA, start_yA);
- pos = {n, m};
- while (pos != make_pair(start_xA, start_yA)) {
- g[pos.ff][pos.ss] = 'a';
- pos = pr[pos.ff][pos.ss];
- }
- for (size_t i = 1; i <= n; i++) {
- for (size_t j = 1; j <= m; j++) {
- cout << g[i][j];
- }
- cout << '\n';
- }
- }
- }
- signed main() {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- cout.tie(NULL);
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- int t = 1;
- cin >> t;
- while (t--)
- solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement