Tempter of the Bone

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

Total Submission(s): 146511    Accepted Submission(s): 39059

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

题意

给出一个N*M的矩阵和一个时间T,问能不能在时间恰好为T的时候从S走到D。X不能走'.'可以走,每次走过之后'.'就会消失。

AC代码

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#define ll long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x3f3f3f3f
const double E=exp(1);
const int maxn=1e2+10;
using namespace std;
char ch[maxn][maxn];
int vis[maxn][maxn];
int ans;
int xx,yy;
int T,n,m;
int flag;
int dir[4][2]={1,0,-1,0,0,1,0,-1};
void dfs(int x,int y,int t)
{
vis[x][y]=1;
//如果正好在T时刻走到D
if(t==T&&ch[x][y]=='D')
{
flag++;
return ;
}
//奇偶剪枝,如果相差的时间和相差的曼哈顿距离的奇偶性不同,则一定无法到达
//或者相差的时间小于曼达顿距离也不行
//PS:不剪枝会超时
int res=T-t-abs(xx-x)-abs(yy-y);
if(res<0||res%2)
return ;
for(int i=0;i<4;i++)
{
int dx=x+dir[i][0];
int dy=y+dir[i][1];
if(dx>=0&&dx<n&&dy>=0&&dy<m&&ch[dx][dy]!='X'&&vis[dx][dy]==0)
{
dfs(dx,dy,t+1);
//如果能够走到D,就可以结束了,不需要回溯
if(flag)
return ;
// 回溯
vis[dx][dy]=0;
}
}
}
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
while(cin>>n>>m>>T&&T&&n&&m)
{
ms(vis);
flag=0;
ans=0;
int x,y;
for(int i=0;i<n;i++)
cin>>ch[i];
// 记录开始和结束的位置
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
if(ch[i][j]=='S')
{x=i;y=j;}
if(ch[i][j]=='D')
{xx=i;yy=j;}
} dfs(x,y,0);
if(flag)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}

最新文章

  1. django一些操作命令
  2. 使用gdb调试Python进程
  3. iOS - UIProgressView
  4. 2016 - 1 - 3 国旗选择demo
  5. 【转载】alter table move 和 alter table shrink space的区别
  6. 第二百八十一、二、三天 how can I 坚持
  7. NOI题库1159 Maze
  8. ssh框架整合之登录以及增删改查
  9. 原生js+css实现重力模拟弹跳系统的登录页面
  10. [DeeplearningAI笔记]改善深层神经网络_深度学习的实用层面1.10_1.12/梯度消失/梯度爆炸/权重初始化
  11. 3.3 声明[[],]的数组, push_back() 和 back() 的区别
  12. Scala的类层级讲解
  13. [Swift]LeetCode926. 将字符串翻转到单调递增 | Flip String to Monotone Increasing
  14. Java多线程学习(总结很详细!!!)
  15. dp的斜率优化
  16. OpenCV——Brisk特征检测、匹配与对象查找
  17. 关于dubbo和zookeeper 注册中心
  18. 在触屏设备上面利用html5裁剪图片
  19. “全栈2019”Java多线程第一章:认识多线程
  20. python3 安装win32api

热门文章

  1. android开发:Android 中自定义View的应用
  2. Codeforces 556D - Case of Fugitive
  3. Confluence 6 从 Crowd 或 JIRA 应用中切换回使用内部用户管理
  4. 『Collections』namedtuple_具名元组
  5. hdu-4678-sg
  6. Oracle性能诊断艺术-读书笔记(脚本execution_plans截图)
  7. adb命令(笔记)
  8. linux physical and virtual addressing modes
  9. Oracle 固定执行计划-使用SPM(Sql Plan Management)固定执行计划
  10. 去除 DBGridEh SelectedRows里无效的书签