zoj 2110 Tempter of the Bone

// zoj 2110

#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;

char map[9][9]; //迷宫地图
int n,m,t; //迷宫的大小,及迷宫的门会在第t秒开启
int di,dj; //(di,dj):门的位置
bool escape;
int dir[4][2]={{0,-1},{0,1},{1,0},{-1,0}};//控制行走方向 左 右 下 上
void dfs(int si,int sj,int cnt) //已经到达(si, sj)位置 且已经花费cnt秒

{
int i,temp;
if (si>n||sj>m||si<=0||sj<=0) return; //起点不再矩阵之内。返回 //边界
if (cnt==t&&si==di&&sj==dj) escape=1;// 起点在门处,且门在零秒的时候开启,成功逃生
if (escape) return;

temp=(t-cnt)-abs(si-di)-abs(sj-dj);//
if (temp<0||temp&1) return; //temp<0是最短距离小于时间的剪枝,temp&1是奇偶剪枝,还有一种写法,temp%2!=0;
for (i=0;i<4;i++)
{
if (map[si+dir[i][0]][sj+dir[i][1]]!=‘X‘)//如果该方格是可以通过的,跳到此空格进行操作
{
map[si+dir[i][0]][sj+dir[i][1]]=‘X‘;//设置志。变为不可通过,表明已经搜过
dfs(si+dir[i][0],sj+dir[i][1],cnt+1); //从此点开始递归
map[si+dir[i][0]][sj+dir[i][1]]=‘.‘; //递归完,将该点还原
}
}
return;
}

