• UVA11624(bfs)


    题意:给一张图,有火源,有障碍物,剩下的是道路,火源在下一分钟能够让上下左右四个方向的道路也着火,告诉人的位置,问最短时间能逃出去的时间是多少;

    思路:一个bfs用来求出所有的火源能蔓延到的地方,另一个bfs求最短路,本来我以为bfs只能求最短路;

    超级源:有多个源头的时候,就把所有的都压进去,每个源头能蔓延到的地方都看作是新的源头,继续压进去,每个只能蔓延4个方向,直到空;

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstdlib>
      4 #include <cmath>
      5 #include <queue>
      6 #include <algorithm>
      7 #include <vector>
      8 #include <cstring>
      9 #include <stack>
     10 #include <cctype>
     11 #include <utility>
     12 #include <map>
     13 #include <string>
     14 #include <climits>
     15 #include <set>
     16 #include <string>
     17 #include <sstream>
     18 #include <utility>
     19 #include <ctime>
     20 using namespace std;
     21 const int MAXN(1010);
     22 const int MAXM(1000010);
     23 int fire[MAXN][MAXN];
     24 char g[MAXN][MAXN];
     25 bool vis[MAXN][MAXN];
     26 int fx[MAXM], fy[MAXM];
     27 int R, C, SX, SY;
     28 struct NODE
     29 {
     30     int x, y, val;
     31     NODE()
     32     {}
     33     NODE(int tx, int ty, int td): x(tx), y(ty), val(td)
     34     {}
     35 };
     36 
     37 int ok(int nx,int ny)
     38 {
     39     if(nx >= 1 && nx <= R && ny >= 1 && ny <= C )
     40         return 1;
     41     return 0;
     42 }
     43 int move_x[4] = {0, -1, 0, 1};
     44 int move_y[4] = {-1, 0, 1, 0};
     45 
     46 void bfs1(int n)///多个起点,超级源
     47 {
     48     queue<NODE> q;
     49     memset(fire+1, -1, sizeof(fire));
     50     for(int i = 0; i < n; ++i)///起点都压进去
     51     {
     52         q.push(NODE(fx[i], fy[i], 0));
     53         fire[fx[i]][fy[i]] = 0;
     54     }
     55     NODE cur;
     56     while(!q.empty())
     57     {
     58         cur = q.front();///从一个起点开始,然后四个方向扩散
     59         q.pop();
     60         for(int i = 0; i < 4; ++i)
     61         {
     62             int nx = cur.x+move_x[i], ny = cur.y+move_y[i];
     63             if(ok(nx,ny)&& g[nx][ny] != '#' && fire[nx][ny] == -1)
     64             {
     65                 fire[nx][ny] = cur.val+1;
     66                 q.push(NODE(nx, ny, cur.val+1));
     67                 ///重新做一个新的起点
     68             }
     69         }
     70     }
     71 }
     72 
     73 int bfs2()
     74 {
     75     queue<NODE> q;
     76     memset(vis+1, false, sizeof(vis[0])*R);
     77     q.push(NODE(SX, SY, 0));
     78     vis[SX][SY] = true;
     79     NODE cur;
     80     while(!q.empty())
     81     {
     82         cur = q.front();
     83         q.pop();
     84         if(cur.x == 1 || cur.y == 1 || cur.x == R || cur.y == C)
     85             return cur.val+1;
     86         for(int i = 0; i < 4; ++i)
     87         {
     88             int nx = cur.x+move_x[i], ny = cur.y+move_y[i];
     89             if(ok(nx,ny)&& g[nx][ny] != '#' && !vis[nx][ny] && (fire[nx][ny] == -1 || cur.val+1 < fire[nx][ny]))
     90             {
     91                 vis[nx][ny] = true;
     92                 q.push(NODE(nx, ny, cur.val+1));
     93             }
     94         }
     95     }
     96     return -1;
     97 }
     98 
     99 int main()
    100 {
    101     int T;
    102     scanf("%d", &T);
    103     while(T--)
    104     {
    105         scanf("%d%d", &R, &C);
    106         int count = 0;
    107         for(int i = 1; i <= R; ++i)
    108         {
    109             scanf("%s", g[i]+1);
    110             for(int j = 1; j <= C; ++j)
    111             {
    112                 if(g[i][j] == 'J')
    113                 {
    114                     SX = i;
    115                     SY = j;
    116                 }
    117                 if(g[i][j] == 'F')
    118                 {
    119                     fx[count] = i;
    120                     fy[count++] = j;
    121                 }
    122             }
    123         }
    124         bfs1(count);
    125         int ans = bfs2();
    126         if(ans == -1)
    127             printf("IMPOSSIBLE
    ");
    128         else
    129             printf("%d
    ", ans);
    130     }
    131     return 0;
    132 }
    View Code
  • 相关阅读:
    简便的将DataSet导入到数据库中
    数据类型的小小研究:Access与SQL Server的数据类型
    【jxust acm 20120708】
    【D ECJTU_ACM 11级队员2012年暑假训练赛(2)】
    【hdu 2101 A + B Problem Too】
    【hdu 1014】
    【hdu 1164 Eddy's research I】
    【开始,安全编程】
    【hdu 1285 确定比赛名次】
    【hdu 1163 Eddy's digital Roots 】
  • 原文地址:https://www.cnblogs.com/ACMERY/p/4495970.html
Copyright © 2020-2023  润新知