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点能量,一秒消 ...
随机推荐
- Java虚拟机日志与参数
虚拟机日志 打印GC日志可以使用参数-XX:+PrintGC /** * -Xmx10m -Xms10m -XX:PretenureSizeThreshold=10485760 * -XX:+Prin ...
- Python 命令行之旅 —— 深入 argparse (一)
作者:HelloGitHub-Prodesire HelloGitHub 的<讲解开源项目>系列,项目地址:https://github.com/HelloGitHub-Team/Arti ...
- RDIFramework.NET敏捷开发框架通过SignalR技术整合即时通讯(IM)
1.引言 即时通讯(IM)是RDIFramework.NET敏捷开发框架全新提供的一个基于Web的即时通讯.内部聊天沟通的工具.界面美观大方对于框架内部进行消息的沟通非常方便.基于RDIFramewo ...
- 第一次appium自动化
今天,自己独自做了一下app自动化,从搭环境到写好一个脚本花了很长时间.用的主要环境是python3.7+appium+sdk+夜神模拟器.appium环境搭建较于复杂,这里就不累述,参考百度教程. ...
- C# 中的数据库操作~存储过程篇Mysql SqlServer
Mysql 存储过程查询方式 SQL server 普通数据库操作 EF 调用SQL SERVER存储过程 Mysql 存储过程查询方式: public NetPort GetNetdevicePor ...
- Python面试总结篇
Python Coding Interview Questions and Answers 面试题一:逻辑运算赋值 v1 = 1 or 9 v2 = 0 or 9 # print(v1, v2)会输出 ...
- IDEA实用教程(一)
IDEA实用教程 一. IDEA简介 简介 IDEA 全称IntelliJ IDEA,是java语言开发的集成环境. IDEA是JetBrains公司的产品. JetBrains官网 : https: ...
- 数据的查找和提取[2]——xpath解析库的使用
xpath解析库的使用 在上一节,我们介绍了正则表达式的使用,但是当我们提取数据的限制条件增多的时候,正则表达式会变的十分的复杂,出一丁点错就提取不出来东西了.但python已经为我们提供了许多用于解 ...
- effective java 3th 序
正本基本是自己翻译,翻译绝对有错误,就是这么自信,看的时候,自己注意下,如果感觉有语句不通,那么可能就是我翻译的出现了问题,可以自己翻找原文对比下. 其中自己的见解,我写在脚注中. 在 1997 年, ...
- npm基本命令
1.npm是什么? npm(Node Package Manager)意思是 node 的包管理器,它是随着 NodeJs 安装时一起被安装的: 无论是在前端还是在前端开发中都会使用到 npm 包管理 ...