zoj 2110 奇偶性剪枝
来源:互联网 发布:分水岭算法的大米图 编辑:程序博客网 时间:2024/05/21 19:28
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
题解:给你起点和终点,规定一个时间t,如果可能在t秒中到达终点,输出yes
奇偶性剪枝:就是在到达一个点时,目前坐标与终点坐标的绝对值差应该和剩余时间同奇同偶
#include <iostream>#include <cstdio>#include <algorithm>using namespace std;int n,m,t;char mp[10][10];int sx,sy;int ex,ey;int dis[4][2]={1,0,0,1,-1,0,0,-1};int flag;int check(int x,int y) { if(x<0||y<0||x>=n||y>=m) return 1; if(mp[x][y]=='X') return 1; return 0;}void dfs(int x,int y,int step) { mp[sx][sy]='X'; int cnt=abs(x-ex)+abs(y-ey); if((t-step-cnt)%2!=0) return;//同奇偶 if(flag==1) return ; if(t==step&&x==ex&&y==ey) { flag=1; return; } for(int i=0;i<4;i++) { int curx=x+dis[i][0]; int cury=y+dis[i][1]; if(check(curx,cury)) continue; mp[curx][cury]='X'; if(step<=t) dfs(curx,cury,step+1); mp[curx][cury]='.'; }}int main() {// freopen("input.txt","r",stdin); while(scanf("%d%d%d",&n,&m,&t)!=EOF) { if(n==0&&m==0&&t==0) break; for(int i=0;i<n;i++) { scanf("%s",mp[i]); } int tot=0; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(mp[i][j]=='S'){ sx=i; sy=j; } if(mp[i][j]=='D') { ex=i; ey=j; } if(mp[i][j]=='X') tot++; } } if(tot+t>=n*m) { printf("NO\n"); continue; } flag=0; dfs(sx,sy,0); if(flag==1) printf("YES\n"); else printf("NO\n"); } return 0;}
- zoj 2110 奇偶性剪枝
- ZOJ-2110 Tempter of the Bone( 搜索,奇偶性剪枝)
- zoj 2110 hdoj 1010 深度遍历+奇偶性剪枝
- 奇偶性剪枝
- DFS之奇偶性剪枝
- HDU1010(奇偶性剪枝)
- hdu--1010--DFS--奇偶性剪枝
- HDOJ1010深搜+数学奇偶性剪枝
- hdu 1010 dfs+奇偶性剪枝
- hdu 1010 dfs+奇偶性剪枝
- HDU 1010 Bone 奇偶性剪枝
- new hdoj 1010(奇偶性剪枝)
- hdu 1010 回溯加奇偶性剪枝
- 奇偶性剪枝--hdu1010 Tempter of the Bone
- zoj 2110 很好的dfs+奇偶剪枝
- 奇偶剪枝-优化(ZOJ 2110 , HDU 1010)
- hdu--1010--DFS(奇偶性剪枝之经典)
- HDU 1010 Tempter of the Bone(DP+奇偶性剪枝)
- My SQL安装步骤
- 使用Python对文件名批量修改
- 软件设计原则--接口隔离原则
- mybatis 使用简单的增删查改
- 求树的高度
- zoj 2110 奇偶性剪枝
- 请教!!linux上的mysql无法登陆问题!
- SpringMvc入门到开发(一)之入门小程序
- Android Scroller完全解析,关于Scroller你所需知道的一切
- BZOJ 1269 [AHOI 2006] STL(rope)\SPLAY 解题报告
- java反射机制
- 搭建数据库时的笔记;
- Java菜鸟教程 标识符
- 得到一个随机字符[A-Z]or[a-z] (大小写也要随机),并打印