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. ZooKeeper实现同步屏障(Barrier)

    按照维基百科的解释:同步屏障(Barrier)是并行计算中的一种同步方法.对于一群进程或线程,程序中的一个同步屏障意味着任何线程/进程执行到此后必须等待,直到所有线程/进程都到达此点才可继续执行下文. ...

  2. linux下实现并发逻辑

    ################shell 模拟实现并发跑数#################有时候我们知道一些程序是可以同时跑的,互不影响,为了提高效率不得不使用并发跑脚本 #1.思路一我们都知道在 ...

  3. Redis集群环境下的键值空间监听事件实现方案

    一直想记录工作中遇到的问题和解决的方法,奈何没有找到一方乐土,最近经常反思,是否需要记录平时的点滴,后台还是决定下定决心记录一些,以便以后用到的时候找不着,实现这样的一个功能主要也是业务所需要的. 需 ...

  4. C#_细说Cookie_Json Helper_Cookies封装

    阅读目录 开始 Cookie 概述 Cookie的写.读过程 使用Cookie保存复杂对象 Js中读写Cookie Cookie在Session中的应用 Cookie在身份验证中的应用 Cookie的 ...

  5. Codeforces 814C

    题意略. 思路: 尺取法,依然是要利用之前的结果. 感觉时间复杂度太高了,竟然也过了. #include<bits/stdc++.h> using namespace std; ; ]; ...

  6. 《阿里巴巴Java开发手册1.4.0》阅读总结与心得(五)

    笔者作为一名有数年工作经验的Java程序员,仔细研读了这份手册,觉得其是一份不可多得的好材料.阿里巴巴在发布时所说,“阿里巴巴集团推出的<阿里巴巴Java开发手册(正式版)>是阿里巴巴近万 ...

  7. EF的3种开发模式

    那么明显开发模式是三种. 即:DateBase First(数据库优先).Model First(模型优先)和Code First(代码优先). 当然,如果把Code First模式的两种具体方式独立 ...

  8. 位运算总结(Bit Operation)

    位运算 数字用二进制表示后的运算 无论是有符号,无符号还是其他各种类型的数.它们之间的转换的基石就是二进制的表达式没有发生改变,变得只是转换的表达式. 1.简单的布尔运算 Boolean algebr ...

  9. 2018中国大学生程序设计竞赛 - 网络选拔赛 hdu 6440 Dream 模拟

    Dream Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  10. lightoj 1046 - Rider(bfs)

    A rider is a fantasy chess piece that can jump like a knight several times in a single move. A rider ...