利用dfs解决规定路程问题
2014-07-21 00:51
288 查看
今天继续dfs的训练,遇到了一道神题,不停地TLE,我DD都快碎了。。。。。好在经过本渣不懈努力,还是弄了出来,不容易啊,发上来纪念一下,顺便总结一下关于用dfs解决规定路程的问题。
先上题目:
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
Sample Output
附上本渣题解:
map的用处:
v可以把map看成这样:
v0 1 0 1 0 1
v1 0 1 0 1 0
v0 1 0 1 0 1
v1 0 1 0 1 0
v0 1 0 1 0 1
v从为 0的格子走一步,必然走向为
1的格子
v从为 1的格子走一步,必然走向为
0的格子
v即:
v0->1或1->0必然是奇数步
v0->0走1->1必然是偶数步
所以当遇到从 0走向0或从1走向1但是要求时间是奇数的,或者,从1
走向 0
但是要求时间是偶数的都可以直接判断不可达!
所以map表能从一开始就减少可能的情况。
先上题目:
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
附上本渣题解:
//基本思路:通过回溯(即return回来后)把标记的改为未标记的从而得到所有路径 #include<stdio.h> #include<cstdlib> int r,c,t,x,y,x1,y1; int dx[4]={0,0,1,-1}; int dy[4]={1,-1,0,0}; char point[7][7]; bool flag; int count; int map[7][7];//能够帮助去掉不可能的情况,不用就TLE void dfs(int x0,int y0,int r,int c,int step)//step记录当前点在当前路径的步数 { int temp; temp=count-step-abs(x0-x1)-abs(y0-y1)+1;//通过对当前点和最终点的位置判断当前点有无可能满足条件,不用就TLE if (temp<0||temp%2==1)//这是通过奇偶判断,数学知识,易忽视。。 return; for(int i=0;i<4;i++) { int tempx=x0+dx[i],tempy=y0+dy[i]; if(point[tempy][tempx]=='D'&&step==count)//判断步数与路径长度是否相同 flag=true; if(tempx>=0&&tempx<c&&tempy>=0&&tempy<r&&point[tempy][tempx]=='.') { point[tempy][tempx]='X';//通过使当前点变为X来标记当前路径是否经过 dfs(tempx,tempy,r,c,step+1);//探索邻接点 point[tempy][tempx]='.';//回溯还原,使其得能够探索全部路径 if(flag==true)//如果存在,就直接返回,不用此判断就TLE。。。 return ; } } return ; } int main() { for(int i=0;i<7;i++) for(int j=0;j<7;j++) { if((i+j)%2) map[i][j]=0; else map[i][j]=1; } while(1) { scanf("%d%d%d",&r,&c,&t); getchar(); if(r==0&&c==0&&t==0) break; count=t; for(int i=0;i<r;i++) { scanf("%s",point[i]); } for(int i=0;i<r;i++) for(int j=0;j<c;j++) { if(point[i][j]=='S') { y=i; x=j; } else if(point[i][j]=='D') { y1=i; x1=j; } } if(abs(map[y1][x1]-map[y][x])%2!=t%2)//map判断起点终点的位置是否有可能,不用就TLE。。 { printf("NO\n"); continue; } flag=false; dfs(x,y,r,c,1); if(flag) printf("YES\n"); else printf("NO\n"); } return 0; }
map的用处:
v可以把map看成这样:
v0 1 0 1 0 1
v1 0 1 0 1 0
v0 1 0 1 0 1
v1 0 1 0 1 0
v0 1 0 1 0 1
v从为 0的格子走一步,必然走向为
1的格子
v从为 1的格子走一步,必然走向为
0的格子
v即:
v0->1或1->0必然是奇数步
v0->0走1->1必然是偶数步
所以当遇到从 0走向0或从1走向1但是要求时间是奇数的,或者,从1
走向 0
但是要求时间是偶数的都可以直接判断不可达!
所以map表能从一开始就减少可能的情况。
相关文章推荐
- 利用dfs解决规定路程问题
- (转载)利用 .NET 框架简化发布和解决 DLL Hell 问题
- 利用Observer模式解决组件间通信问题
- Web程序中利用web.config解决无法输出excel页面的问题
- sql server 2005 中的利用ROW_NUMBER() 解决数据分页问题
- 利用sprint设置解决中文显示问题,web.xml
- 如何解决移动应用中 PeekMessage 调用的资源利用问题
- 利用.NET的XML序列化解决系统配置问题
- Web程序中利用web.config解决无法输出excel页面的问题
- Web程序中利用web.config解决无法输出excel页面的问题
- 利用触发器和INSERT INTO ...SELECT...解决Oracle 数据匹配问题
- 利用.NET的XML序列化解决系统配置问题
- 如何利用网络解决技术问题
- 利用errorstack event解决问题
- 利用WebBrowser彻底解决Web打印问题
- 利用netBeans开发JSP+javaBeans总是出错问题的解决
- 利用动态SQL解决排序问题
- 利用errorstack event解决问题
- 利用扩展ActionServlet,解决中文编码问题
- 利用Repeater解决DataGrid的嵌套显示问题