文件目录

#include <bits/stdc++.h>
using namespace std;
int t, n, m, sx, sy, ex, ey;
char c[205][205];
struct node
{
    int x, y, step;
};
int dir[4][2] = {1, 0, -1, 0, 0, -1, 0, 1};
int bfs(int x, int y)
{
    queue<node> q;
    q.push((node){x, y, 0});
    bool vis[205][205] = {};
    vis[x][y] = true;
    while (!q.empty())
    {
        node u = q.front();
        q.pop();
        if (u.x == ex && u.y == ey)
            return u.step;
        for (int i = 0; i < 4; i++)
        {
            int xx = u.x + dir[i][0];
            int yy = u.y + dir[i][1];
            if (xx >= 0 && yy >= 0 && xx < n && yy < m && vis[xx][yy] == false && c[xx][yy] != '#')
            {
                vis[xx][yy] = true;
                q.push((node){xx, yy, u.step + 1});
            }
        }
    }
    return -1;
}
int main()
{
    cin >> t;
    while (t--)
    {
        cin >> n >> m;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
            {
                cin >> c[i][j];
                if (c[i][j] == 'S')
                    sx = i, sy = j;
                if (c[i][j] == 'E')
                    ex = i, ey = j;
            }
        int ans = bfs(sx, sy);
        if (ans != -1)
            printf("%d\n", ans);
        else
            printf("oop!\n");
    }
}