杭电1010

Tempter of the BoneTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 125246    Accepted Submission(s): 33812
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

http://acm.hdu.edu.cn/showproblem.php?pid=1010           //题目来源
先说说题目的大意吧,看得懂题目的请自动忽略哈!
一只小狗在一个迷宫里,迷宫大小为m*n,即m行n列的迷宫,S代表小狗的起始位置即迷宫入口,D代表门即迷宫出口,小狗走过后那一块就会消失,且不能在原地呆的时间超过一秒,门会在T秒打开,小狗必须要在T秒刚好到达出口,方能活着离开,输出YES,否则NO,总的来说小狗从入口位置要走T步刚好到达出口。所以这一题应该用深搜,广搜只能找到最短的路径,所以对于这题来说并不适合。一开始题目给出m  n  T 三个数,分别是地图的大小和打开的时间(步数),接下来上代码
#include"iostream"
#include"cstring"
using std::cin;
using std::cout;
char map[10][10];//存放地图
int m, n, T;
int sx, sy, ex, ey;//存放S的位置以及D的位置
bool flag;//判断是否存活
int next[4][2] = { 0,1,1,0,0,-1,-1,0 };//下一步的走法
int abs(int x)//绝对值
{
return x<0 ? -x : x;
}
void dfs(int x, int y, int time)//深搜
{
if (x<0 || x >= m || y<0 || y >= n)//判断是否越界
return;
if (x == ex&&y == ey&&time == T)//判断是否到达终点且时间刚好为T
{
flag = true;
return;
}
if (flag)
return;
int temp = T - time - abs(x - ex) - abs(y - ey);//仔细看一下,temp是用来奇偶剪枝的,它是剩余的时间(步数)减去从该点到终点的最短步数
if (temp < 0 || temp & 1)//奇偶剪枝 显然temp为偶数的时候才有可能存活  temp&1 当temp为奇数该表达式为真  temp必须>0 
return;
for (int i = 0; i < 4; i++)//这些就是深搜的基本方法了,在此就不细说了
{
int nx = x + next[i][0];
int ny = y + next[i][1];
if (map[nx][ny] != 'X')
{
map[nx][ny] = 'X';
dfs(nx, ny, time + 1);
map[nx][ny] = '.';
if (flag)
return;
}
}
}
int main()
{
int wall_count;
while (std::cin >> m >> n >> T)
{
if (m == 0 && n == 0)
break;
wall_count = 0;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
std::cin >> map[i][j];
if (map[i][j] == 'S')
{
sx = i;
sy = j;
}
else if (map[i][j] == 'D')
{
ex = i;
ey = j;
}
else if (map[i][j] == 'X')
wall_count++;
}
}
//剩余的可走的地方必须大于时间(步数)T  不懂仔细想想就好
if ((n*m - wall_count) <= T)
{
std::cout << "NO" << std::endl;
continue;
}
flag = false;
map[sx][sy] = 'X';//将起点设为已走过的
dfs(sx, sy, 0);
if (flag)
std::cout << "YES" << std::endl;
else
std::cout << "NO" << std::endl;
}
return 0;
}
代码的注释应该是比较详细的,你们应该能看懂的,为啥呢,以因为我水平也不高,哈哈哈
以上的代码是可以AC的,希望评论一起交流
再说一些注意的地方,也就是我在做这一题的时候遇到问题,
1.用scanf输入地图的可能会有点问题,会WA;
2.要奇偶剪枝,不然会超时;
3.起点一定要设为走过的,不然会WA;
奇偶剪枝的知识点:https://baike.baidu.com/item/%E5%A5%87%E5%81%B6%E5%89%AA%E6%9E%9D/10385689?fr=aladdin
最后希望大家在杭电上有个愉快的收获吧!!!

你可能感兴趣的:(杭电)