就是搜索模板;

#include<bits/stdc++.h>
using namespace std;
const int maxn=510;
int s,t,n,m;
int mp[maxn][maxn],vis[maxn][maxn];
int d[4][2]={{1,0},{0,1},{0,-1},{-1,0}};
int bfs()
{
    queue<int> q;
    q.push(s);vis[s/m][s%m]=1;
    while(!q.empty())
    {
        int cur=q.front();
        int dx=cur/m,dy=cur%m;
        q.pop();
        for(int i=0;i<4;i++)
        {
            int x=dx+d[i][0],y=dy+d[i][1];
            if(x>=n||y>=m||x<0||y<0) continue;//要先判断这个,因为如果同时判断vis,mp数组,x,y可能为负,会造成数组越界
            if(!mp[x][y]||vis[x][y]) continue;
            if(x*m+y==t) return 1;
            q.push(x*m+y);vis[x][y]=1;
        }
    }
    return 0;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(mp,0,sizeof(mp));
        memset(vis,0,sizeof(vis));
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                char ch;
                cin>>ch;
                if(ch=='S') s=i*m+j,mp[i][j]=1;
                if(ch=='E') t=i*m+j,mp[i][j]=1;
                if(ch=='.') mp[i][j]=1;
            }
        }
        if(bfs()) cout<<"Yes"<<endl;
        else cout<<"No"<<endl;
    }
}