HDU 1010 Tempter of the Bone

来源:互联网 发布:阿里旺旺 mac 老版本 编辑:程序博客网 时间:2024/05/24 07:34

Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 76119    Accepted Submission(s): 20867


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 5S.X...X...XD....3 4 5S.X...X....D0 0 0
 

Sample Output
NOYES
敲打查了一个小时的bug,最后的错误居然是数组类型写错了,晕到家了,一点都不细心。
在深搜的函数接口里,先将约束条件写好,然后再进行递归。
这里边有一个知识点:
从一个点到另一个点,这两个点之间的横纵坐标相加再加上给的时间,之和为偶数的话就能走到,否则走不到。
#include <iostream>#include <stdio.h>using namespace std;int n,m,t,flag,endx,endy;char maze[10][10];void dfs(int x,int y,int count){    if(flag==1) return ;    if(maze[x][y]=='X') return ;    if(x==endx&&y==endy&&count==t) {flag=1;return;}    if(x<1||x>n||y<1||y>m) return ;    if(count>t) return ;    maze[x][y]='X';    dfs(x,y+1,count+1);    dfs(x,y-1,count+1);    dfs(x+1,y,count+1);    dfs(x-1,y,count+1);    maze[x][y]='.';    if(flag==1) return ;}int main(void){   // freopen("A.txt","r",stdin);    while(cin>>n>>m>>t)    {        if(n==0&&m==0&&t==0) break;        int startx,starty,point=0;        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]=='X') {point++;}                if(maze[i][j]=='D')  {endx=i;endy=j;}            }        }        if((startx+starty+endx+endy+t)%2==1||t>(n*m-point)){ cout<<"NO"<<endl;continue;}        flag=0;        dfs(startx,starty,0);        if(flag==0)          cout<<"NO"<<endl;        else           cout<<"YES"<<endl;    }    return 0;}

0 0