Problem Description

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.

Input

For 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.

Output

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

SampleInput

4 4 3 10
N 1 1 1 1
W 1 1 3 2
W 2 1 2 4
4 4 3 10
N 1 1 1 1
W 1 1 3 2
W 1 1 2 4

SampleOutput

9
Bad luck! 题意就是要你从(0,0)跑到(n,m),只能在d时间内跑到,并且在图中有k个炮台,炮台只能往一个方向(W,E,N,S)发送子弹,子弹发射间隔为t,速度为v。 人不能跑进炮台,炮台会挡住另一炮台的子弹,当且仅当人到达某个点且子弹也到达当点才算被射中,人可以不动。 题意清楚了就是BFS搜吧,但是同样标记数组得开一个三维数组标记坐标和时间,因为每个点可能不止走一次,所以这道题唯一的难度就是判断是否被射杀。其实不是很难,当你处于某个位置时,往四个方向找炮台判断是否会被杀死就行了。
代码:

 #include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <sstream>
#include <iomanip>
#include <map>
#include <stack>
#include <deque>
#include <queue>
#include <vector>
#include <set>
#include <list>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <iterator>
#include <cmath>
#include <bitset>
#include <ctime>
#include <fstream>
#include <limits.h>
#include <numeric> using namespace std; #define F first
#define S second
#define mian main
#define ture true #define MAXN 1000000+5
#define MOD 1000000007
#define PI (acos(-1.0))
#define EPS 1e-6
#define MMT(s) memset(s, 0, sizeof s)
typedef unsigned long long ull;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef stringstream sstm;
const int INF = 0x3f3f3f3f; int m,n,k,d;
bool mp[][];
bool vis[][][]; int fx[][]={{,},{-,},{,},{,-},{,}}; //可以不动 struct paotai{ //记录炮台数据
char d;
int t,v;
}cas[][]; struct node{
int x,y;
int step;
node(int _x,int _y,int _step):x(_x),y(_y),step(_step){}
}; bool judge(int x,int y,int time){ //往四个方向找炮台 for(int i = x-; i >= ; i--){
if(mp[i][y]){
if(cas[i][y].d == 'S' && time >= db(x-i) / cas[i][y].v){
double c = db(x-i) / cas[i][y].v;
if(time == c)
return false;
while(time >= c){
if(time == c)
return false;
c += cas[i][y].t;
}
}
break; //只要找最近的就行了
}
} for(int i = x+; i <= m; i++){
if(mp[i][y]){
if(cas[i][y].d == 'N' && time >= db(i-x) / cas[i][y].v){
double c = db(i-x) / cas[i][y].v;
if(time == c)
return false;
while(time >= c){
if(time == c)
return false;
c += cas[i][y].t;
}
}
break;
}
} for(int i = y-; i >= ; i--){
if(mp[x][i]){
if(cas[x][i].d == 'E' && time >= db(y-i) / cas[x][i].v){
double c = db(y-i) / cas[x][i].v;
if(time == c)
return false;
while(time >= c){
if(time == c)
return false;
c += cas[x][i].t;
}
}
break;
}
} for(int i = y+; i <= n; i++){
if(mp[x][i]){
if(cas[x][i].d == 'W' && time >= db(i-y) / cas[x][i].v){
double c = db(i-y) / cas[x][i].v;
if(time == c)
return false;
while(time >= c){
if(time == c)
return false;
c += cas[x][i].t;
}
}
break;
}
}
return true;
} bool check(int x,int y,int time){
if(x < || x > m || y < || y > n || time > d || mp[x][y]){
return false;
}
if(judge(x,y,time)){
return true;
}
return false;
} void bfs(){
MMT(vis); queue<node> s;
s.push(node(,,));
vis[][][] = ;
while(!s.empty()){
node cnt = s.front();
s.pop();
if(cnt.step > d){
cout << "Bad luck!" << endl;
return ;
}
if(cnt.x == m && cnt.y == n && cnt.step <= d){
cout << cnt.step << endl;
return;
}
for(int i = ; i < ; i++){
int x = cnt.x + fx[i][];
int y = cnt.y + fx[i][];
int time = cnt.step + ;
if(check(x,y,time) && !vis[x][y][time]){
vis[x][y][time] = ;
s.push(node(x,y,time));
}
}
}
cout << "Bad luck!" << endl;
} int main(){
ios_base::sync_with_stdio(false);
cout.tie();
cin.tie();
char a;
int b,c,x,y;
while(cin>>m>>n>>k>>d){
MMT(mp);
for(int i = ; i < k; i++){
cin>>a>>b>>c>>x>>y;
cas[x][y].d = a, cas[x][y].t = b,cas[x][y].v = c;
mp[x][y] = ;
}
bfs();
}
}


