`

Uva 11624 - Fire! 多源宽度优先遍历

 
阅读更多

   连接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=543&problem=2671&mosmsg=Submission+received+with+ID+11653475

 

Problem B: 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.

Given Joe's location in the maze and which squares of the maze are on fire, you must determine whether Joe can exit the maze before the fire reaches him, and how fast he can do it.

Joe and the fire each move one square per minute, vertically or horizontally (not diagonally). The fire spreads all four directions from each square that is on fire. Joe may exit the maze from any square that borders the edge of the maze. Neither Joe nor the fire may enter a square that is occupied by a wall.

Input Specification

The first line of input contains a single integer, the number of test cases to follow. The first line of each test case contains the two integers R and C, separated by spaces, with 1 <= R,C <= 1000. The following R lines of the test case each contain one row of the maze. Each of these lines contains exactly C characters, and each of these characters is one of:

  • #, a wall
  • ., a passable square
  • J, Joe's initial position in the maze, which is a passable square
  • F, a square that is on fire

There will be exactly one J in each test case.

Sample Input

2
4 4
####
#JF#
#..#
#..#
3 3
###
#J.
#.F

Output Specification

For each test case, output a single line containing IMPOSSIBLE if Joe cannot exit the maze before the fire reaches him, or an integer giving the earliest time Joe can safely exit the maze, in minutes.

Output for Sample Input

3
IMPOSSIBLE

 

/*
普通的宽度搜索题
先预处理一下map,找出每个点的着火时间,然后再次宽度搜索
如果人走到那一点的时间小于着火时间,才能入队。
*/
#include<cstdio>
#include<queue>
#include<iostream>
#include<cstring>
using namespace std;
const int M=1002;
char map[M][M];//存放地图
int newmap[M][M];//存放地图上的点的着火时间
int fang[4][2]={0,1,0,-1,1,0,-1,0};//方向数组
struct node
{
       int x;
       int y;
       int step;//记录步数
}J,F,term;
queue<node>q;
int R,C;
int main()
{
       int T,i,j,flag;
       scanf("%d",&T);
       while(T--)
       {
              while(!q.empty())q.pop();
              memset(newmap,0,sizeof(newmap));
              memset(map,0,sizeof(map));
              scanf("%d%d",&R,&C);
              for(i=1;i<=R;i++)
              {
                     for(j=1;j<=C;j++)
                     {
                            cin>>map[i][j];
                            if(map[i][j]=='J')
                            {
                                   J.x=i;
                                   J.y=j;
                                   J.step=0;
                            }
                            if(map[i][j]=='F')
                            {
                                   F.x=i;
                                   F.y=j;
                                   map[i][j]='#';
                                   q.push(F);//着火点不止一个
                            }
                     }
              }
              while(!q.empty())
              {
                     term=q.front();
                     q.pop();
                     for(i=0;i<4;i++)
                     {
                            node t;
                            t.x=term.x+fang[i][0];
                            t.y=term.y+fang[i][1];
                            if(map[t.x][t.y]==0||map[t.x][t.y]=='#'||newmap[t.x][t.y]!=0&&(newmap[t.x][t.y]<=newmap[term.x][term.y]+1))continue;
                            newmap[t.x][t.y]=newmap[term.x][term.y]+1;
                            q.push(t);
                     }
              }
              q.push(J);
              flag=0;
              while(!q.empty())
              {
                     term=q.front();
                     q.pop();
                     for(i=0;i<4;i++)
                     {
                            node t;
                            t.x=term.x+fang[i][0];
                            t.y=term.y+fang[i][1];
                            t.step=term.step+1;
                            if(map[t.x][t.y]==0){flag=1;break;}//逃出
                            if(map[t.x][t.y]=='#'||newmap[t.x][t.y]!=0&&newmap[t.x][t.y]<=t.step)continue;
                            map[t.x][t.y]='#';
                            q.push(t);
                     }
                     if(flag)break;
              }
              if(flag==1)printf("%d\n",term.step+1);
              else printf("IMPOSSIBLE\n");
       }
       return 0;
}

 

 

 

 

 

 

1
8
分享到:
评论

相关推荐

    UVaOJ-401(Palindromes).zip_401 Palindromes

    标题中的"UVaOJ-401(Palindromes)"表明这是一个关于解决UVa Online Judge(UVa OJ)上编号为401的编程挑战,该挑战的主题是"Palindromes",即回文串。回文串是指一个字符串无论从前读到后还是从后读到前都是相同的,...

    Uva 1510 - Neon Sign

    ### Uva 1510 - Neon Sign #### 问题背景与描述 在题目“Uva 1510 - Neon Sign”中,我们面对的是一个霓虹灯招牌设计问题。该霓虹灯招牌由一系列位于圆周上的角点组成,并通过发光管连接这些角点。发光管有两种...

    uva705-Slash-Maze-.rar_Slash_uva705

    【标题】"uva705-Slash-Maze-.rar_Slash_uva705" 指向的是一个在UVa Online Judge (UVa OJ) 上提交并通过的编程问题,具体为问题编号705,名为"Slash Maze"。这个压缩包很可能包含了该问题的解决方案源代码。 ...

    UVA100~200---52道题accept代码,均顺利accept过

    5. **UVA107 - Robot Walk**:该题考察图论和深度优先搜索(DFS)或广度优先搜索(BFS),求解机器人在网格上的所有可能移动路径。 6. **UVA128 - Buses**:这是一个时间表匹配问题,需要找出两路公交车在哪个站点...

    开源项目-codingsince1985-UVa#uva-online-judge-solutions-in-golang.zip

    开源项目-codingsince1985-UVa#uva-online-judge-solutions-in-golang.zip,两年来每天都在解决一个uva在线裁判问题,算起来…

    Algorithm-UVA-Solutions-in-Python.zip

    2. **搜索算法**:包括深度优先搜索(DFS)和广度优先搜索(BFS),常用于图遍历和树结构的处理,对于解决迷宫问题、八皇后问题等具有重要作用。 3. **图论算法**:如Dijkstra最短路径算法、Floyd-Warshall所有对...

    UVA133-TheDoleQueue.zip_site:www.pudn.com_uva133

    《UVA133 - 救济金发放问题:The Dole Queue》 在计算机科学领域,算法是解决问题的关键工具,特别是在处理复杂数据结构和优化问题时。UVA(University of Virginia)在线判题系统提供了丰富的算法题目供程序员挑战...

    uva532-Dungeon-Master.rar_dungeon

    《UVA532 Dungeon Master:解密游戏编程的深度探索》 在计算机科学与编程领域,UVA(University of Virginia)在线判题系统是一个深受程序员喜爱的平台,它提供了丰富的算法题目供学习者挑战。其中,编号为532的...

    tpcw-nyu-uva-client 客户端

    "tpcw-nyu-uva-client 客户端"是一个专为TPCW(Transaction Processing Performance Council Workloads)设计的应用程序,由纽约大学(NYU)和弗吉尼亚大学(UVA)共同开发。这个客户端软件主要用于模拟和评估数据库...

Global site tag (gtag.js) - Google Analytics