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点能量,一秒消 ...
随机推荐
- vim 基础配置
最近在使用 python 搞服务, 简单配置了一个 vim, 配置了自动补全以及背景色 .(ps:搜狗输入法快捷键占用真是太坑爹,改用谷歌输入法,世界安静了) 具体配置如下: 一. 安装插件 1.克隆 ...
- 从零写一个编译器(九):语义分析之构造抽象语法树(AST)
项目的完整代码在 C2j-Compiler 前言 在上一篇完成了符号表的构建,下一步就是输出抽象语法树(Abstract Syntax Tree,AST) 抽象语法树(abstract syntax ...
- insertSql语句中的trim标签的使用
insert into MB_BATCH_DIS_DETAILS <trim prefix="(" suffix=")" suffixOverrid ...
- 阿里分布式事务seata入门(采坑)
1. 阿里分布式事务seata入门(采坑) 1.1. 前言 seata是feascar改名而来,这是阿里在19年年初开源出来的分布式事务框架,当初刚出来的时候就想研究下了,一直拖到了现在,目前是0.8 ...
- HBase的安装和使用
文章作者:foochane 原文链接:https://foochane.cn/article/2019062801.html 1 Hbase基本介绍 Hbase是一个分布式数据库,可以提供数据的实时 ...
- 熔断监控Turbine
step1:修改hosts的ip地址映射,创建eureka集群 可参考:https://www.cnblogs.com/noneplus/p/11374883.html step2:创建服务提供者 p ...
- three.js实现球体地球2018年全球GDP前十国家标记
概况如下: 1.SphereGeometry实现自转的地球: 2.THREE.Math.degToRad,Math.sin,Math.cos实现地图经纬度与三位坐标x,y,z之间的转换: 3.Imag ...
- [Python] 通过采集两万条数据,对《无名之辈》影评分析
一.说明 本文主要讲述采集猫眼电影用户评论进行分析,相关爬虫采集程序可以爬取多个电影评论. 运行环境:Win10/Python3.5. 分析工具:jieba.wordcloud.pyecharts.m ...
- 详解javascript中的this的指向问题
首先,要明白this 既不指向函数自身,也不指函数的词法作用域.this一般存在于函数中,表示当前函数的执行上下文,如果函数没有执行,那么this没有内容,只有函数在执行后this才有绑定. 然后,我 ...
- POJ-2155-Matrix二位树状数组应用
题目: 一个只有0和1构成的二维平面,给你两种指令,一种是区间的更新,即0变为1,1变为0:一种是查询一个点是1还是0: 由于是二进制,所以每次更新在相应的点上加一,最后对2取余即可. 至于二维的树状 ...