HDU1010 Tempter of the Bone(DFS+剪枝)

 1 # include <stdio.h>
 2 #include <cstdio>
 3 #include <iostream>
 4 #include <cstring>
 5 #include <cmath>
 6 using namespace std;
 7 char map[8][8];
 8 int visit[8][8];
 9 struct node{
10     int x,y;
11 };
12 int n,m;
13 node Save,Dog;//开始的点和出口位置 
14 int step;//记录总的 步数/时间 
15  
16 int flag = 0;
17 void dfs(int x,int y,int num){
18     visit[x][y] = 1;
19     if(map[x][y] == 'D'){
20         if(num == step)
21             flag = 1;
22         return;
23     }
24     //剪枝 
25     int tmp = step - num - abs(Dog.x-x) - abs(Dog.y-y);
26     if(tmp < 0 || tmp&1){
27         //cout<<x<<" "<<y<<" "<<time<<" "<<abs(c-x)<<" "<<abs(d-y)<<endl;
28         return;
29     }
30     if(num>step){
31         return;
32     }
33     int i;
34     int a[4] = {0,0,1,-1};
35     int b[4] = {-1,1,0,0};
36     for(i=0;i<4;i++){
37         int xx = x+a[i];
38         int yy = y+b[i];
39         if(xx>=0&&xx<n&&yy>=0&&yy<m&&map[xx][yy]!='X'&&!visit[xx][yy]){
40                dfs(xx,yy,num+1);
41                if(flag) return;
42             visit[xx][yy] = 0;
43         }
44     }
45     //return;
46 }
47 int main(){
48     //freopen("in.txt","r",stdin);
49     while(cin>>n>>m>>step){
50         int i,j;
51         if(n==0&&m==0&&step==0) break;
52         for(i=0;i<n;i++){
53             for(j=0;j<m;j++){
54                 cin>>map[i][j];
55                 if(map[i][j]=='S'){
56                     Save.x = i;
57                     Save.y = j;
58                 }
59                 if(map[i][j]=='D'){
60                     Dog.x = i;
61                     Dog.y = j;
62                 }
63             }
64         }
65         
66         flag = 0;
67         memset(visit,0,sizeof(visit));
68         dfs(Save.x,Save.y,0);
69         
70         if(flag==1) cout<<"YES"<<endl;
71         else{
72             cout<<"NO"<<endl;
73         }
74     }    
75 }

 

转载于:https://www.cnblogs.com/MAX-ZMY/p/11562058.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值