计蒜客 走迷宫 dfs

#include
#include
#include
#include
using namespace std;
char Map[11][11];
int n,m;
int ans = 0;
int X[4] = {0,0,1,-1};
int Y[4] = {1,-1,0,0};
bool vis[11][11] ={false};
void dfs(int x,int y){
	if(Map[x][y]=='T'){
		ans++;
		return;
	}
	for(int i=0;i<4;i++){
		int x1 = x + X[i];
		int y1 = y + Y[i];
		if(x1>=0&&x1<n&&y1>=0&&y1<m&&Map[x1][y1]!='#'&&vis[x1][y1]==false){
			vis[x1][y1] = true;
			dfs(x1,y1);
			vis[x1][y1] = false;	
		}
	}
}
int main(){
	//freopen("in.txt","r",stdin);
	cin>>n>>m;
	int sx,sy;
	getchar(); //消除回车 
	for(int i=0;i<n;i++){
		for(int j =0;j<m;j++){
			scanf("%c",&Map[i][j]);
			//cin>>Map[i][j];
			if(Map[i][j]=='S'){
				sx = i;
				sy = j;
			}
		}
		getchar();//消除回车 
	}
	vis[sx][sy] = true;
	dfs(sx,sy);
	printf("%d\n",ans);
	return 0;
} 

你可能感兴趣的:(搜索问题,PTA)