查看原题
题意
思路
代码
#include <iostream>
using namespace std;
int booked[7][7],step,all,flag,n,m;
char field[7][7];
void dfs(int x,int y){
int next[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int tempx,tempy;
if(step==all){
flag=1;
return;
}
for(int i=0;i<4;i++){
tempx=x+next[i][0];
tempy=y+next[i][1];
if(tempx<=0||tempx>n||tempy<=0||tempy>m){
continue;
}
if(field[tempx][tempy]!='S'&&booked[tempx][tempy]==0){
step++;
booked[tempx][tempy]=1;
dfs(tempx,tempy);
booked[tempx][tempy]=0;
step--;
}
}
}
int main() {
while(cin>>n>>m&&(n!=0&&m!=0)){
all=0;flag=0;step=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>field[i][j];
if(field[i][j]=='.')all++;
}
}
booked[1][1]=1;
step++;
dfs(1,1);
if(flag)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}