一个简单的bfs,直接拿下

#include<queue>
#include<cstring>
using namespace std;
const int N = 510;
char g[N][N];
typedef pair<int, int>PII;
int n, m;
bool st[N][N];
int dx[] = { -1,0,1,0 }, dy[] = { 0,1,0,-1 };
int x, y;
bool bfs()
{
    queue<PII>q;
    q.push({ 0,0 });
    st[0][0] = true;;
    while (q.size())
    {
        auto t = q.front();
        q.pop();
        if (t.first == x && t.second == y) return true;
        for (int i = 0; i < 4; i++)
        {
            int a = t.first + dx[i], b = t.second + dy[i];
            if (a < 0 || a >= n || b < 0 || b >= m)continue;
            if (st[a][b])continue;
            if (g[a][b] == '#')continue;
            q.push({ a,b });
            st[a][b] = true;
        }
    }
    return false;
}

void sove()
{
    while (cin >> n >> m)
    {
        memset(st,0,sizeof st);
        for (int i = 0; i < n; i++)cin >> g[i];


        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                if (g[i][j] == 'E')
                {
                    x = i, y = j;
                    break;
                }
            }
        }
        if (bfs()) cout << "Yes" << endl;
        else cout << "No" << endl;
    }
}
int main()
{
    sove();
    return 0;
}