zoukankan      html  css  js  c++  java
  • 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
    ", 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 }
    编译人生,运行世界!
  • 相关阅读:
    Windows Phone 的控件倾斜效果
    在framework4.0 3.5中反序列化 解析JSON
    PhpStorm2.0、3.0,5.0注册码到2112年
    Windows Phone(wp7)系统长按的秘密
    配置ethereal编译环境
    复习C++:引用的一些用法
    转载
    Stay hungry,Stay foolish
    C++:继承的一点注意事项
    纯虚函数
  • 原文地址:https://www.cnblogs.com/Running-Time/p/4703122.html
Copyright © 2011-2022 走看看