dfs+剪枝
#include<iostream>
#include<math.h>
using namespace std;
int bj[10][10]={0};
char mg[10][10];
int t,p,t1,t2;
int e_x,e_y,k_x,k_y;
int m,n;
void dfs(int x,int y,int t1)
{
if(x==e_x&&y==e_y&&t1==t) {p=1; return;}
t2=t-t1-(abs(e_x-x)+abs(e_y-y));
if(t2<0||t2%2!=0)
return;
if(mg[x+1][y]!='X'&&bj[x+1][y]==0&&x+1<m){bj[x+1][y]=1;dfs(x+1,y,t1+1);bj[x+1][y]=0;if(p==1) return;}
if(mg[x][y+1]!='X'&&bj[x][y+1]==0&&y+1<n){bj[x][y+1]=1;dfs(x,y+1,t1+1);bj[x][y+1]=0;if(p==1) return;}
if(mg[x-1][y]!='X'&&bj[x-1][y]==0&&x-1>=0){bj[x-1][y]=1;dfs(x-1,y,t1+1);bj[x-1][y]=0;if(p==1) return;}
if(mg[x][y-1]!='X'&&bj[x][y-1]==0&&y-1>=0){bj[x][y-1]=1;dfs(x,y-1,t1+1);bj[x][y-1]=0;if(p==1) return;}
}
int main()
{
while(cin>>m>>n>>t)
{
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
{
bj[i][j]=0;
}
int t1=0;
p=0;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
{
cin>>mg[i][j];
if(mg[i][j]=='S')
{
k_x=i;
k_y=j;
}
if(mg[i][j]=='D')
{
e_x=i;
e_y=j;
}
}
if(m==0&&n==0&&t==0) break;
bj[k_x][k_y]=1;
dfs(k_x,k_y,t1);
if(p==1) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
hdu 1010 Tempter of the Bone
最新推荐文章于 2022-12-04 15:41:50 发布