标签:des style class code java tar ext width color get int
Tempter of the BoneTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 64326 Accepted Submission(s): 17567 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
Recommend
|
神马是奇偶剪枝,以一个4*4的图举例,当前坐标为偶数和为0,否则为1
0 1 0 1
1 0 1 0
0 1 0 1
1 0 1 0
#include<stdio.h>
#include<memory.h>
#include <iostream>
#include<cmath>
using namespace std;
#define size 10
int
N,M,T;
char maze[size][size];
int startx,starty,endx,endy;
int
dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
bool
flag;
void dfs(int x, int y,
int time)
{
if
(x<=0
|| x>N ||
y<= 0
|| y>M) return;
if (flag) return; //1.找到解后还有部分仍在搜索,这个为了让没必要的搜索终止
if (x == endx && y == endy && time ==
T)
{
flag = true;
return;
}
int temp = (T - time) -
abs(x - endx) - abs(y
- endy);//2.奇偶性剪枝
if(temp<0 || temp &
1) return;
for (int i
= 0; i<4; i++)
{
int
nextx=x+dx[i];
int
nexty=y+dy[i];
if
(maze[nextx][nexty] != ‘X‘)
{
maze[nextx][nexty] =
‘X‘;
dfs(nextx, nexty, time + 1);
maze[nextx][nexty] = ‘.‘;//回溯
}
}
return;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(~scanf("%d%d%d",&N,&M,&T)){
int
num=0;
if(N==0&&M==0&&T==0){
break;
}
for(int i=1;i<=N;i++){
for(int j=1;j<=M;j++){
cin>>maze[i][j];
if(maze[i][j]==‘S‘){
startx=i;
starty=j;
}
if(maze[i][j]==‘D‘){
endx=i;
endy=j;
}
if(maze[i][j]==‘X‘){
num++;
}
}
}
flag=false;
maze[startx][starty]=‘X‘;//这里注意
if(N*M-num <=
T){ //如果可以走的步数少于时间直接NO!所谓估价剪枝
printf("NO\n");
continue;
}
dfs(startx,starty,0);
if(!flag)
printf("NO\n");
else{
printf("YES\n");
}
}
//fclose(stdout);
//fclose(stdin);
return 0;
}
【深搜加剪枝五】HDU 1010 Tempter of the Bone,码迷,mamicode.com
【深搜加剪枝五】HDU 1010 Tempter of the Bone
标签:des style class code java tar ext width color get int
原文地址:http://www.cnblogs.com/zhaokongnuan/p/45a6e0ec95e19fce5b2a4fed00a88193.html