哈理工 OJ Fire Maze(2次bfs)

题意:

‘#’是墙 ,F是火,S起始点,人一秒走一格,火也是1秒扩散一格,问在给定的时间T内是否人可以逃出去。

思路:

很明显两次bfs 第一次先求出火扩散到每个格子时的 时刻。第二次在对人进行bfs的时候就多了个条件,那就是人不能走大于等于着火时刻的点。 开始竟然以为是1个火点!!!!!悲剧啊 。。。知道这个后就没啥了。。。。

AC:

/*=============================================================================
#
#      Author: liangshu - cbam 
#
#      QQ : 756029571 
#
#      School : 哈尔滨理工大学 
#
#      Last modified: 2015-09-07 21:06
#
#     Filename: hdu1015.cpp
#
#     Description: 
#        The people who are crazy enough to think they can change the world, are the ones who do ! 
=============================================================================*/
#
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int INF= 103;
char G[INF][INF];
int vis[INF][INF];
int n, m, T;
int fs, fe, ss, se;
int dir[][2] = {{0, 1}, {1, 0},{-1, 0},{0, -1}};
struct F
{
    int x, y, num;
    F(int x, int y, int num):x(x),y(y),num(num){}
};
typedef pairPLL;
mapdic;

struct S
{
    int x, y, step;
    S(int x, int y, int step):x(x),y(y),step(step){}
};
bool check1(int x, int y)
{
    if(x >= 0 && x < n && y >= 0 && y < m && !vis[x][y] && G[x][y] != '#'){
        return true;
    }
    return false;
}

bool check2(int x, int y)
{
    if(x == 0 || x == n - 1 || y == 0 || y == m - 1){
        return true;
    }
    return false;
}
void bfs1()
{
    memset(vis, 0, sizeof(vis));
     queueqq;
    for(int i = 0 ;i < n; i++)
    for(int j = 0; j < m; j++){
                if(G[i][j] == 'S'){
                    ss = i;se = j;
                }
                if(G[i][j] == 'F'){
                    vis[i][j] = 1;
                 qq.push(F(i, j, 0));
                  dic[make_pair(i,j)] = 0;
                }
    }
    while(!qq.empty()){
        F u = qq.front();
        qq.pop();
        for(int i = 0; i < 4; i++){
        int tx = u.x + dir[i][0];
        int ty = u.y + dir[i][1];
        if(check1(tx, ty)){
            vis[tx][ty] = 1;//cout<<"u.m + 1 = "<q;
    q.push(S(ss, se, 0));
    while(!q.empty()){
        S u = q.front();
        q.pop();
        if(check2(u.x, u.y)){
            return u.step + 1;
        }
        for(int i = 0; i < 4; i++){
            int tx = u.x + dir[i][0];
            int ty = u.y + dir[i][1];
            int ts = u.step + 1;
            if(check1(tx, ty) && ts < dic[make_pair(tx,ty)]){
                vis[tx][ty] = 1;
                q.push(S(tx, ty, ts));
            }
        }
    }
    return 0;
}
int main()
{
    int t;cin>>t;
    while(t--){
            dic.clear();
            scanf("%d%d%d",&n, &m, &T);
        for(int i = 0; i < n; i++){
            scanf("%s",G[i]);

        }
        bfs1();
//    for(int i = 0; i < 4;i++){
//        for(int j = 0; j < 4; j++){
//            cout<


你可能感兴趣的:(dfs,bfs,搜索)