BFS(两点搜索) UVA 11624 Fire!

题目传送门

  1 /*
  2     BFS:首先对火搜索,求出火蔓延到某点的时间,再对J搜索,如果走到的地方火已经烧到了就不入队,直到走出边界。
  3 */
  4 /************************************************
  5 Author        :Running_Time
  6 Created Time  :2015-8-4 8:11:54
  7 File Name     :UVA_11624.cpp
  8 *************************************************/
  9
 10 #include <cstdio>
 11 #include <algorithm>
 12 #include <iostream>
 13 #include <sstream>
 14 #include <cstring>
 15 #include <cmath>
 16 #include <string>
 17 #include <vector>
 18 #include <queue>
 19 #include <deque>
 20 #include <stack>
 21 #include <list>
 22 #include <map>
 23 #include <set>
 24 #include <bitset>
 25 #include <cstdlib>
 26 #include <ctime>
 27 using namespace std;
 28
 29 #define lson l, mid, rt << 1
 30 #define rson mid + 1, r, rt << 1 | 1
 31 typedef long long ll;
 32 const int MAXN = 1e3 + 10;
 33 const int INF = 0x3f3f3f3f;
 34 const int MOD = 1e9 + 7;
 35 char maze[MAXN][MAXN];
 36 bool vis[MAXN][MAXN];
 37 int step[MAXN][MAXN];
 38 int dx[4] = {-1, 1, 0, 0};
 39 int dy[4] = {0, 0, -1, 1};
 40 int n, m;
 41
 42 bool judge_f(int x, int y)  {
 43     if (x < 1 || x > n || y < 1 || y > m || vis[x][y] || maze[x][y] == ‘#‘)    return false;
 44     return true;
 45 }
 46
 47 bool judge_j(int x, int y)  {
 48     if (x < 1 || x > n || y < 1 || y > m)   return true;
 49     return false;
 50 }
 51
 52 void BFS_F(void)  {
 53     memset (step, INF, sizeof (step));
 54     memset (vis, false, sizeof (vis));
 55     queue<pair<int, int> > Q;
 56     for (int i=1; i<=n; ++i)    {
 57         for (int j=1; j<=m; ++j)    {
 58             if (maze[i][j] == ‘F‘) {
 59                 Q.push (make_pair (i, j));  vis[i][j] = true;
 60                 step[i][j] = 0;
 61             }
 62         }
 63     }
 64     while (!Q.empty ()) {
 65         int x = Q.front ().first, y = Q.front ().second;    Q.pop ();
 66         for (int i=0; i<4; ++i) {
 67             int tx = x + dx[i], ty = y + dy[i];
 68             if (!judge_f (tx, ty))    continue;
 69             step[tx][ty] = step[x][y] + 1;
 70             Q.push (make_pair (tx, ty));    vis[tx][ty] = true;
 71         }
 72     }
 73 }
 74
 75 void BFS_J(void)    {
 76     memset (vis, false, sizeof (vis));
 77     queue<pair<int, int> > Q;
 78     for (int i=1; i<=n; ++i)    {
 79         for (int j=1; j<=m; ++j)    {
 80             if (maze[i][j] == ‘J‘)  {
 81                 Q.push (make_pair (i, j)); step[i][j] = 0; vis[i][j] = true;   break;
 82             }
 83         }
 84     }
 85     while (!Q.empty ()) {
 86         int x = Q.front ().first, y = Q.front ().second;    Q.pop ();
 87         for (int i=0; i<4; ++i) {
 88             int tx = x + dx[i], ty = y + dy[i];
 89             if (judge_j (tx, ty))    {
 90                 printf ("%d\n", step[x][y] + 1);    return ;
 91             }
 92             if (step[x][y] + 1 >= step[tx][ty] || vis[tx][ty] || maze[tx][ty] == ‘#‘) continue;
 93             Q.push (make_pair (tx, ty));    step[tx][ty] = step[x][y] + 1;  vis[tx][ty] = true;
 94         }
 95     }
 96     puts ("IMPOSSIBLE");
 97 }
 98
 99 int main(void)    {     //UVA 11624 Fire!
100     int T;  scanf ("%d", &T);
101     while (T--) {
102         scanf ("%d%d", &n, &m);
103         for (int i=1; i<=n; ++i)    {
104             scanf ("%s", maze[i] + 1);
105         }
106         BFS_F ();   BFS_J ();
107     }
108
109     return 0;
110 }
时间: 2024-10-25 05:47:09

BFS(两点搜索) UVA 11624 Fire!的相关文章

uva 11624 Fire!(多源BFS)

uva 11624 Fire! 题目大意:J在迷宫里工作,有一天迷宫起火了,火源有多处.每过一秒,火源都会向四个方向蔓延,J也会向四个方向移动,问J能不能跑出去,能的话输出他跑出去的最短时间,否则输出"IMPOSSIBLE" 解题思路:先进行一次BFS,找出每一点火焰蔓延到该处的最短时间.然后根据这张"火势图",对J的行进路线进行BFS.注意J在边缘的时候,以及没有火源的时候. #include <cstdio> #include <cstring

UVA 11624 - Fire!(BFS)

UVA 11624 - Fire! 题目链接 题意:一个迷宫,一些格子着火了,火每秒向周围蔓延,现在J在一个位置,问他能走出迷宫的最小距离 思路:BFS2次,第一次预处理每个位置着火时间,第二次根据这个再BFS一次 代码: #include <cstdio> #include <cstring> #include <queue> using namespace std; const int d[4][2] = {0, 1, 1, 0, 0, -1, -1, 0}; co

UVa 11624 Fire!(着火了!)

p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: "Times New Roman"; font-size: 10.5000pt } h3 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 1

UVa 11624 Fire!(BFS 逃离火灾)

题意   n*m的迷宫中有一些着火点  每个着火点上下左右相邻的非墙点下一秒也将成为一个着火点  Joe每秒能向相邻的点移动一步  给你所有着火点的位置和Joe的位置  问Joe逃离这个迷宫所需的最小时间 可以先一遍bfs把每个点的最早着火时间存起来   只有Joe到达该点的时间小于这个时间Joe才能走这个点   只需要对Joe所在的点为起点再来一次bfs就行了   需要注意的是开始可能有多个着火点  我开始以为只有一个着火点被坑了好久 v[i][j]第一遍bfs记录点i,j的最早着火时间  第

(简单) UVA 11624 Fire! ,BFS。

Description 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 o

UVA - 11624 - Fire! (BFS的应用)

A - Fire! Time Limit:1000MS    Memory Limit:0KB    64bit IO Format:%lld & %llu SubmitStatus Description 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

uva 11624 Fire!(BFS)

Fire! Time Limit: 1000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description 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 f

UVA 11624 Fire!(广度优先搜索)

题目大意:在一个N*M的迷宫内,J代表某人(只有一个),F代表火(可能不只一个),#代表墙,火每分钟会向四周除了墙以外的地方扩散一层,问人能否在没被火烧到 之前逃出迷宫,若能逃出输出最短时间.很明显的bfs.但由于火到达的地方人不能抵达,故需先对火进行bfs,标记后若人在火烧到之前抵达即可.最后逃出时间需要加一, 因为当时只是抵达边界,若逃出时间需加一. #include <stdio.h> #include <queue> #include <string.h> #i

BFS [uva 11624] Fire!

J - Fire! Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu 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