dfs记录路径(非最简)
while(head
{
head++;
for(int i=0;i<4;i++)
{
xx=w[head].x+step[i][0];
yy=w[head].y+step[i][1];
if(p[xx][yy]=='.'&&f[xx][yy]==0)
{
f[xx][yy]=1;
}
}
cout<<"NO";
loop: ;
#include
using namespace std;
char p[1200][1200];
int f[1200][1200];
int step[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int temp=0,n,coutt=0;
struct kkk
{
int x,y;
}w[12000];
kkk q[12000];
void dfs(int xi,int yi)
{
q[coutt].x=xi;
q[coutt].y=yi;
if(temp==0)coutt++;
int xx,yy;
if(p[xi][yi]=='N'){temp=1;return ;}
for(int i=0;i<4;i++)
{
xx=xi+step[i][0];
yy=yi+step[i][1];
if(p[xx][yy]!='#'&&f[xx][yy]==0&&xx>0&&yy>0&&xx
{
f[xx][yy]=1;
dfs(xx,yy);
f[xx][yy]=0;
}
}
if(temp==0)coutt--;
return ;
}
int main()
{
int a,b;
cin>>n;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
cin>>p[i][j];
if(p[i][j]=='E')
{
a=i;b=j;
}
}
dfs(a,b);
if(temp==1)
cout<<"YES"<<endl;
else cout<<"NO"<<endl;
if(temp==1)
for(int i=0;i<=coutt-1;i++)
cout<<q[i].x<<' '<<q[i].y<<endl;
return 0;
}
bfs记录路径(最简)
#include
#include
using namespace std;
char p[1200][1200];
int f[1200][1200];
int head=0,end=2;
int step[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
struct kkk
{
int x,y;
char xs[12000],ys[12000];
}w[12000];
void bfs(int xi,int yi)
{
char xx,yy;
w[head+1].x=xi;
w[head+1].y=yi;
w[end].x=xx;
w[end].y=yy;
strcpy(w[end].xs,w[head].xs);
strcpy(w[end].ys,w[head].ys);
w[end].xs[strlen(w[end].xs)]='0'+xx;
w[end].ys[strlen(w[end].ys)]='0'+yy;
end++;
}
if(p[xx][yy]=='N')
{
cout<<"YES";
strcpy(w[end].xs,w[head].xs);
strcpy(w[end].ys,w[head].ys);
w[end].xs[strlen(w[end].xs)]='0'+xx;
w[end].ys[strlen(w[end].ys)]='0'+yy;
return ;
}
}
int main()
{
int n,a1,b1,a2,b2;
cin>>n;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
cin>>p[i][j];
if(p[i][j]=='E')
{
a1=i;b1=j;
}
if(p[i][j]=='N')
{
a2=i;b2=j;
}
}
bfs(a1,b1);
for(int i=0;i
cout<<w[end].xs[i]<<' '<<w[end].ys[i]<<endl;
cout<<a2<<' '<<b2<<endl;
return 0;
}