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 VY, 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.

输入输出样例

输入样例#1:

5 7
Y.....V
..I....
..IIIII
.......
...T...
输出样例#1:

YES
输入样例#2:

5 7
Y....V.
..I....
..IIIII
.......
...T...
输出样例#2:

NO
输入样例#3:

2 3
.YT
VII
输出样例#3:

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();
}

最新文章

  1. Linux 内核中的 Device Mapper 机制
  2. js简单弹出层、遮罩层
  3. 微信 {&quot;errcode&quot;:40029,&quot;errmsg&quot;:&quot;invalid code, hints: [ req_id: Cf.y.a0389s108 ]&quot;}
  4. JSTL中的TLD配置和使用。
  5. shell学习之路:流程控制(if)
  6. R12月末关帐的异常检查和处理
  7. switch… case 语句的用法(二)
  8. Ubuntu中设置永久的DNS
  9. UITableView的编辑模式
  10. *[topcoder]TheTree
  11. MVC+UnitOfWork+Repository+EF
  12. VirtualBox 主机与虚拟机互通
  13. 自定义分布式RESTful API鉴权机制
  14. 傅里叶变换 - Fourier Transform
  15. 微信小程序开发入门篇
  16. MT【327】两道不等式题
  17. web前端安全
  18. springmvc 项目完整示例03 小结
  19. day4——无重复字符的最长子串
  20. Linux进程核心调度器之主调度器schedule--Linux进程的管理与调度(十九)

热门文章

  1. 自己写的jQuery浮动广告插件
  2. 关于javascript的cookie的封装
  3. 【POJ】2420 A Star not a Tree?(模拟退火)
  4. Glusterfs3.3.1DHT(hash分布)源代码分析
  5. 使用CMQ和SCF实现邮件发送
  6. python输入输出及变量
  7. 任务计划程序-Windows2008定时重启
  8. java msgbox
  9. AJAX服务器返回数据 连接数据库查询数据
  10. 安装新操作系统 Windows 路径设置 节省C盘空间