代码如下
#include <iostream>
using namespace std;
int n,m;
char maze[110][110];
bool vis[110][110];
int cnt=0;
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
void dfs (int x,int y){
if (x<0||x>=n||y<0||y>=m||vis[x][y]||maze[x][y]=='#'){
return ;
}
if (maze[x][y]=='T'){
cnt++;
return ;
}
vis[x][y]=true;
dfs (x+1,y);
dfs (x,y+1);
dfs (x-1,y);
dfs (x,y-1);
vis[x][y]=false;
}
int main ()
{
cin>>n>>m;
for (int i=0;i<n;i++){
cin>>maze[i];
}
int x,y;
for (int i=0;i<n;i++){
for (int j=0;j<m;j++){
if (maze[i][j]=='S')
x=i,y=j;
}
}
dfs (x,y);
cout<<cnt<<endl;
return 0;
}
详解欢迎关注我的公众号:王同学的蓝桥杯训练营