您的位置:首页 > 其它

hdu1010-深搜+奇偶剪枝-Tempter of the Bone

2014-12-25 20:09 344 查看
<div class="plm" style="text-align: center; font-size: 12pt; color: rgb(34, 34, 34); clear: both;"><div class="ptt" id="problem_title" style="font-size: 18pt; font-weight: bold; color: blue; padding: 10px;">Tempter of the Bone</div></div><div style="color: rgb(34, 34, 34); font-family: Verdana, Arial, sans-serif; font-size: 14.3999996185303px; width: 960px; margin: auto;"><div class="hiddable" id="vj_description" style="font-family: 'times new roman'; font-size: 17px;"><p class="pst" style="font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; margin-bottom: 0px;">Description</p></div></div>


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


第一次用深搜写超时了,后来知道用奇偶剪枝,结果交上去wa了,,后来再搜了别人的代码,原来是存地图时用了%c,用%s就对了,其实用%c也行,是getchar()加错了地方。。具体见代码吧

#include<stdio.h>
#include<string.h>
#include<queue>
#include<math.h>
using namespace std;
int n,m,T,flag;
int vis[10][10];
int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
struct Info
{
char c;
int x,y,time;
}s[10][10];
void dfs(Info t)
{
int i,xx,yy;
if(t.c=='D')
{
if(t.time==T)
{
flag=1;
printf("YES\n");
}
return;
}
for(i=0;i<4;i++)
{
xx=t.x+dir[i][0];
yy=t.y+dir[i][1];
if(xx>=0&&xx<n&&yy>=0&&yy<m&&s[xx][yy].c!='X'&&vis[xx][yy]==0)
{
vis[xx][yy]=1;
s[xx][yy].time=t.time+1;
dfs(s[xx][yy]);
if(flag==1)
return;
vis[xx][yy]=0;
}
}
}
int main()
{
int i,j,x1,y1,x2,y2,cnt,ans;
char cc[10][10];
while(scanf("%d%d%d",&n,&m,&T)!=EOF&&(n||m||T))
{
flag=0;cnt=0;
memset(vis,0,sizeof(vis));
//getchar();
for(i=0;i<n;i++)
{
scanf("%s",cc[i]);
for(j=0;j<m;j++)
{
//scanf("%c",&s[i][j].c);
//getchar();
s[i][j].c=cc[i][j];
s[i][j].x=i;
s[i][j].y=j;
s[i][j].time=0;
if(s[i][j].c=='S')
{
x1=i;
y1=j;
}
else if(s[i][j].c=='D')
{
x2=i;
y2=j;
}
}
}
ans=fabs(x1-x2)+fabs(y1-y2);
if((T-ans)%2!=0)
{
printf("NO\n");
continue;
}
vis[x1][y1]=1;
dfs(s[x1][y1]);
if(flag==0)
{
printf("NO\n");
}

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: