Escape (BFS + 模拟)
Problem Description
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
All castles begin to shoot when Little A starts to escape.
Proceed to the end of file.
Output
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 + 模拟)的更多相关文章
- BFS+模拟 ZOJ 3865 Superbot
题目传送门 /* BFS+模拟:dp[i][j][p] 表示走到i,j,方向为p的步数为多少: BFS分4种情况入队,最后在终点4个方向寻找最小值:) */ #include <cstdio&g ...
- hdu_1495_非常可乐(bfs模拟)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1495 题意:不解释 题解:BFS模拟,不过要细心,把所有情况都列举出来,开一个数组记录状态,代码有点长 ...
- Hdu 5336 XYZ and Drops (bfs 模拟)
题目链接: Hdu 5336 XYZ and Drops 题目描述: 有一个n*m的格子矩阵,在一些小格子里面可能会有一些水珠,每个小水珠都有一个size.现在呢,游戏开始咯,在一个指定的空的小格子里 ...
- hdu 2364 Escape【模拟优先队列】【bfs】
题目链接:https://vjudge.net/contest/184966#problem/A 题目大意: 走迷宫.从某个方向进入某点,优先走左或是右.如果左右都走不通,再考虑向前.绝对不能往后走, ...
- HDU3533 Escape —— BFS / A*算法 + 预处理
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3533 Escape Time Limit: 20000/10000 MS (Java/Others) ...
- HDU 3533 Escape (BFS + 预处理)
Escape Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- POJ 3414 Pots【bfs模拟倒水问题】
链接: http://poj.org/problem?id=3414 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22009#probl ...
- HDU 3533 Escape(bfs)
Escape Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- 【算法系列学习】[kuangbin带你飞]专题二 搜索进阶 D - Escape (BFS)
Escape 参考:http://blog.csdn.net/libin56842/article/details/41909459 [题意]: 一个人从(0,0)跑到(n,m),只有k点能量,一秒消 ...
随机推荐
- HashMap与ConcurrentHashMap在Java8的改进
链接:http://www.cnblogs.com/huaizuo/archive/2016/04/20/5413069.html#undefined http://www.cnblogs.com/h ...
- Kali Linux无法访问网络的问题
首先 ping www.baidu.com ping: unkown host www.baidu.com 然后 ping 8.8.8.8 connect:network is unreachable ...
- 10G的变态SQL文件,如何快速打开编辑?
工作中,偶尔需要编辑一些大文件,比如 log 文件,后者一些变态的 SQL,此时用平常的编辑器就会显得力不从心,要么直接打不开,要么打开后卡得要死. 本文就给大家推荐几款可以操作大文件的编辑器,准备好 ...
- 以帧为存储单位的循环stack
此stack主要是作为存储空间使用,主要的借口就是push和pop. stack frame的src以及例程位于stack_FrameTest这个库当中,其中有readme文件,可以快速上手. sta ...
- 【阿里云IoT+YF3300】3. Alink物模型之属性上传和下发
[名词解释]属性:设备的功能模型之一,一般用于描述设备运行时的状态,如环境监测设备所读取的当前环境温度等.属性支持 GET 和 SET 请求方式.应用系统可发起对属性的读取和设置请求. 在上一篇文章& ...
- 亲,麻烦给个五星好评!—RatingBar
引言 上一篇的CheckBox已经让大家越来越接近实战演练了,本章我们继续分享干货给大家,今天介绍一个实用的UI控件RatingBar(星级评分条),对于使用过电商APP(某东,某宝等)的小伙伴们来说 ...
- 二分查找法---scala方式
二分查找法---scala方式 ,b) } }
- Spring学习之旅(十四)--缓存
数据库的读写并发一直都是应用性能的瓶颈所在之一,针对改动频率很小的数据我们应该将他存放到缓存中,减少与数据库的交互. 启用对缓存的支持 Spring 对缓存的支持有两种方式: 注解驱动的缓存 XML ...
- tf.control_dependencies
tf.control_dependencies()是用来控制计算流图的,给图中的某些节点指定计算的顺序. 原型: tf.control_dependencies(self, control_input ...
- 使用coding和hexo快速搭建博客
欢迎访问我的个人博客皮皮猪:http://www.zhsh666.xyz 今天教大家怎么用hexo快速搭建自己的博客.我不是专业人士,不懂前端知识,所以我十分讨厌那些专业术语,讲了一大堆,对于技术小白 ...