The students of the HEU are maneuvering for their military training.
The red army and the blue army are at war today. The blue army
finds that Little A is the spy of the red army, so Little A has to
escape from the headquarters of the blue army to that of the red army.
The battle field is a rectangle of size m*n, and the headquarters of the
blue army and the red army are placed at (0, 0) and (m, n),
respectively, which means that Little A will go from (0, 0) to (m, n).
The picture below denotes the shape of the battle field and the notation
of directions that we will use later.

The blue army is eager to revenge, so it tries its best to kill
Little A during his escape. The blue army places many castles, which
will shoot to a fixed direction periodically. It costs Little A one unit
of energy per second, whether he moves or not. If he uses up all his
energy or gets shot at sometime, then he fails. Little A can move north,
south, east or west, one unit per second. Note he may stay at times in
order not to be shot.

To simplify the problem, let’s assume that Little A cannot stop in
the middle of a second. He will neither get shot nor block the bullet
during his move, which means that a bullet can only kill Little A at
positions with integer coordinates. Consider the example below. The
bullet moves from (0, 3) to (0, 0) at the speed of 3 units per second,
and Little A moves from (0, 0) to (0, 1) at the speed of 1 unit per
second. Then Little A is not killed. But if the bullet moves 2 units per
second in the above example, Little A will be killed at (0, 1).

Now, please tell Little A whether he can escape.

InputFor every test case, the first line has
four integers, m, n, k and d (2<=m, n<=100, 0<=k<=100, m+
n<=d<=1000). m and n are the size of the battle ground, k is the
number of castles and d is the units of energy Little A initially has.
The next k lines describe the castles each. Each line contains a
character c and four integers, t, v, x and y. Here c is ‘N’, ‘S’, ‘E’ or
‘W’ giving the direction to which the castle shoots, t is the period, v
is the velocity of the bullets shot (i.e. units passed per second), and
(x, y) is the location of the castle. Here we suppose that if a castle
is shot by other castles, it will block others’ shots but will NOT be
destroyed. And two bullets will pass each other without affecting their
directions and velocities.

All castles begin to shoot when Little A starts to escape.

Proceed to the end of file.

OutputIf Little A can escape, print the minimum time required in seconds on a single line. Otherwise print “Bad luck!” without quotes.

#include<stdio.h>
#include<iostream>
#include<queue>
#include<string.h>
using namespace std; struct man{
short x,y,t;
}; int n,m,k,energy;
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
bool tmap[101][101][1001];//前两维代表坐标,后一维表时时间:意为每个位置的每个时刻有没子弹和碉堡(这里一定要用bool,用int会超内存)
bool vist[101][101][1001];//第几个时刻的位置走过否 int OK(int x,int y,int time)//返回1说明位位置的当前时间可以走,加入队列
{
if(x>=0&&x<=n&&y>=0&&y<=m&&!tmap[x][y][time]&&n-x+m-y<=energy-time&&!vist[x][y][time])
{
vist[x][y][time]=true; return 1;
}
return 0;
}
void bfs()
{
queue<man>qMan;
man pMan,tpMan;
int time=0,x,y; pMan.x=0; pMan.y=0; pMan.t=0;
if(OK(pMan.x,pMan.y,time))
qMan.push(pMan); while(true)
{
time++;
if(qMan.empty()||time>energy)
{
printf("Bad luck!\n"); return ;
}
while(!qMan.empty())
{
pMan=qMan.front();
if(pMan.t>=time)
break;
qMan.pop();
pMan.t=time;
if(OK(pMan.x,pMan.y,time))//可以停在原地
qMan.push(pMan);
for(int e=0;e<4;e++)
{
tpMan=pMan;
tpMan.x+=dir[e][0]; tpMan.y+=dir[e][1];
if(OK(tpMan.x,tpMan.y,time))
{
if(tpMan.x==n&&tpMan.y==m)
{
printf("%d\n",time); return ;
}
qMan.push(tpMan);
}
}
}
}
}
int main()
{
char ss[3];
int map[105][105];
int tx,ty,x[105],y[105],T[105],v[105],d[105];
while(scanf("%d%d%d%d",&n,&m,&k,&energy)>0)
{
for(int i=0;i<=n;i++)
for(int j=0;j<=m;j++)
for(int t=0;t<=energy;t++)
tmap[i][j][t]=vist[i][j][t]=false;
memset(map,0,sizeof(map)); for(int i=0;i<k; i++)
{
scanf("%s%d%d%d%d",ss,&T[i],&v[i],&x[i],&y[i]);
if(ss[0]=='N') d[i]=0;
else if(ss[0]=='S') d[i]=1;
else if(ss[0]=='W') d[i]=2;
else if(ss[0]=='E') d[i]=3;
map[x[i]][y[i]]=1;//碉堡位置
}
//计算每个时刻子弹和碉堡在地图的位置
for(int i=0;i<k; i++)
{
for(int t=1;t<=energy; t++)//一开始当前碉堡发出的子弹的每个时间运行
{
tmap[x[i]][y[i]][t]=true;//每个刻碉堡的位置
//第i个碉堡一开始发的子弹运行了t时到达的位置
tx=x[i]+dir[d[i]][0]*v[i]*t;
ty=y[i]+dir[d[i]][1]*v[i]*t; if(tx>=0&&tx<=n&&ty>=0&&ty<=m)//是否越界
for(int tt=t; tt<=energy; tt+=T[i])//第i个碉堡发出的子弹周期性的到达同一位置
if(!tmap[tx][ty][tt])
{
//判断从第i个碉堡位置发出的子弹能否到达位置(tx,ty)
int flag=0,xx=x[i],yy=y[i];
if(d[i]==0)
{
for( xx=x[i]-1; xx>=tx; xx--)
if(map[xx][yy])
break;
if(xx<tx) flag=1;
}
else if(d[i]==1)
{
for( xx=x[i]+1; xx<=tx; xx++)
if(map[xx][yy])
break;
if(xx>tx) flag=1;
}
else if(d[i]==2)
{
for(yy=y[i]-1; yy>=ty; yy--)
if(map[xx][yy])
break;
if(yy<ty) flag=1;
}
else
{
for(yy=y[i]+1; yy<=ty; yy++)
if(map[xx][yy])
break;
if(yy>ty) flag=1;
} if(flag)//能到达,说明路过的地没有其他碉堡阻挡
tmap[tx][ty][tt]=true;
}
}
}
bfs();
}
}

