#include<bits/stdc++.h> using namespace std; int R,C; char arr[101][101]; int min_step[101][101]; int startx,starty; int endx,endy; int direction[4][2]={{-1,0},{1,0},{0,-1},{0,1}}; void dfs(int x,int y,int step){ for(int i = 0;i<4;i++){ int nx = x+direction[i][0]; int ny = y+direction[i][1];
if(nx>=1 && nx<=R && ny>=1 && ny<=C && (arr[nx][ny]=='.'||arr[nx][ny]=='T')){
if (step + 1 < min_step[nx][ny]) {
min_step[nx][ny] = step + 1;
dfs(nx, ny, step + 1);
}
}
}
} int main(){ cin>>R>>C; for(int i = 1;i<=R;i++){ for(int j = 1;j<=C;j++){ cin>>arr[i][j]; if(arr[i][j]=='S'){ startx = i; starty = j; }else if(arr[i][j]=='T'){ endx = i; endy = j;
}
}
}
for(int i = 1;i<=R;i++){
for(int j = 1;j<=C;j++){
min_step[i][j] = R*C;
}
}
cout<<startx<<" "<<starty<<endl;
cout<<endx<<" "<<endy<<endl;
min_step[startx][starty] = 0;
dfs(startx,starty,0);
cout<<min_step[endx][endy]<<endl;
return 0;
}
共 1 条回复
代码没问题,数据的问题