[hdu]Tempter of the Bone
2015-06-05 21:18
302 查看
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 85139 Accepted Submission(s): 23194
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
题意:小狗走迷宫。S是开始D是结束,X是墙,问能否在T时刻恰好到达重点。注意是恰好。
题解:因为问的是恰好,所以应该用的是深度优先搜索。注意奇偶剪枝,回溯法也是个重点。
#include "stdio.h"
#include "cmath"
#include "iostream"
using namespace std;
int N,M,T;
char a[10][10];
int vis[10][10];
int ax,ay,bx,by,flag;
int dir[4][2]={{0,-1},{0,1},{-1,0},{1,0}};//上下左右
void dfs(int x,int y,int t)
{
int n,m,i;
if(x==bx&&y==by&&t==T)
{
flag=1;return ;
}
if(t>=T)return ;
for(i=0;i<4;i++)//遍历上下左右
{
if(x+dir[i][0]>=1&&x+dir[i][0]<=N&&y+dir[i][1]>=1&&y+dir[i][1]<=M)//判断是否出格
{
n=x+dir[i][0];m=y+dir[i][1];
if(a
[m]!='X'&&vis
[m]!=1)
{
vis
[m]=1;
dfs(n,m,t+1);//开始深搜
if(flag>0)return ;
vis
[m]=0;//回溯还原标记
}
}
}
}
int main()
{
int t;
while(scanf("%d %d %d",&N,&M,&T)==3&&(N+M+T))
{
for(int i=1;i<=N;i++)
{ getchar();//谜之回车 。。。
for(int j=1;j<=M;j++)
{
cin>>a[i][j];
if(a[i][j]=='S')
{
ax=i;ay=j;
}
if(a[i][j]=='D')
{
bx=i;by=j;
}
}
}
flag=0;t=0;
memset(vis,0,sizeof(vis));
if(abs(ax-bx)+abs(ay-by)>T||(ax+bx+ay+by)%2!=T%2)//普通剪枝和奇偶剪枝。。感觉有一些弱
{cout<<"NO"<<endl;continue;}
vis[ax][ay]=1;
dfs(ax,ay,t);
if(flag==0)cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}
return 0;
}
Total Submission(s): 85139 Accepted Submission(s): 23194
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
题意:小狗走迷宫。S是开始D是结束,X是墙,问能否在T时刻恰好到达重点。注意是恰好。
题解:因为问的是恰好,所以应该用的是深度优先搜索。注意奇偶剪枝,回溯法也是个重点。
#include "stdio.h"
#include "cmath"
#include "iostream"
using namespace std;
int N,M,T;
char a[10][10];
int vis[10][10];
int ax,ay,bx,by,flag;
int dir[4][2]={{0,-1},{0,1},{-1,0},{1,0}};//上下左右
void dfs(int x,int y,int t)
{
int n,m,i;
if(x==bx&&y==by&&t==T)
{
flag=1;return ;
}
if(t>=T)return ;
for(i=0;i<4;i++)//遍历上下左右
{
if(x+dir[i][0]>=1&&x+dir[i][0]<=N&&y+dir[i][1]>=1&&y+dir[i][1]<=M)//判断是否出格
{
n=x+dir[i][0];m=y+dir[i][1];
if(a
[m]!='X'&&vis
[m]!=1)
{
vis
[m]=1;
dfs(n,m,t+1);//开始深搜
if(flag>0)return ;
vis
[m]=0;//回溯还原标记
}
}
}
}
int main()
{
int t;
while(scanf("%d %d %d",&N,&M,&T)==3&&(N+M+T))
{
for(int i=1;i<=N;i++)
{ getchar();//谜之回车 。。。
for(int j=1;j<=M;j++)
{
cin>>a[i][j];
if(a[i][j]=='S')
{
ax=i;ay=j;
}
if(a[i][j]=='D')
{
bx=i;by=j;
}
}
}
flag=0;t=0;
memset(vis,0,sizeof(vis));
if(abs(ax-bx)+abs(ay-by)>T||(ax+bx+ay+by)%2!=T%2)//普通剪枝和奇偶剪枝。。感觉有一些弱
{cout<<"NO"<<endl;continue;}
vis[ax][ay]=1;
dfs(ax,ay,t);
if(flag==0)cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}
return 0;
}
相关文章推荐
- OpenStack行业实践和发展趋势
- NGUI使用3——制作图集&自定义按钮&切换按钮图片
- 关于ssh连接主机git连接github失败的问题
- 输入的优化
- HDU 不容易系列之(3)—— LELE的RPG难题
- 【原创视频教程】SqlServer2008视频教程[共9集]
- NGUI使用2——Button -- 按钮
- [置顶] Python入门-函数的使用到程序的发布安装
- 一行代码解决各种IE兼容问题,IE6,IE7,IE8,IE9,IE10
- Codeforces Round #306 (Div. 2) A
- NGUI使用1——label
- Tomcat的配置
- Maximal Square - LeetCode 221
- 用软件工程的方式开发WebApp
- 正则表达式学习(三)
- UI进阶 —— 收起键盘方法
- JS 判断某个变量是否为空
- mongoDB基本使用(二)
- mongoDB基本使用(二)
- HDU 超级楼梯