Diablo(广搜)

Diablo
Time Limit: 1000 MS Memory Limit: 65536 K
Total Submit: 43(22 users) Total Accepted: 24(21 users) Rating: Special Judge: No
Description

Diablo是地狱中的三大魔王之一,有着非常强大的破坏力,Diablo期望着可以将一切都投入到地狱之中。为了不让Diablo的计划得逞,一位英雄决定挺身而出,试图击败Diablo。DIablo喜欢把眼前的区域变成一片火海,Diablo吐出火焰的蔓延是有一定规律的,火焰总是会向能量较低的区域蔓延。英雄如果不想被Diablo击败的话,就需要在Diablo喷出火焰的一瞬间逃到火焰烧不到的地方,但是他只能在这一瞬逃出3个单位长度的距离。现在我们给出现场的一个地图,并且给出Diablo和英雄所在的位置,我们要知道英雄是否可以逃脱Diablo的攻击。(火焰蔓延方向只能是上、下、左、右)

Input
本题有多组测试数据,对于每组数据第一行输入6个非负整数,分别表示地图的长、宽(分别对应m、n),Diablo的坐标以及英雄的坐标。接下来n行每行输入m个数,表示地图上每个点的能量值(值在32位有符号整数范围内)。m、n值均不大于100,且我们保证数据合理合法。
Output
如果英雄可以逃脱Diablo的攻击,输出Hero will be back并换行,否则输出Diablo win并换行。
Sample Input

3 3 0 0 2 2 5 4 1 4 3 2 3 2 1

6 4 0 0 2 3 5 2 6 7 3 0 4 3 8 1 5 5 9 1 8 3 6 5 8 7 6 5 4 2

Sample Output

Diablo win

Hero will be back

Hint
我们来看看第二组样例数据,Diablo所处的位置是(0, 0),那么Diablo在地图上左上角的点,其能量值为5。英雄所在的位置是(2, 3),也就是我们看到地图上第三行第四列的位置,其能量值为3。不过火焰只能从能量高的地方向能量低的地方蔓延,尽管英雄处在的位置能量值较低,但是火焰却无法蔓延到这里,因为有能量相对较高的地势阻隔。假设英雄所在的位置能被火焰蔓延到,他只需在3步内找到一个火焰不可达的合法区域即可躲过攻击。
Source
2014 Winter Holiday Contest 5
Author
杨和禹

这个广搜的时候、应该注意,即便是已经遍历过的点,也不应该阻止下次·对这个点的遍历,因为只要被监测点的周围任意一个店的能量值比此点高,那么此点就应该被火焰蔓延

代码:

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
int m,n,x_1,y_1,x_2,y_2;
typedef long long LONG;
LONG a[101][101];
int direct[4][2]={0,1,-1,0,0,-1,1,0};
int visit[100][100];
struct node
{
int x,y;
LONG e;
} p[10001],p1,p2;
int bfs()
{
int k=0;
int i,front=0,rear=0;
p[0]=p1;
memset(visit,0,sizeof(visit));
visit[x_1][y_1]=2;
while(rear<=front)
{
p2=p[rear++];
int e1=p2.e;
for(i=0;i<4;i++)
{
int x1=p2.x+direct[i][0];
int y1=p2.y+direct[i][1];
if(x1>=0&&x1<n&&y1>=0&&y1<m&&visit[x1][y1]!=2)
{
if(a[x1][y1]<e1)
{
p1.x=x1;
p1.y=y1;
p1.e=a[x1][y1];
p[++front]=p1;
visit[x1][y1]=2;
if(x1==x_2&&y1==y_2)
{
k=1;
}
}
else
{
visit[x1][y1]=1;
}
}
}
}
return k;

}
int check()
{
int xh,yh,i,j;
for(i=0;i<4;i++)
{
xh=x_2;
yh=y_2;
for(j=0;j<3;j++)
{
xh+=direct[i][0];
yh+=direct[i][1];
if(xh>=0&&xh<n&&yh>=0&&yh<m)
{
// cout<<visit[xh][yh];
if(visit[xh][yh]!=2)
{
return 1;
}
}
}
// cout<<endl;
}
return 0;
}
int main()
{
while(~scanf("%d%d%d%d%d%d",&m,&n,&x_1,&y_1,&x_2,&y_2))
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%lld",&a[i][j]);
}
}