Escape (BFS + 模拟)的更多相关文章

  1. BFS+模拟 ZOJ 3865 Superbot

    题目传送门 /* BFS+模拟:dp[i][j][p] 表示走到i,j,方向为p的步数为多少: BFS分4种情况入队,最后在终点4个方向寻找最小值:) */ #include <cstdio&g ...

  2. hdu_1495_非常可乐(bfs模拟)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1495 题意:不解释 题解:BFS模拟,不过要细心,把所有情况都列举出来,开一个数组记录状态,代码有点长 ...

  3. Hdu 5336 XYZ and Drops (bfs 模拟)

    题目链接: Hdu 5336 XYZ and Drops 题目描述: 有一个n*m的格子矩阵,在一些小格子里面可能会有一些水珠,每个小水珠都有一个size.现在呢,游戏开始咯,在一个指定的空的小格子里 ...

  4. hdu 2364 Escape【模拟优先队列】【bfs】

    题目链接:https://vjudge.net/contest/184966#problem/A 题目大意: 走迷宫.从某个方向进入某点,优先走左或是右.如果左右都走不通,再考虑向前.绝对不能往后走, ...

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

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

  6. HDU 3533 Escape (BFS + 预处理)

    Escape Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  7. POJ 3414 Pots【bfs模拟倒水问题】

    链接: http://poj.org/problem?id=3414 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22009#probl ...

  8. HDU 3533 Escape(bfs)

    Escape Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

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

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

随机推荐

  1. c# NPOI 导出23万条记录耗时12秒

    先上测试代码: string connectionString = "Server=localhost;Initial Catalog=******;User ID=sa;Password= ...

  2. Kafka 原理和实战

    本文首发于 vivo互联网技术 微信公众号 https://mp.weixin.qq.com/s/bV8AhqAjQp4a_iXRfobkCQ作者简介:郑志彬,毕业于华南理工大学计算机科学与技术(双语 ...

  3. spark通过JDBC读取外部数据库,过滤数据

    官网链接: http://spark.apache.org/docs/latest/sql-programming-guide.html#jdbc-to-other-databases http:// ...

  4. 卷积神经网络cnn的实现

    卷积神经网络 代码:https://github.com/TimVerion/cat 卷积层 卷积层:通过在原始图像上平移来提取特征,每一个特征就是一个特征映射 原理:基于人脑的图片识别过程,我们可以 ...

  5. peewee

    字段查看http://docs.peewee-orm.com/en/latest/peewee/models.html#fields 方法使用https://blog.csdn.net/qq_3962 ...

  6. 用代码说话:synchronized关键字和多线程访问同步方法的7种情况

    synchronized关键字在多线程并发编程中一直是元老级角色的存在,是学习并发编程中必须面对的坎,也是走向Java高级开发的必经之路. 一.synchronized性质 synchronized是 ...

  7. Java反射使用总结

    最近公司招了几名刚毕业的大学生,在给他们培训的过程中,讲到反射,他们有些人听不懂,对反射的概念云里雾里的,不知道反射有什么用. 因此就有了本文的诞生. 反射是java提供的一个重要功能,可以在运行时检 ...

  8. Codeforces 255C

    题意略. 本题考查动态规划,顺便考查一下优化. 这个题目可以归约到最长递增子序列那一类,定义状态:dp[i][j] --- 当前以第i个数结尾,前一个数是第j个数的最长序列. if(a[i] == a ...

  9. (五十三)c#Winform自定义控件-滚动文字

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  10. dotnet core各rpc组件的性能测试

    一般rpc通讯组件都具有高性特性,因为大部分rpc都是基于二进制和连接复用的特点,相对于HTTP(2.0以下的版本)来说有着很大的性能优势,非常适合服务间通讯交互.本文针对了dotnet core平台 ...