zoukankan      html  css  js  c++  java
  • Fire!

    uva11624:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2671

    题意:一个大火蔓延的迷宫。Joe每分钟可以走到上下左右4个方向的相邻格之一,而所有着火的格子都会往四周蔓延(即如果某个空格与着火格子有公共边,则下一分钟这个格子将会着火。)迷宫中有一些障碍,Joe和火都无法进入。当Joe走到一个迷宫的边界格子时,我们认为他已经出了迷宫。

    题解:把每个着火点作为起点,做一遍BFS,得出每个格子的着火时间,然后以人的坐标为起点,做一遍BFS,此时判断条件是下一个格子的着火时间要比当前时间晚,下一个格子才可以进去。然后BFS之后,判断边界的的最小值即可。

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cstring>
      4 #include<algorithm>
      5 #include<queue>
      6 #define INF 1000000000
      7 using namespace std;
      8 const int N=1002;
      9 struct Node{
     10    int x;
     11    int y;
     12    int step;
     13 };
     14 queue<Node>Q2;
     15 int counts1[N][N];
     16 int counts2[N][N];
     17 int ct[N][N];
     18 int n,m;
     19 char map[N][N];
     20 void BFS(){
     21     int dir[4][2]={{-1,0},{1,0},{0,1},{0,-1}};
     22     while(!Q2.empty()){
     23         Node tp=Q2.front();
     24         Q2.pop();
     25         for(int i=0;i<4;i++){
     26             int xx=dir[i][0]+tp.x;
     27             int yy=dir[i][1]+tp.y;
     28             if(xx>=1&&xx<=n&&yy>=1&&yy<=m){
     29                 if(map[xx][yy]!='#'&&tp.step+1<counts1[xx][yy]){
     30                     Node ttt;
     31                     ttt.x=xx;
     32                     ttt.y=yy;
     33                     ttt.step=tp.step+1;
     34                     counts1[xx][yy]=tp.step+1;
     35                     Q2.push(ttt);
     36                 }
     37 
     38             }
     39         }
     40     }
     41 }
     42 int BFS1(int x,int y){
     43     int dir[4][2]={{-1,0},{1,0},{0,1},{0,-1}};
     44     for(int i=1;i<=n;i++)
     45         for(int j=1;j<=m;j++)
     46           counts2[i][j]=INF;
     47     queue<Node>Q;
     48     Node temp;
     49     temp.x=x;
     50     temp.y=y;
     51     temp.step=0;
     52     Q.push(temp);
     53     counts2[x][y]=0;
     54     while(!Q.empty()){
     55         Node tp=Q.front();
     56         Q.pop();
     57         for(int i=0;i<4;i++){
     58             int xx=dir[i][0]+tp.x;
     59             int yy=dir[i][1]+tp.y;
     60             if(xx>=1&&xx<=n&&yy>=1&&yy<=m){
     61                 if(tp.step+1<counts1[xx][yy]&&map[xx][yy]!='#'&&tp.step+1<counts2[xx][yy]){
     62                     Node ttt;
     63                     ttt.x=xx;
     64                     ttt.y=yy;
     65                     ttt.step=tp.step+1;
     66                     counts2[xx][yy]=tp.step+1;
     67                     Q.push(ttt);
     68                 }
     69 
     70             }
     71         }
     72     }
     73     int minn=INF;
     74    for(int i=1;i<=n;i++)
     75     minn=min(counts2[i][m],min(minn,counts2[i][1]));
     76    for(int i=1;i<=m;i++)
     77     minn=min(counts2[1][i],min(minn,counts2[n][i]));
     78      return minn;
     79 }
     80 void  init(){
     81    while(!Q2.empty())
     82             Q2.pop();
     83         for(int i=1;i<=n;i++)
     84         for(int j=1;j<=m;j++)
     85           counts1[i][j]=INF;
     86 }
     87 int sx,sy;
     88 int main(){
     89     int test;
     90     scanf("%d",&test);
     91     while(test--){
     92             scanf("%d%d",&n,&m);
     93               init();
     94         for(int i=1;i<=n;i++)
     95             for(int j=1;j<=m;j++){
     96                cin>>map[i][j];
     97                if(map[i][j]=='F'){
     98                     Node ttp;
     99                     ttp.x=i;
    100                     ttp.y=j;
    101                     ttp.step=0;
    102                     Q2.push(ttp);
    103                     counts1[i][j]=0;
    104                }
    105                if(map[i][j]=='J'){
    106                      sx=i;
    107                      sy=j;
    108                }
    109             }
    110 
    111             BFS();
    112          int ans=BFS1(sx,sy);
    113          if(ans==INF)printf("IMPOSSIBLE
    ");
    114          else printf("%d
    ",ans+1);
    115     }
    116 }
    View Code
  • 相关阅读:
    jq insertBefore 的返回值
    微信公众号-定位之地址逆解析&导航
    微信JS-SDK
    Vue
    ES6-函数的扩展
    ES6-数组的扩展
    JSP
    JS
    HTML+CSS
    jdbc操作数据库
  • 原文地址:https://www.cnblogs.com/chujian123/p/3757738.html
Copyright © 2011-2022 走看看