您的位置:首页 > 其它

hdu-1010-Tempter of the Bone(搜索 优化)

2013-08-16 11:05 381 查看

Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 54764    Accepted Submission(s): 14763


[align=left]Problem Description[/align]
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.

 

[align=left]Input[/align]
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.

 

[align=left]Output[/align]
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.

 

[align=left]Sample Input[/align]

4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0

 

[align=left]Sample Output[/align]

NO
YES

 

[align=left]Author[/align]
ZHANG, Zheng
 

[align=left]Source[/align]
ZJCPC2004

 

[align=left]Recommend[/align]
JGShining
import java.util.Scanner;

public class Main {

/**
* @param args
*/
static char[][] a;
static int x2;
static int y2;
static boolean flag;
static int n;
static int m;
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
while(true){
n=input.nextInt();
m=input.nextInt();
int t=input.nextInt();
if(n==0 && m==0 && t==0)
break;
a=new char
[m];
int x1 = 0,y1 = 0;
int point=0;
for(int i=0;i<n;i++){
String s=input.next();
for(int j=0;j<m;j++){
a[i][j]=s.charAt(j);
if(a[i][j]=='S'){
x1=i;
y1=j;
}
if(a[i][j]=='D'){
x2=i;
y2=j;
}
if(a[i][j]=='.'){
point++;
}
}
}//for
if(Math.abs(x1-x2)+Math.abs(y1-y2)>t || point+1<t){
System.out.println("NO");
continue;
}
flag=false;
a[x1][y1]='X';
dfs(x1,y1,t);
if(flag==true)
System.out.println("YES");
else
System.out.println("NO");
}
}//main
private static void dfs(int x, int y, int t) {
// TODO Auto-generated method stub
if(flag==true)
return;
if(t==0){
if(x==x2 && y==y2)
flag=true;
return;
}
if(Math.abs(x-x2)+Math.abs(y-y2)>t ||
Math.abs(t-Math.abs(x-x2)-Math.abs(y-y2))%2!=0)
return;
//左
if(x-1>=0 && a[x-1][y]!='X'){
a[x-1][y]='X';
dfs(x-1,y,t-1);
a[x-1][y]='.';
}
//右
if(x+1<n && a[x+1][y]!='X'){
a[x+1][y]='X';
dfs(x+1,y,t-1);
a[x+1][y]='.';
}
//上
if(y-1>=0 && a[x][y-1]!='X'){
a[x][y-1]='X';
dfs(x,y-1,t-1);
a[x][y-1]='.';
}
//下
if(y+1<m && a[x][y+1]!='X'){
a[x][y+1]='X';
dfs(x,y+1,t-1);
a[x][y+1]='.';
}

}

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