题目意思

给出地图,星号是无法走的格子,不能同时踏过F和和M两个格子,也就是走进一个格子没有影响。

解题思路

广度优先搜索,居然我们可以走一个特殊房间,那我们直接把某一个特殊房间直接当成空地即可,分别两次bfs,跑出去到终点的最短距离,再换个房间再跑一遍bfs,跑出另外一条路去终点的最短路径,如果都是不存在说明无法到达,如果一条路不存在选择可以走的那一条,如果都可以去选择小的那一条路。

#pragma GCC target("avx,sse2,sse3,sse4,popcnt")
#pragma GCC optimize("O2,O3,Ofast,inline,unroll-all-loops,-ffast-math")
#include <bits/stdc++.h>
using namespace std;
#define js ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define all(vv) (vv).begin(), (vv).end()
#define endl "\n"
#define pai pair<int, int>
typedef long long ll; typedef unsigned long long ull; typedef long double ld;
const ll MOD = 1e9 + 7;
inline ll read() { ll s = 0, w = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') w = -1; for (; isdigit(ch); ch = getchar())    s = (s << 1) + (s << 3) + (ch ^ 48); return s * w; }
inline void write(ll x) { if (!x) { putchar('0'); return; } char F[40]; ll tmp = x > 0 ? x : -x; if (x < 0)putchar('-');    int cnt = 0;    while (tmp > 0) { F[cnt++] = tmp % 10 + '0';        tmp /= 10; }    while (cnt > 0)putchar(F[--cnt]); }
inline ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll qpow(ll a, ll b) { ll ans = 1;    while (b) { if (b & 1)    ans *= a;        b >>= 1;        a *= a; }    return ans; }    ll qpow(ll a, ll b, ll mod) { ll ans = 1; while (b) { if (b & 1)(ans *= a) %= mod; b >>= 1; (a *= a) %= mod; }return ans % mod; }
inline int lowbit(int x) { return x & (-x); }

const int N = 1e3 + 7;
char mp[N][N], mp2[N][N];
bool vis[N][N];
int dis[N][N];
int n, m, r, c;
int dx[] = { 1,-1,0,0 };
int dy[] = { 0,0,1,-1 };

void bfs() {
    queue<pai> q;
    q.push({ 0,0 });
    dis[0][0] = 0;
    vis[0][0] = 1;
    while (q.size()) {
        pai x = q.front();    q.pop();
        for (int i = 0; i < 4; ++i) {
            int xx = x.first + dx[i], yy = x.second + dy[i];
            if (xx < 0 or xx >= n or yy < 0 or yy >= m or mp2[xx][yy] != '.' or vis[xx][yy])
                continue;
            vis[xx][yy] = 1;
            pai y = make_pair(xx, yy);
            q.push(y);
            dis[xx][yy] = dis[x.first][x.second] + 1;
            if (r == xx and c == yy)    return;
        }
    }
}

int main() {
    int T = read();
    while (T--) {
        n = read(), m = read(), r = read() - 1, c = read() - 1;
        for (int i = 0; i < n; ++i)
            scanf("%s", mp + i);
        for (int i = 0; i < n; ++i)
            for (int j = 0; j < m; ++j)
                if (mp[i][j] == 'F')    mp2[i][j] = '.';
                else    mp2[i][j] = mp[i][j];
        memset(dis, -1, sizeof(dis));
        memset(vis, 0, sizeof(vis));
        bfs();
        int ans1 = dis[r][c];
        for (int i = 0; i < n; ++i)
            for (int j = 0; j < m; ++j)
                if (mp[i][j] == 'M')    mp2[i][j] = '.';
                else    mp2[i][j] = mp[i][j];
        memset(dis, -1, sizeof(dis));
        memset(vis, 0, sizeof(vis));
        bfs();
        int ans2 = dis[r][c];
        if (ans1 == -1 and ans2 == -1)
            puts("IMPOSSIBLE");
        else {
            if (ans1 == -1 or ans2 == -1)
                printf("%d\n", max(ans1, ans2) << 1);
            else
                printf("%d\n", min(ans1, ans2) << 1);
        }
    }
    return 0;
}