zoukankan      html  css  js  c++  java
  • hLG2034Fire Maze ---BFS

    Description
    After escaping from Figo's chase, Severus falls into a N * M maze designed by Figo.

    At first, Severus is located on the grid S. Every second he can only move to the four grids that adjacent to the grid he is

     located on. The moment he move to any side of the maze, he will get rid of Figo.

    After T seconds, Figo will reach the maze. Because Figo is the designer of the maze, when Figo arrive, he can reach any 

    grid if he want. If Severus can't leave the maze at that moment, there is no doubt that he will be caught by Figo.

    Figo is very cunning. In the maze he set not only walls, but also fire! After every second, the fire will spread to the four 

    grid that adjacent to it. When a grid is on fire, certainly, Severus can not be on the grid. Can Severus escape from the

     maze?

    Input
    The first line will be a integer CS, indicating the number of test cases.
    In every case, there will be three integer N, M, T.
    After that there will be N * M characters, decribe the maze.

    The "." is a empty grid, "#" is wall, "F" is the fire, "S" is the initial grid that Severus stands on.

    10 <= n , m  <= 100      10 <= T <=10000

    Output

    There is only one line, if Severus can get out, output the mininum time he need to escape from the maze. If he can't,

     output "Poor Severus must be caught by strong Figo!!!"

    Sample Input
    2
    4 4 4
    ####
    #SF#
    #..#
    #..#
    3 3 4
    ###
    #S.

    #.F

     

    Sample Output
    3
    Poor Severus must be caught by strong Figo!!!
     
    Source
    2014 Winter Holiday Contest 5
    Author
    TwIStOy

    思路:双重bfs  只需要两个vis分别标记并记录路径  q1 q2 分别储存点即可

    代码:

      1 #include<iostream>
      2 #include<string.h>
      3 #include<stdio.h>
      4 #include<queue>
      5 using namespace std;
      6 
      7 struct point
      8 {
      9     int x;
     10     int y;
     11 }poin[10];
     12 
     13 queue<point>q1,q2;
     14 
     15 int xx[4]={0,0,1,-1};
     16 int yy[4]={1,-1,0,0};
     17 
     18 int n,m;
     19 
     20 const int maxn=105;
     21 
     22 int vis1[maxn][maxn];
     23 int vis2[maxn][maxn];
     24 char a[maxn][maxn];
     25 
     26 int bfs()
     27 {
     28     while(!q1.empty())
     29     {
     30         while(!q2.empty()&&vis2[q2.front().x][q2.front().y]!=vis1[q1.front().x][q1.front().y])
     31         {
     32             int tmx=q2.front().x;
     33             int tmy=q2.front().y;
     34             q2.pop();
     35             for(int j=0;j<4;j++)
     36             {
     37                 poin[3].x=tmx+xx[j];
     38                 poin[3].y=tmy+yy[j];
     39                 if(!vis2[poin[3].x][poin[3].y]&&poin[3].x>=0&&poin[3].x<n&&poin[3].y>=0&&poin[3].y<m&&a[poin[3].x][poin[3].y]!='#')
     40                 {
     41                     vis2[poin[3].x][poin[3].y]=vis2[tmx][tmy]+1;
     42                     q2.push(poin[3]);
     43                 }
     44             }
     45         }
     46         int tmpx=q1.front().x;
     47         int tmpy=q1.front().y;
     48         q1.pop();
     49         for(int i=0;i<4;i++)
     50         {
     51             poin[4].x=tmpx+xx[i];
     52             poin[4].y=tmpy+yy[i];
     53             if(!vis1[poin[4].x][poin[4].y]&&!vis2[poin[4].x][poin[4].y]&&a[poin[4].x][poin[4].y]!='#')
     54             {
     55                 if(poin[4].x<0||poin[4].x>=n||poin[4].y<0||poin[4].y>=m)
     56                     return vis1[tmpx][tmpy]+1;
     57 
     58 
     59                 vis1[poin[4].x][poin[4].y]=vis1[tmpx][tmpy]+1;
     60                 q1.push(poin[4]);
     61             }
     62         }
     63     }
     64     return 0xfffffff;
     65 }
     66 
     67 int main()
     68 {
     69     int t;
     70     int time;
     71     ios::sync_with_stdio(false);
     72     //freopen("aa.txt","r",stdin);
     73     cin>>t;
     74     while(t--)
     75     {
     76         while(!q1.empty())
     77         q1.pop();
     78         while(!q2.empty())
     79         q2.pop();
     80 
     81         cin>>n>>m>>time;
     82         for(int i=0;i<n;i++){
     83             for(int j=0;j<m;j++){
     84                 cin>>a[i][j];
     85                 if(a[i][j]=='S')
     86                 {
     87                     poin[0].x=i;
     88                     poin[0].y=j;
     89                     vis1[i][j]=1;
     90                     q1.push(poin[0]);
     91                 }
     92                 else if(a[i][j]=='F')
     93                 {
     94                     poin[1].x=i;
     95                     poin[1].y=j;
     96                     vis2[i][j]=1;
     97                     q2.push(poin[1]);
     98                 }
     99             }
    100         }
    101         int bx=bfs()-1;
    102         if(bx<=time)
    103         cout<<bx<<endl;
    104         else
    105         cout<<"Poor Severus must be caught by strong Figo!!!"<<endl;
    106     }
    107     return 0;
    108 }
    View Code
  • 相关阅读:
    OpenST Basic tool library
    本地自旋锁与信号量/多服务台自旋队列-spin wait风格的信号量
    [IOS]mac以太网连接
    Android自己定义控件皮肤
    DirectX11 学习笔记1
    启用QNX系统,海尔智能冰箱或成业界“宝马”
    vu 是什么
    java.lang.NoClassDefFoundError: Lorg/apache/log4j/Logger报错
    关于Maven项目install时出现No compiler is provided in this environment的处理
    taotao购物车
  • 原文地址:https://www.cnblogs.com/zhanzhao/p/3574619.html
Copyright © 2011-2022 走看看