博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 1010--DFS第二炮
阅读量:6787 次
发布时间:2019-06-26

本文共 3183 字,大约阅读时间需要 10 分钟。

        HDU 1010 

                               --DFS第二炮

题目:

                                                                                       

                                                                       Tempter of the Bone

     

                                   

                                                                 

                                                                                                                       

                                                                        time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

       

                   

                                                                                            Total Submission(s): 29454 Accepted Submission(s): 8029

Problem Description

The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze.

The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.

Input

The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give the maze layout, with each line containing M characters. A character is one of the following:

'X': a block of wall, which the doggie cannot enter;

'S': the start point of the doggie;
'D': the Door; or
'.': an empty block.

The input is terminated with three 0's. This test case is not to be processed.

Output

For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.

Sample Input

4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0

Sample Output

NO
YES

Author

ZHANG, Zheng

Source

ZJCPC2004

Recommend

JGShining

分析:

        

此题讲的是一只狗要恰好为t秒时走出迷宫,求其可能性。开始时写了一个DFS,悲剧的TLE了。后来才知道有个奇偶性剪枝,对于奇偶性剪枝

        可以把map看成这样:

         0 1 0 1 0 1
         1 0 1 0 1 0
         0 1 0 1 0 1
         1 0 1 0 1 0
         0 1 0 1 0 1
         从为 0 的格子走一步,必然走向为 1 的格子 ,从为 1 的格子走一步,必然走向为 0 的格子
         即:
         0 ->1或1->0 必然是奇数步 ,0->0 走1->1 必然是偶数步
         所以当遇到从 0 走向 0 但是要求时间是奇数的,或者, 从 1 走向 0 但是要求时间是偶数的 都可以直接判断不可达!

         所以

         v=t-time-abs(dx-x)-abs(dy-y); 在这里 判断当前位置步数的奇偶性

         if(v<0||v&1)  

return;

         还有一个地方就是

         if(flag) return; 不写它的话会超时,写上这几句话就能及时的返回结果,同时也说明了DFS的层数之多,导致超时,需注意这一点。

代码:

1 #include
2 #include
3 4 char map[10][10]; 5 int dir[4][2]={
{
0,1},{
0,-1},{
1,0},{-1,0}}; 6 int m,n,t,sx,sy,dx,dy,flag; 7 void dfs(int x,int y,int time) 8 {
9 int i,x1,y1,v; 10 if(flag) 11 return; 12 v=t-time-abs(dx-x)-abs(dy-y); 13 if(v<0||v&1) 14 return; 15 if(x==dx&&y==dy&&t==time) 16 flag=1; 17 for(i=0;i<4;i++) 18 {
19 x1=x+dir[i][0]; 20 y1=y+dir[i][1]; 21 if(x1>=0&&x1
=0&&y1
=t) 60 dfs(sx,sy,0); 61 if(flag==1) 62 printf("YES\n"); 63 else 64 printf("NO\n"); 65 } 66 return 0; 67 }

ps.有时候就得无所谓点,不能太较真哪~~~

转载于:https://www.cnblogs.com/hankers/archive/2012/01/26/2329775.html

你可能感兴趣的文章
捐助账号
查看>>
线程交替运行
查看>>
ubuntu10.04 –像QQ一样截屏,注解
查看>>
三年观察揭示TNF抑制剂持续改善强柱患者躯体功能的预测因子
查看>>
数据库练习
查看>>
ORM了解知识
查看>>
[NHibernate]ISessionFactory配置
查看>>
jboss服务器修改端口说明
查看>>
个人开发—进度记录(三)
查看>>
Idhttp中get与Post的区别
查看>>
比基尼新娘沉醉花海之爱。(组图)
查看>>
jdk1.8.0环境变量设置
查看>>
《会说话的代码》英文能力的提升
查看>>
HDOJ_ACM_命运
查看>>
SpringMVC-拦截器
查看>>
MVC5+EF6 简易版CMS(非接口) 第一章:新建项目
查看>>
弹出窗口2window.open()---2011-11-11 09:47 window.open 打开窗口最大化
查看>>
水平居中
查看>>
2016年微软机试题第一题——FontSize
查看>>
matlab函数_连通区域
查看>>