int main()
{
int i,j ; //循环变量
int si,sj; //小狗的起始位置
while (cin>>n>>m>>t)
{
if (n==0&&m==0&&t==0) break;//测试数据结束
int wall=0;
for (i=1;i<=n;i++)
for (j=1;j<=m;j++)
{
cin>>map[i][j];
if (map[i][j]==‘S‘)
{
si=i; //记录起点位置
sj=j;
}
else if (map[i][j]==‘D‘)
{
di=i; //记录门位置
dj=j;
}
else if (map[i][j]==‘X‘) wall++; //wall 为不能穿越的方格的个数
}
if (n*m-wall<=t) //可以走的个数少于等于时间,说明即使全部走,也不能刚好在t时间到达门
{
cout<<"NO"<<endl;
continue;
}
escape=0; //逃跑成功标志位,成功了为1,不成功为0
map[si][sj]=‘X‘;
dfs(si,sj,0); //从起点出开始深搜,此时时间为零
if (escape) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}

//2110
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<cstdlib>
using namespace std;
int n, m, t, di, dj, flag;
char map[10][10];
int dir[4][2]={{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
void dfs(int si, int sj, int cnt)
{
int i, xx, yy;
if(si<=0||sj<=0||si>n||sj>m) return ;
if(si==di&&sj==dj&&cnt==t)
{
flag=1;
return ;
}
int temp=t-cnt-abs(si-di)-abs(sj-dj);
if(temp<0 || temp%2)
return ;
for(i=0; i<4; i++)
{
xx=si+dir[i][0], yy=sj+dir[i][1];
if(map[xx][yy]!=‘X‘)
{
map[xx][yy]=‘X‘;
dfs(xx, yy, cnt+1);
if(flag) return;
map[xx][yy]=‘.‘;
}
}
return ;
}
int main()
{
int i, j, si, sj;
while(scanf("%d%d%d", &n, &m, &t)!=EOF)
{
if(n==0&&m==0&&t==0) break;
memset(map, 0, sizeof(map));
int wal=0;
char temp;
scanf("%c", &temp);
for(i=1; i<=n; i++)
{
//scanf("%s%*c", map[i]);
for(j=1; j<=m; j++)
{
scanf("%c", &map[i][j]);
if(map[i][j]==‘S‘)
si=i, sj=j;
else if(map[i][j]==‘D‘)
di=i, dj=j;
else if(map[i][j]==‘X‘)
wal++;
}
scanf("%c", &temp);
}
if(n*m-wal<=t)
{
printf("NO\n");
continue;
}
flag=0;
map[si][sj]=‘X‘;
dfs(si, sj, 0);
if(flag)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}

****************************************
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<cstdlib>
using namespace std;
int n, m, t, dx, dy, flag;
char map[10][10];
int dir[4][2]={{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
void dfs(int x, int y, int step)
{
int i, xx, yy;
if(x<=0||y<=0||x>n||y>m) return ;
if(x==dx&&y==dy&&step==t)
{
flag=1;
return ;
}
int temp=t-step-abs(x-dx)-abs(y-dy);
if(temp<0 || temp%2)
return ;
for(i=0; i<4; i++)
{
xx=x+dir[i][0], yy=y+dir[i][1];
if(map[xx][yy]!=‘X‘)
{
map[xx][yy]=‘X‘;
dfs(xx, yy, step+1);
if(flag) return;
map[xx][yy]=‘.‘;
}
}
return ;
}
int main()
{
int i, j, x, y;
while(scanf("%d%d%d", &n, &m, &t)!=EOF)
{
if(n==0&&m==0&&t==0) break;
memset(map, 0, sizeof(map));
int wal=0;
char temp;
scanf("%c", &temp);
for(i=1; i<=n; i++)
{
//scanf("%s%*c", map[i]);
for(j=1; j<=m; j++)
{
scanf("%c", &map[i][j]);
if(map[i][j]==‘S‘)
x=i, y=j;
else if(map[i][j]==‘D‘)
dx=i, dy=j;
else if(map[i][j]==‘X‘)
wal++;
}
scanf("%c", &temp);
}
if(n*m-wal<=t)
{
printf("NO\n");
continue;
}
flag=0;
map[x][y]=‘X‘;
dfs(x, y, 0);
if(flag)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}

zoj 2110 Tempter of the Bone,布布扣,bubuko.com

时间: 2024-10-06 01:50:03

zoj 2110 Tempter of the Bone的相关文章

ZOJ 2110 Tempter of the Bone(条件迷宫DFS,HDU1010)

题意  一仅仅狗要逃离迷宫  能够往上下左右4个方向走  每走一步耗时1s  每一个格子仅仅能走一次且迷宫的门仅仅在t时刻打开一次  问狗是否有可能逃离这个迷宫 直接DFS  直道找到满足条件的路径  或者走全然部可能路径都不满足 注意剪枝  当前位置为(r,c)  终点为(ex,ey) 剩下的时间为lt  当前点到终点的直接距离为  d=(ex-r)+(ey-c)   若多走的时间rt=lt-d<0 或为奇数时  肯定是不可能的  能够自己在纸上画一下 每一个点仅仅能走一次的图  走弯路的话多

HDU 1010 &amp;&amp; ZOJ 2110 Tempter of the bone (DFS + 奇偶剪枝)

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 desp

Tempter of the Bone

Tempter of the Bone Time Limit: 1000ms                                            Memory Limit: 32768KB 64-bit integer IO format:      Java class name: The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it

Tempter of the Bone(dfs奇偶剪枝)

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 92175    Accepted Submission(s): 25051 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

HDU - 1010 Tempter of the Bone 深搜模板题(DPS)解题报告

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 88587    Accepted Submission(s): 24116 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

【深搜加剪枝五】HDU 1010 Tempter of the Bone

Tempter of the BoneTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 64326    Accepted Submission(s): 17567 Problem Description The doggie found a bone in an ancient maze, which fascinated him a l

M - Tempter of the Bone (没有代码)

M - Tempter of the Bone Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u 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 d

HDU1010 Tempter of the Bone 【DFS】+【剪枝】

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 70895    Accepted Submission(s): 19535 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

HDU 1010 Tempter of the Bone heuristic 剪枝法

本题就是考剪枝法了. 应该说是比较高级的应用了.因为要使用heuristic(经验)剪枝法.要总结出这个经验规律来,不容易.我说这是高级的应用也因为网上太多解题报告都没有分析好这题,给出的程序也很慢,仅仅能过掉,由此看来很多人没有做好这道题. 这里我需要更正一下网上流行的说法:奇偶剪枝法. 其实本题使用奇偶剪枝法并不能太大提高速度,只能说仅仅让使用奇偶剪枝过掉.所以网上说本题使用奇偶剪枝的,其实并不能提高速度. 原因: 奇偶剪枝只能剪枝一次,不能在递归的时候剪枝,因为只要初始化位置符合奇偶性,那