Treasures and Vikings(两次搜索)
Treasures and Vikings
https://www.luogu.org/problemnew/show/P4668
题意翻译
你有一张藏宝图,藏宝图可视为 N×MN×M 的网格。每个格子可能是你的船、贼船、海、陆地或藏宝点。你只有一条船,整张图只有一条贼船。你和贼船都只能在海域移动。藏宝点在海中。
你与贼船交替移动,你移动一次+贼船移动一次算作一回合。每次移动,你可以移动到上下左右四个相邻格子中的一格,也可以不移动。贼船的移动同理,贼船也可以不移动。你先移动。
每一回合结束后,如果你和贼船在同一行或同一列,你就挂了;在你没挂的情况下,如果你位于藏宝点,你就拿到了宝藏。
请问:是否有一条安全的路径,使得无论贼船怎么跑你都能或者拿到宝藏。
Translated by @Planet6174
题目描述
You have a treasure map that is arranged into a N \times MN×M grid. A grid square may be either sea or part of an island. In addition, the map shows the treasure and an enemy Viking ship that occupies one (sea) square. Finally, for convenience you have also drawn your own position.
Now you must set up a fixed route to get the treasure. The route must start at your position, end at the treasure, and consist of a sequence of moves. In each move, you can go only to an (horizontally or vertically) adjacent square that is not part of an island. But beware: The Viking ship might follow you, using the same kind of moves! After each of your moves according to your route, the Viking ship may move or not. Your move and the Vikings’ reaction together is called a round.
After every round, the following checks are made:
- If you are in line with the Viking ship (you are in the same vertical or horizontal line as the Viking ship with only sea between the Viking ship and you), you are dead.
- If you aren’t dead and at the treasure-spot, you get the treasure.
Write a program that decides whether it is possible to set up a fixed route in advance such that you can get the treasure by following this route and will not get killed by the Vikings – no matter how the Viking ship moves.
输入输出格式
输入格式:
The first line of input contains two integers NN and MM, the dimensions of the map. Each of the following NN lines contain MM characters. Each character describes a square in the map, and is either .
(sea), I
(part of an island), V
(the Viking ship), Y
(your position), or T
(the treasure). Each of V
, Y
, and T
will occur exactly once.
输出格式:
The only line of the output must contain the string YES
, if it is possible to set up a route to get the treasure, or NO
otherwise.
输入输出样例
5 7
Y.....V
..I....
..IIIII
.......
...T...
YES
5 7
Y....V.
..I....
..IIIII
.......
...T...
NO
2 3
.YT
VII
NO
说明
Sample Explanation 1
The route is:go down for three times,go right for three times too,go down at last.
数据范围
对于 50\%50% 的数据,1 ≤ N,M ≤ 200 1≤N,M≤200。
对于所有数据,1≤N,M ≤ 700 1≤N,M≤700。
搜索出海盗船到每个点的最短路,在搜自己到每个点的最短路是否小于海盗船到每个点的最短路(洛谷开了氧气才过。。感觉有更厉害的搜索方法,但是没想出来。。)
// luogu-judger-enable-o2
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std; char map[][];
int book[][];
int flag[][];
int n,m;
int yx,yy,vx,vy,tx,ty;
struct sair{
int x,y,step;
}; struct Step{
int x,y,step,num,dir;
}; int dir[][]={,,,-,,,-,}; void bfsV(){
sair s,e;
s.x=vx,s.y=vy,s.step=;
memset(book,-,sizeof(book));
queue<sair>Q;
Q.push(s);
flag[s.x][s.y]=;
book[s.x][s.y]=;
while(!Q.empty()){
s=Q.front();
Q.pop();
int co=;
int tmp=s.y+co;
while(tmp<m&&map[s.x][tmp]!='I'){
if(book[s.x][tmp]==-){
book[s.x][tmp]=s.step;
}
co++;
tmp=s.y+co;
}
co=-;
tmp=s.y+co;
while(tmp>=&&map[s.x][tmp]!='I'){
if(book[s.x][tmp]==-){
book[s.x][tmp]=s.step;
}
co--;
tmp=s.y+co;
}
co=;
tmp=s.x+co;
while(tmp<n&&map[tmp][s.y]!='I'){
if(book[tmp][s.y]==-){
book[tmp][s.y]=s.step;
}
co++;
tmp=s.x+co;
}
co=-;
tmp=s.x+co;
while(tmp>=&&map[tmp][s.y]!='I'){
if(book[tmp][s.y]==-){
book[tmp][s.y]=s.step;
}
co--;
tmp=s.x+co;
}
for(int i=;i<;i++){
e.x=s.x+dir[i][];
e.y=s.y+dir[i][];
if(e.x>=&&e.x<n&&e.y>=&&e.y<m&&map[e.x][e.y]!='I'&&!flag[e.x][e.y]){
flag[e.x][e.y]=;
e.step=s.step+;
Q.push(e);
}
}
}
} void bfsY(){
sair s,e;
queue<sair>Q;
s.x=yx,s.y=yy,s.step=;
Q.push(s);
memset(flag,,sizeof(flag));
while(!Q.empty()){
s=Q.front();
Q.pop();
for(int i=;i<;i++){
e.x=s.x+dir[i][];
e.y=s.y+dir[i][];
if(e.x>=&&e.y<n&&e.y>=&&e.y<m&&s.step<book[e.x][e.y]&&!flag[e.x][e.y]){
e.step=s.step+;
flag[e.x][e.y]=;
if(e.x==tx&&e.y==ty){
puts("YES");
return;
}
Q.push(e);
}
}
}
puts("NO");
} int main(){
scanf("%d %d%*c",&n,&m);
for(int i=;i<n;i++){
scanf("%s%*c",map[i]);
}
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(map[i][j]=='Y'){
yx=i,yy=j;
}
else if(map[i][j]=='V'){
vx=i,vy=j;
}
else if(map[i][j]=='T'){
tx=i,ty=j;
}
}
}
bfsV();
/* for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cout<<book[i][j]<<" ";
}
cout<<endl;
}*/
bfsY();
}
Treasures and Vikings(两次搜索)的更多相关文章
- iOS中的两种搜索方式UISearchDisplayController和UISearchController
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 以前iOS的搜索一般都使用UISearchDisplayCon ...
- Java实现 LeetCode 581 最短无序连续子数组(从两遍搜索找两个指针)
581. 最短无序连续子数组 给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序. 你找到的子数组应是最短的,请输出它的长度. 示例 1: 输入: ...
- 使用Nominatim进行openstreetmap地址搜索/解析
Nominatim(来自拉丁语,意思是“名称”)是一个可以按名称和地址来搜索OSM中的数据,并生成OSM点的合成地址的工具(反向地理编码).可用在http://nominatim.openstreet ...
- Elasticsearch 数据搜索篇·【入门级干货】
ES即简单又复杂,你可以快速的实现全文检索,又需要了解复杂的REST API.本篇就通过一些简单的搜索命令,帮助你理解ES的相关应用.虽然不能让你理解ES的原理设计,但是可以帮助你理解ES,探寻更多的 ...
- 迷宫问题求解之“A*搜索”(二)
摘要:在迷宫问题求解之"穷举+回溯"(一)这篇文章中采用"穷举+回溯"的思想,虽然能从迷宫的入口到出口找出一条简单路径,但是找出来的不是最优路径.因此本文采用A ...
- linux 命令案例学习——文件搜索
两个搜索文件的工具 locate ——仅仅通过文件名查找文件 find ——依据文件的各种属性在既定目录(包括子目录)里查找 一个通常与文件搜索命令一起使用.处理搜索结果文件列表的命令 xa ...
- CodeForces 164A Variable, or There and Back Again 搜索
Variable, or There and Back Again 题目连接: http://codeforces.com/problemset/problem/164/A Description L ...
- Google搜索语法
原文:http://www.jianshu.com/p/37fe4f1381ef 前言 之前听过一个笑话,有人打开浏览器,输入www.baidu.com, 然后搜索框输入Google,查询google ...
- 第三代搜索推出网民评价系统,seo末日还会远吗?
昨天的360搜索可谓风光无限,两大搜索新品同日上线,至今360导航页面依旧飘荡着两者的身影,但是不少站长从此却是忧心忡忡,seo末日是否真的要到来了?笔者想起数日前写的一篇博文:seo末日言论频频来袭 ...
随机推荐
- js判断是android访问还是ios访问
原文地址:http://blog.csdn.net/wy978651775/article/details/9014039 该博主也是转载的,但是没有标明出处. 判断原理: JavaScript是前端 ...
- OpenGL 画出雷达动态扫描效果(一)
最终效果如下所示 Demo下载 http://files.cnblogs.com/xd-jinjian/Debug.zip 源代码下载 http://download.csdn.net/detail ...
- 自己写的jQuery放大镜插件效果(二)(采用只有一张图片的思路)
废话不多说,先看效果图,和上一章节用的是同一个小图片: 这个方法实现的放大有个弊端就是放大倍数设置的过大的话,会带来图片上的模糊.但是图片加载的话要比使用2张图片加载的快很多 插件代码: ; (fun ...
- javaScript语言的预编译与运行
JS代码执行的过程: 1.预编译 ---- 事先对js代码做一个预处理 2.代码运行---开始执行JS代码. JS编程: 1.加载DOM的最好在/BODY之前 2.与DOM渲染无关的放在Head里面 ...
- java自定义事件机制分析
import java.util.ArrayList; import java.util.EventListener; import java.util.EventObject; import jav ...
- NLP-Progress记录NLP最新数据集、论文和代码: 助你紧跟NLP前沿
Github https://github.com/sebastianruder/NLP-progress 官方网址 https://nlpprogress.com/ NLP-Progress 同时涵 ...
- 中国移动基于ARM/x86服务器的Ceph性能对比
2018年11月17日,Ceph中国行首次走进杭州,万众期待的杭州站沙龙如期而至,虽然杭州已经开始降温,阵雨不断,但活动现场依然热度爆表,杭州各大IT公司的从业人员和Ceph爱好者纷纷而来. 中国移动 ...
- [Mysql]查看版本号的五种方式
[Mysql]查看版本号的五种方式 目录(?)[+] 查看版本信息 #1 使用命令行模式进入mysql会看到最开始的提示符 Your MySQL connection id is 3Serve ...
- 20180129周一之学习PYTHON笔记【安装、查看工作目录、】
一,安装过程中多选一个ADD的项,免去设置环境变量. 二,PYAUTOGUI模块控制键鼠. IMAGE模块. ----------------------python 如何查看与更换工作目录----- ...
- 〈Android 群英传-神兵利器〉第7章一个的寂寞与一群人的狂欢
|---第7章一个的寂寞与一群人的狂欢 |---7.1如何解决问题 |---Chrome浏览器 |---Chrome开发者工具 |---Chrome插件(Json-Handle:Json格式化查看工具 ...