p1.x=x_1;
p1.y=y_1;
p1.e=a[x_1][y_1];
/* i=bfs();
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%ld ",visit[i][j]);
}
cout<<endl;
}*/
i=bfs();
if(i==0)
{
printf("Hero will be back\n");
}
else
{
if(check()==1)
{
printf("Hero will be back\n");
}
else
{
printf("Diablo win\n");
}
}
}
return 0;
}

时间: 2024-08-28 12:00:52

Diablo(广搜)的相关文章

NYOJ 284 坦克大战 &amp;&amp; POJ 2312 Battle City (广搜+优先队列)

链接:click here~~ 题意: 描述 Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are discussing is a simple edition of this game. Given a map that consists of empty space

Catch That Cow(广搜)

个人心得:其实有关搜素或者地图啥的都可以用广搜,但要注意标志物不然会变得很复杂,想这题,忘记了标志,结果内存超时: 将每个动作扔入队列,但要注意如何更简便,更节省时间,空间 Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and t

codevs 1225:八数码难题【双向广搜】

这里是传送门 这道题用普通BFS是可以做的,但是很明显没得过,效率太低了.效率更高的算法A*和双向广搜都可取,这写一下双向广搜的. 注意题目中的判重很重要,可以转化成九位数用hash来解决这个问题. #include <set> #include <string> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define

迷宫广搜

上学期学了C,这学期学C++.感觉最难的还是算法,上周作业的一道广搜题是我第一次接触广搜,由于第一学期刚学编程就接触的太多的算法难题,不禁对代码产生畏惧,不过还好没有放弃,虽然算法很难,但我慢慢找到了一点学数学时的乐趣.先介绍一下这道未来的我看过来会觉得很简单一道题吧 You are provided a maze(迷宫), and you need to program to find the least steps to walk from the start to the end.And

宽搜和广搜、

广搜与深搜的小区别 一般来说,广搜常用于找单一的最短路线,或者是规模小的路径搜索,它的特点是"搜到就是最优解", 而深搜用于找多个解或者是"步数已知(好比3步就必需达到前提)"的标题,它的空间效率高,然则找到的不必定是最优解,必需记实并完成全数搜索,故一般情况下,深搜需要很是高效的剪枝(优化). 像搜索最短路径这些的很显著若是用广搜,因为广搜的特征就是一层一层往下搜的,保证当前搜到的都是最优解,当然,最短路径只是一方面的操作,像什么起码状态转换也是可以操作的.深搜就

hdu1241详解 Java广搜搞定

import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int m = sc.nextInt();//输入地图的行数 int n = sc.nextInt();//输入地图的列数 if (m == 0) {//若m=0则退出程序 break; } // 初始化图

poj 1166 The Clocks 记录路径的广搜

题意: 给9个时钟的初始状态,和一些对某几个钟的操作,求最少经过几步能到目标状态(全指向12点). 分析: 明显的广搜,但实现起来的细节要注意:1.因为要记录路径,所以要在整个程序执行过程中扩展出的节点在输出路径前不能销毁, 故采用静态内存分配的方法(开node[600000],用get_node()创建节点.2.queue<node>比queue<int>要多花1别的时间. //poj 1166 //sep9 #include <iostream> #include

nyoj 523 双向广搜

题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=523 #include<iostream> #include<cstdio> #include<queue> using namespace std; /* 用普通搜索TLE,已知起点和终点,可以考虑双向广搜或A*算法加速搜索 双向广搜,一个方向从出发点向终点搜索,一个方向从终点向出发点搜索,搜索到相同的结点时,即找到最短路径. */ const int N

hdu 2717 Catch That Cow(广搜bfs)

题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7909    Accepted Submission(s): 2498 Problem Description Farmer John has been inform