M - Tempter of the Bone(DFS,奇偶剪枝)
M - Tempter of the Bone
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
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
'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
Sample Input
Sample Output
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std; char map[][];
bool vis[][];
int s_x,s_y,e_x,e_y;
int a,b,t,ok; bool check(int x,int y)
{
if (x<=||x>a||y<=||y>b)
return ;
if (map[x][y]=='X'||vis[x][y]||ok)
return ;
return ;
} void dfs(int x,int y,int time)
{
vis[x][y]=;
//printf("(%d,%d)\n",x,y);
if (x==e_x&&y==e_y&&time==t)//满足条件到终点
{
ok=;
return;
} int temp=t-time-(abs(x-e_x)+abs(y-e_y)); if (temp<)//剩余步数小于 0
return; int n_x,n_y; n_x=x,n_y=y+;
if (check(n_x,n_y))
{
dfs(n_x,n_y,time+);
vis[n_x][n_y]=;
} n_x=x+,n_y=y;
if (check(n_x,n_y))
{
dfs(n_x,n_y,time+);
vis[n_x][n_y]=;
} n_x=x,n_y=y-;
if (check(n_x,n_y))
{
dfs(n_x,n_y,time+);
vis[n_x][n_y]=;
} n_x=x-,n_y=y;
if (check(n_x,n_y))
{
dfs(n_x,n_y,time+);
vis[n_x][n_y]=;
}
} int main()
{
int i,j;
while (scanf("%d%d%d",&a,&b,&t)&&a+b+t)
{
getchar();
int wall=;
for (i=;i<=a;i++)
{
for (j=;j<=b;j++)
{
scanf("%c",&map[i][j]);
if (map[i][j]=='S')
{
s_x=i;
s_y=j;
}
if (map[i][j]=='D')
{
e_x=i;
e_y=j;
}
if (map[i][j]=='X')
wall++;
}
getchar();
} if (a*b-wall<=t)//所有路都走了还到不了终点,注意是 <=t ,可以少300ms
{
printf("NO\n");
continue;
} ok=;
memset(vis,,sizeof(vis));
int temp=t-(abs(s_x-e_x)+abs(s_y-e_y)); if (temp%==)//奇偶性相同才bfs
dfs(s_x,s_y,); if (ok)
printf("YES\n");
else
printf("NO\n");
}
return ;
}
最新文章
- ubuntu 报错: The system is running in low-graphics mode
- IOAPIC重定位中断处理函数思路整理
- BestCoder Round #90 A.Kblack loves flag(随机数生成种子)
- 利用CSS3 clip-path裁剪各种图形。
- kvm 克隆虚拟机
- Codeforces Round #136 (Div. 2)
- 跳转界面方法 (runtime实用篇一)
- 自己写的自动生成动态边框的jquery小插件
- 快速构建Windows 8风格应用15-ShareContract构建
- 转 windows 下 Oracle 导出表结构
- java 常用命令工具
- 浅谈一下Java String
- 浅谈surging服务引擎中的rabbitmq组件和容器化部署
- 使用Tornado异步接入第三方(支付宝)支付
- mac 远程连接 云服务器
- java ---->; 注解/反射
- js获取属性
- webstorm 安装与基本使用
- ThreeJS中的点击与交互——Raycaster的用法
- Go的基本类型与变量
热门文章
- vue 表单验证实例
- 【BIEE】12_查看BIEE的物理SQL
- sql server 循环
- html 5 中的 6位 十六进制颜色码 代表的意思180313
- event.returnvalue = false的使用
- 关于POI 中单元格背景色设置(转)
- centos 7 查看修改时区
- kafka快速开始教程
- Spring 注解 @Scheduled(cron = ";0 0/10 * * * ? ";) 任务调度动态改变时间
- Constant, random or timezone-dependent expressions in (sub)partitioning function are not allowed