tj
2025-07-27 20:05:14
发布于:浙江
#include<bits/stdc++.h>
using namespace std;
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
int n,m,a[1010][1010],sx,sy,fx,fy;
char b[1010][1010];
bool vis[1010][1010],flag;
struct xy{
int x,y;
};
stack<xy>pxy;
stack<xy>npxy;
void dfs(int x,int y){
if(xfx&&yfy){
while(!pxy.empty()){
npxy.push(pxy.top());
pxy.pop();
}
while(!npxy.empty()){
cout<<npxy.top().x-1<<" "<<npxy.top().y-1<<endl;
npxy.pop();
}
flag=1;
return;
}
pxy.push({x,y});
vis[x][y]=1;
for(int i=0;i<4;i++){
int nx=x+dx[i],ny=y+dy[i];
if(nx<1||ny<1||nx>n||ny>m||vis[nx][ny]||a[nx][ny]) continue;
else{
dfs(nx,ny);
}
}
vis[x][y]=0;
pxy.pop();
}
int main() {
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>b[i][j];
if(b[i][j]'X') a[i][j]=1;
if(b[i][j]'S') sx=i,sy=j;
if(b[i][j]=='T') fx=i,fy=j;
}
}
vis[sx][sy]=1;
dfs(sx,sy);
if(flag)cout<<fx-1<<" "<<fy-1<<endl;
if(flag)cout<<"YES";
else cout<<"NO";
return 0;
}
这里空空如也
有帮助,赞一个