Uvaoj 11624 - Fire!

简介:
/*************************************************************************
    > File Name: test.cpp
    > Author: HJZ
    > Mail: 2570230521@qq.com 
    > Created Time: 2014年08月03日 星期日 07时26分58秒
 ************************************************************************/

/*
   题目大意:一个人joe想从迷宫中逃脱,但是迷宫中有着火的地方!joe每分钟向上下左右其中一个方向走一步,当然有火的地方和有墙的地方是不能通过的!
   另外,火的蔓延的方向是同一时刻向上下左右四个方向蔓延!

   思路:每一时刻,先将火的蔓延位置标记出来,并将新的火的位置放入队列qf中;
   因为某一时刻,我们将所有joe可能走的路径都放入了队列中了,假如t1时刻他能走的位置是5个,那么在t1+1时刻,根据上一时刻t1的可能位置更新t1+1
   时刻的可能位置,t1时刻的位置出队列q, t1+1时刻的新位置并重新进入队列!
*/

#include <queue>
#include <string>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <iomanip>
#include<cmath>
#include <algorithm>
#include<queue>
#define M 1005
#define mem(a) (memset((a), 0, sizeof(a)))
#define get(s) fgets(s, sizeof(s)-1, stdin)

using namespace std;

char map[M][M];
int n, m;
int bg, ed;
int dir[4][2]={1, 0, 0, 1, -1, 0, 0, -1};

struct Point{
   int x, y, step;
   Point(){
     
   }
   Point(int x, int y, int step){
      this->x=x; 
      this->y=y; 
      this->step=step;
   }
};
queue<Point>q; queue<Point>qf;
int cntF;//某一时刻,火点的位置进入队列的个数
int cntP;//某一时刻,joe可走位置进入队列的个数

int bfs(){
   while(!q.empty()) q.pop();
   q.push(Point(bg, ed, 1));
   while(1){
      while(!qf.empty() && cntF){
         Point  Fcur=qf.front();
         qf.pop();
         --cntF;
         int x=Fcur.x, y=Fcur.y;
         for(int i=0; i<4; ++i){
            int xx=x+dir[i][0];
            int yy=y+dir[i][1];
            if(map[xx][yy]!='F' && map[xx][yy]!='#'){
               map[xx][yy]='F';
               qf.push(Point(xx, yy, 0));
            }
         }
      }
      cntF=qf.size();
      while(!q.empty() && cntP){
            Point cur=q.front();
         q.pop(); --cntP; int x=cur.x, y=cur.y;
        if(x==1 || x==n || y==1 || y==m) return cur.step;
        for(int i=0; i<4; ++i){
          int xx=x+dir[i][1];
          int yy=y+dir[i][0];
          if(map[xx][yy]!='#' && map[xx][yy]!='F' && map[xx][yy]!='X'){
              map[xx][yy]='X'; 
              if(x==1 || x==n || y==1 || y==m) return cur.step+1;
              q.push(Point(xx, yy, cur.step+1)); } } }
          cntP=q.size();
      if(cntP==0)  return -1;
   }
   return -1;
}

int main(){
   int t;
   scanf("%d", &t);
   while(t--){
      scanf("%d%d", &n, &m);
      for(int i=0; i<=n+1; ++i)
         map[i][0]=map[i][m+1]='#';
      for(int i=0; i<=m+1; ++i)
         map[0][i]=map[n+1][i]='#';
      
      while(!qf.empty())  qf.pop();
      cntF=0;
      cntP=1;
          for(int j=0, i=1; i<=n; ++i){
         scanf("%s", map[i]+1);
         for(j=1; j<=m; ++j)
            if(map[i][j]=='J'){
                bg=i;
                ed=j;
            }
            else if(map[i][j]=='F'){
                ++cntF;
                qf.push(Point(i, j, 0));
            }
         map[i][j]='#';
      }     

        int tt=bfs();
        if(tt!=-1)
           printf("%d\n", tt);
        else printf("IMPOSSIBLE\n");
   }
   return 0;
}









本文转自 小眼儿 博客园博客,原文链接:http://www.cnblogs.com/hujunzheng/p/3888581.html,如需转载请自行联系原作者
目录
相关文章
|
4月前
|
分布式计算 大数据 API
|
5月前
|
前端开发
The following tasks did not complete: first Did you forget to signal async completion?
The following tasks did not complete: first Did you forget to signal async completion?
|
6月前
|
内存技术
Egret的TimerEvent.TIMER和Event.ENTER_FRAME的区别
Egret的TimerEvent.TIMER和Event.ENTER_FRAME的区别
35 0
|
9月前
|
自然语言处理 Java 测试技术
Saliency as Evidence: Event Detection with Trigger Saliency Attribution 论文解读
事件检测(ED)是事件抽取的关键子任务,它试图识别文本中特定类型的事件触发词。尽管ED取得了重大进展,但现有方法通常遵循“一个模型适合所有类型”的方法,这种方法认为事件类型之间没有差异,通常会导致相当倾斜的性能。
49 0
|
9月前
|
机器学习/深度学习 人工智能 自然语言处理
DualCor: Event Causality Extraction with Event Argument Correlations论文解读
事件因果关系识别(ECI)是事件因果关系理解的重要任务,其目的是检测两个给定文本事件之间是否存在因果关系。然而,ECI任务忽略了关键的事件结构和因果关系组件信息
54 0
|
11月前
[joint_state_publisher-3] process has died
[joint_state_publisher-3] process has died
|
图形学
Unity【Timeline】- 使用Signal Track添加事件
Unity【Timeline】- 使用Signal Track添加事件
925 0
Unity【Timeline】- 使用Signal Track添加事件
Fire!!
Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the maze neglected to create a fire escape plan. Help Joe escape the maze.
106 0
Twice enter press click will trigger backend roundtrip
Twice enter press click will trigger backend roundtrip
101 0
Twice enter press click will trigger backend roundtrip
bubble click event handling
bubble click event handling
40 0
bubble click event handling