• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Hug_Sea
博客园    首页    新随笔    联系   管理    订阅  订阅

HDU-1010 Tempter of the Bone

题意:给出N*M的地图,问能否从S到D话费T时间。

思路:dfs奇偶剪枝。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010

 

View Code
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cstdlib>
 4 #include <cmath>
 5 #include <string>
 6 #include <algorithm>
 7 #include <iostream>
 8 using namespace std;
 9 const int N=10; 
10 
11 int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
12 typedef struct In{
13     int x;
14     int y;
15 }In;
16 In start,end;
17 char map[N][N];
18 int n,m,t,sum;
19 bool vis[N][N],flag;
20 
21 void dfs(int x,int y,int cnt){
22     if(flag) return ;
23     if((abs(end.x-x)+abs(end.y-y)+cnt)%2!=t%2||cnt>t) return ;
24     if(n*m-sum<t) return ;
25     if(x==end.x&&y==end.y&&cnt==t) {flag=true;return ;}
26     for(int i=0;i<4;i++){
27         if(x+dir[i][0]>=1&&x+dir[i][0]<=n&&y+dir[i][1]>=1&&y+dir[i][1]<=m&&!vis[x+dir[i][0]][y+dir[i][1]]&&map[x+dir[i][0]][y+dir[i][1]]!='X'){
28             vis[x+dir[i][0]][y+dir[i][1]]=true;
29             dfs(x+dir[i][0],y+dir[i][1],cnt+1);
30             vis[x+dir[i][0]][y+dir[i][1]]=false;
31         }
32     }
33 }
34 
35 int main(){
36     
37 //    freopen("data.in","r",stdin);
38 //    freopen("data.out","w",stdout);
39     
40     while(scanf("%d%d%d",&n,&m,&t),n||m||t){
41         sum=0;
42         getchar();
43         for(int i=1;i<=n;i++){
44             for(int j=1;j<=m;j++){
45                 scanf("%c",&map[i][j]);
46                 if(map[i][j]=='X') sum++;
47                 if(map[i][j]=='S') {start.x=i;start.y=j;}
48                 if(map[i][j]=='D') {end.x=i;end.y=j;}
49             }
50             getchar();
51         }
52         memset(vis,false,sizeof(vis));
53         flag=false;
54         vis[start.x][start.y]=true;
55         dfs(start.x,start.y,0);
56         if(flag) puts("YES");
57         else puts("NO");
58     }
59     return 0;
60 }
posted @ 2012-06-20 19:55  Hug_Sea  阅读(161)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3