【算法】Escape的更多相关文章

  1. 【算法系列学习】[kuangbin带你飞]专题二 搜索进阶 D - Escape (BFS)

    Escape 参考:http://blog.csdn.net/libin56842/article/details/41909459 [题意]: 一个人从(0,0)跑到(n,m),只有k点能量,一秒消 ...

  2. HDU3533 Escape —— BFS / A*算法 + 预处理

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3533 Escape Time Limit: 20000/10000 MS (Java/Others)  ...

  3. hdu 3605 Escape 二分图的多重匹配(匈牙利算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3605 Escape Time Limit: 4000/2000 MS (Java/Others)    ...

  4. CYQ.Data V5 分布式缓存Redis应用开发及实现算法原理介绍

    前言: 自从CYQ.Data框架出了数据库读写分离.分布式缓存MemCache.自动缓存等大功能之后,就进入了频繁的细节打磨优化阶段. 从以下的更新列表就可以看出来了,3个月更新了100条次功能: 3 ...

  5. [ACM训练] 算法初级 之 搜索算法 之 深度优先算法DFS (POJ 2251+2488+3083+3009+1321)

    对于深度优先算法,第一个直观的想法是只要是要求输出最短情况的详细步骤的题目基本上都要使用深度优先来解决.比较常见的题目类型比如寻路等,可以结合相关的经典算法进行分析. 常用步骤: 第一道题目:Dung ...

  6. Memcache技术分享:介绍、使用、存储、算法、优化、命中率

    1.memcached 介绍 1.1 memcached 是什么? memcached 是以LiveJournal旗下Danga Interactive 公司的Brad Fitzpatric 为首开发 ...

  7. 面试题(C#算法编程题)

    1>用C#写一段选择排序算法,要求用自己的编程风格.答:private int min;    public void xuanZhe(int[] list)//选择排序    {        ...

  8. C#常见算法题目(面试准备)

    1.写出冒泡,选择,插入排序算法.     //冒泡排序    public class bubblesorter    {        public void sort(int[] list)   ...

  9. 物体检测算法 SSD 的训练和测试

    物体检测算法 SSD 的训练和测试 GitHub:https://github.com/stoneyang/caffe_ssd Paper: https://arxiv.org/abs/1512.02 ...

随机推荐

  1. (django1.10)访问url报错Forbidden (CSRF cookie not set.): xxx

    问题:页面访问时报错 Forbidden (CSRF cookie not set.): xxx     解决方法: 修改settings.py文件,注释掉 django.middleware.csr ...

  2. php递归获取目录下所有文件

    <?php function getFileList($dir){ $dir=iconv("utf-8","gb2312",$dir); if ($hea ...

  3. empty 与 remove 的区别

    empty()移除指定元素中的所有子节点,拿$("p").empty()来说,他只是把<p>dsfsd</p>中的文本给移除了,而留下 了<p> ...

  4. 牛客网练习赛43-C(图论)

    题目链接:https://ac.nowcoder.com/acm/contest/548/C 题意:有n个知识点,学会每个知识点花T[i],已经学会了其中k个知识点,有m组关系,t1,t2,t3,表示 ...

  5. 第九章 词典 (e)桶/计数排序

  6. linux下的ping工具--fping

    前言:       如果想ping 大量的不连续的地址,有想知道放回结果,在linux系统下有一个非常合适的工具--fping. Fping 官网:http://www.fping.org/ Fpin ...

  7. POJ-2386.Lakecounting(DFS求连通块)

    本题是一道连通块的入门题,用来练手,后续还会更新连通块的题目. 本题大意:一个n * m 的陆地上面有很多水洼,让你统计水洼的个数并输出. 本题思路:按照顺序遍历陆地,如果发现水洼就将它的八连块都进行 ...

  8. oracle中的to_number在mysql中的转换

    select cast(11 as unsigned int) /*整型*/ select cast(11 as decimal(10,2)) /*浮点型*/ 注:(10,2)代表数字共十位,小数点后 ...

  9. synchronized细节问题(一)

    synchronized锁重入: 关键字synchronized拥有锁重入的功能,也就是在使用synchronized时,当一个线程得到一个对象的锁后,再次请求此对象时是可以再次得到该对象的锁. 下面 ...

  10. jsp页面\n换行替换

    1,js替换 var text= $(this).text(); var reg = new RegExp("\\n","g");//g,表示全部替换. tex ...