[UVa11624]Fire!(BFS)

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2671

题意:一个人在树林里要到出口,同时有几个火点在扩展。问这个人能不能跑出树林,最少多少步能跑出去。

先扩展人,再扩展火。在同一个队列里扩展,所以要记下接下来要扩展几个人或者火点。

  1 #include <algorithm>
  2 #include <iostream>
  3 #include <iomanip>
  4 #include <cstring>
  5 #include <climits>
  6 #include <complex>
  7 #include <fstream>
  8 #include <cassert>
  9 #include <cstdio>
 10 #include <bitset>
 11 #include <vector>
 12 #include <deque>
 13 #include <queue>
 14 #include <stack>
 15 #include <ctime>
 16 #include <set>
 17 #include <map>
 18 #include <cmath>
 19
 20 using namespace std;
 21
 22 typedef pair<int, int> PII;
 23 typedef struct P {
 24     int x;
 25     int y;
 26     int s;
 27     P() {}
 28     P(int xx, int yy, int ss) : x(xx), y(yy), s(ss) {}
 29 }P;
 30
 31 const int inf = 0x7f7f7f;
 32 const int maxn  = 1010;
 33 const int dx[4] = {0, 0, 1, -1};
 34 const int dy[4] = {1, -1, 0, 0};
 35
 36 int r, c, jx, jy;
 37 vector<PII> fir;
 38 int ans;
 39 int vis[maxn][maxn];
 40 char G[maxn][maxn];
 41
 42 inline int min(int x, int y) {
 43     return x < y ? x : y;
 44 }
 45
 46 bool judge(int x, int y) {
 47     if(!vis[x][y] && G[x][y] == ‘.‘ && x >= 0 && x < r && y >= 0 && y < c) {
 48         return 1;
 49     }
 50     return 0;
 51 }
 52
 53 bool success(int x, int y) {
 54     if(G[x][y] == ‘J‘ &&
 55       (x == 0 || x == r - 1 || y == 0 || y == c - 1)) {
 56         return 1;
 57     }
 58     return 0;
 59 }
 60
 61 void init() {
 62     memset(vis, 0, sizeof(vis));
 63     memset(G, 0, sizeof(G));
 64     ans = inf;
 65     fir.clear();
 66 }
 67
 68 void bfs() {
 69     P q[maxn*maxn];
 70     int front = 0, tail = 0;
 71     int fcnt = 0, jcnt = 1, tmp = 0;
 72     for(int i = 0; i < fir.size(); i++) {
 73         q[tail++] = P(fir[i].first, fir[i].second, 0);
 74         vis[fir[i].first][fir[i].second] = 1;
 75         fcnt++;
 76     }
 77     q[tail++] = P(jx, jy, 0); vis[jx][jy] = 1;
 78     while(front < tail) {
 79         for(int k = 0; k < fcnt; k++) {
 80             P f = q[front++];
 81             for(int i = 0; i < 4; i++) {
 82                 int xx = f.x + dx[i];
 83                 int yy = f.y + dy[i];
 84                 if(judge(xx, yy) && G[xx][yy] != ‘F‘) {
 85                     vis[xx][yy] = 1;
 86                     if(G[xx][yy] != ‘J‘) {
 87                         G[xx][yy] = ‘F‘;
 88                     }
 89                     q[tail++] = P(xx, yy, f.s+1); tmp++;
 90                 }
 91             }
 92         }
 93         fcnt = tmp; tmp = 0;
 94         for(int k = 0; k < jcnt; k++) {
 95             P j = q[front++];
 96             if(success(j.x, j.y)) {
 97                 ans = min(ans, j.s);
 98             }
 99             for(int i = 0; i < 4 ; i++) {
100                 int xx = j.x + dx[i];
101                 int yy = j.y + dy[i];
102                 if(judge(xx, yy) && G[xx][yy] != ‘F‘) {
103                     vis[xx][yy] = 1;
104                     if(G[xx][yy] != ‘F‘) {
105                         G[xx][yy] = ‘J‘;
106                     }
107                     q[tail++] = P(xx, yy, j.s+1); tmp++;
108                 }
109             }
110         }
111         jcnt = tmp; tmp = 0;
112     }
113 }
114
115 int main() {
116     int T;
117     scanf("%d", &T);
118     while(T--) {
119         init();
120         scanf("%d %d", &r, &c);
121         for(int i = 0; i < r; i++) {
122             scanf("%s", G[i]);
123         }
124         for(int i = 0; i < r; i++) {
125             for(int j = 0; j < c; j++) {
126                 if(G[i][j] == ‘J‘) {
127                     jx = i; jy = j;
128                 }
129                 if(G[i][j] == ‘F‘) {
130                     fir.push_back(PII(i, j));
131                 }
132             }
133         }
134         bfs();
135         if(ans == inf) {
136             printf("IMPOSSIBLE\n");
137         }
138         else {
139             printf("%d\n", ans+1);
140         }
141     }
142     return 0;
143 }
时间: 2024-08-10 23:27:10

[UVa11624]Fire!(BFS)的相关文章

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

pots(BFS)

D - Pots Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: Input On the first and

USACO抓牛catchcow (bfs)

这题是黄巨大出的比赛题. http://poj.org/problem?id=3278 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤

hdu 1728 逃离迷宫 (BFS)

逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14376    Accepted Submission(s): 3458 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地方

牛汇(BFS)入金具体流程(图文指导)

牛汇开户流程:bfsforex牛汇入金教程 所谓入金,也就是充值的意思,必须充钱到平台才能进行外汇交易.首先,我们先登录bfsforex牛汇官方网站,在交易办公室功能区域下面,点击账户入金: 为您提供中国各大银行的网银支付解决方案,支持人民币支付,和信用卡入金,入金是实时到账的. 牛汇(BFS)入金具体流程(图文指导),布布扣,bubuko.com

URAL 1930 Ivan&#39;s Car(BFS)

Ivan's Car Time limit: 1.5 secondMemory limit: 64 MB The world is in danger! Awful earthquakes are detected all over the world. Houses are destroyed, rivers overflow the banks, it is almost impossible to move from one city to another. Some roads are

【判重+广搜(bfs)】魔板

判重+广搜(bfs)]魔板 Time Limit: 1000MS Memory Limit: 32768KB Special Judge 有一个两行四列的魔板,每个格子里有一个1到8的数字(数字唯一),现在我们可以对魔板进行以下操作: 1.交换两行的数字. 2.将第一列移到第二列,第二列到第三列,第三列到第四列,第四列到第一列. 3.将中间四个数顺时针转一次. 现给你初始状态,我末状态请你用最小的步数将它从初始状态变到末状态. 输入: 前两行,每行4个数表示初状态. 后两行,每行4个数表示末状态

[LeetCode] Binary Tree Zigzag Level Order Traversal(bfs)

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zig

USACO Mother&amp;#39;s Milk(bfs)

a=9MvljJDNdls&S=milk3">题目请点我 题解: 水杯倒水的问题非常经典,套路也是一样的,bfs找出全部状态. 这道题的关键在于每次都应该进行六次的倒水尝试,细心一点.PS:三维数组表示状态真的非常方便. 代码实现: /* ID: eashion LANG: C++ TASK: milk3 */ #include <iostream> #include <cstdio> #include <cstdlib> #include &l