zoukankan      html  css  js  c++  java
  • 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 imes 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  1N,M200。

    对于所有数据,1≤N,M ≤ 700  1N,M700。

    搜索出海盗船到每个点的最短路,在搜自己到每个点的最短路是否小于海盗船到每个点的最短路(洛谷开了氧气才过。。感觉有更厉害的搜索方法,但是没想出来。。)

      1 // luogu-judger-enable-o2
      2 #include<iostream>
      3 #include<cstdio>
      4 #include<cstring>
      5 #include<cmath>
      6 #include<string>
      7 #include<algorithm>
      8 #include<queue>
      9 #include<vector>
     10 using namespace std;
     11 
     12 char map[705][705];
     13 int book[705][705];
     14 int flag[705][705];
     15 int n,m;
     16 int yx,yy,vx,vy,tx,ty;
     17 struct sair{
     18     int x,y,step;
     19 };
     20 
     21 struct Step{
     22     int x,y,step,num,dir;
     23 };
     24 
     25 int dir[4][2]={0,1,0,-1,1,0,-1,0};
     26 
     27 void bfsV(){
     28     sair s,e;
     29     s.x=vx,s.y=vy,s.step=0;
     30     memset(book,-1,sizeof(book));
     31     queue<sair>Q;
     32     Q.push(s);
     33     flag[s.x][s.y]=1;
     34     book[s.x][s.y]=0;
     35     while(!Q.empty()){
     36         s=Q.front();
     37         Q.pop();
     38         int co=1;
     39         int tmp=s.y+co;
     40         while(tmp<m&&map[s.x][tmp]!='I'){
     41             if(book[s.x][tmp]==-1){
     42                 book[s.x][tmp]=s.step;
     43             }
     44             co++;
     45             tmp=s.y+co;
     46         }
     47         co=-1;
     48         tmp=s.y+co;
     49         while(tmp>=0&&map[s.x][tmp]!='I'){
     50             if(book[s.x][tmp]==-1){
     51                 book[s.x][tmp]=s.step;
     52             }
     53             co--;
     54             tmp=s.y+co;
     55         }
     56         co=1;
     57         tmp=s.x+co;
     58         while(tmp<n&&map[tmp][s.y]!='I'){
     59             if(book[tmp][s.y]==-1){
     60                 book[tmp][s.y]=s.step;
     61             }
     62             co++;
     63             tmp=s.x+co;
     64         }
     65         co=-1;
     66         tmp=s.x+co;
     67         while(tmp>=0&&map[tmp][s.y]!='I'){
     68             if(book[tmp][s.y]==-1){
     69                 book[tmp][s.y]=s.step;
     70             }
     71             co--;
     72             tmp=s.x+co;
     73         }
     74         for(int i=0;i<4;i++){
     75             e.x=s.x+dir[i][0];
     76             e.y=s.y+dir[i][1];
     77             if(e.x>=0&&e.x<n&&e.y>=0&&e.y<m&&map[e.x][e.y]!='I'&&!flag[e.x][e.y]){
     78                 flag[e.x][e.y]=1;
     79                 e.step=s.step+1;
     80                 Q.push(e);
     81             }
     82         }
     83     }
     84 }
     85 
     86 void bfsY(){
     87     sair s,e;
     88     queue<sair>Q;
     89     s.x=yx,s.y=yy,s.step=1;
     90     Q.push(s);
     91     memset(flag,0,sizeof(flag));
     92     while(!Q.empty()){
     93         s=Q.front();
     94         Q.pop();
     95         for(int i=0;i<4;i++){
     96             e.x=s.x+dir[i][0];
     97             e.y=s.y+dir[i][1];
     98             if(e.x>=0&&e.y<n&&e.y>=0&&e.y<m&&s.step<book[e.x][e.y]&&!flag[e.x][e.y]){
     99                 e.step=s.step+1;
    100                 flag[e.x][e.y]=1;
    101                 if(e.x==tx&&e.y==ty){
    102                     puts("YES");
    103                     return;
    104                 }
    105                 Q.push(e);
    106             }
    107         }
    108     }
    109     puts("NO");
    110 }
    111 
    112 int main(){
    113     scanf("%d %d%*c",&n,&m);
    114     for(int i=0;i<n;i++){
    115         scanf("%s%*c",map[i]);
    116     }
    117     for(int i=0;i<n;i++){
    118         for(int j=0;j<m;j++){
    119             if(map[i][j]=='Y'){
    120                 yx=i,yy=j;
    121             }
    122             else if(map[i][j]=='V'){
    123                 vx=i,vy=j;
    124             }
    125             else if(map[i][j]=='T'){
    126                 tx=i,ty=j;
    127             }
    128         }
    129     }
    130     bfsV();
    131  /*   for(int i=0;i<n;i++){
    132         for(int j=0;j<m;j++){
    133             cout<<book[i][j]<<" ";
    134         }
    135         cout<<endl;
    136     }*/
    137     bfsY();
    138 }
    View Code
  • 相关阅读:
    WPF XAML之bing使用StringFormat
    C#程序以管理员权限运行
    注册表REG文件编写大全
    linux 的基本操作(编写shell 脚本)
    linux的基本操作(正则表达式)
    linux的基本操作(shell 脚本的基础知识)
    linux的基本操作(RPM包或者安装源码包)
    linux的基本操作(文件压缩与打包)
    linux的基本操作(文本编辑工具vim)
    linux的基本操作(磁盘管理)
  • 原文地址:https://www.cnblogs.com/Fighting-sh/p/9799796.html
Copyright © 2011-2022 走看看