UVA 11624 BFS + 技巧

题目大意:在迷宫内,J表示人,F表示火,火可能不止一处,但是人只有一个,火会向上下左右蔓延,蔓延速度和人移动速度一至(每次一格,但是可以同时向四个方向)问是否可以成功逃出迷宫,不能输出IMPOSSIBLE,能的画话输出最短路径。

 

思路一:

两次BFS,首先把开始的F(火)入队,然后进行BFS 并用times数组记录到达每个位置的时间(初始化为INF,不可达则为INF),然后进行逃跑路线的BFS,当满足基本条件(不越界,未被访问),再满足到达某点时间小于该点的times,则说明在火到达之前,到此位置。如此进行,知道到达边界或者队列为空也没到达(IMPOSSIBLE);

#include<iostream>
#include<stdio.h>
#include<vector>
#include<map>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<set>
#include<queue>
#include<cmath>
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1005;
int dir[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};
int n,m;
int st_x,st_y;
bool vis[N][N];
int times[N][N];
int step[N][N];
char a[N][N];
struct P{
    int x,y,s;
    P(){}
    P(int px,int py,int ps):x(px),y(py),s(ps){}
};
queue<P> q;


bool check(int x, int y){
    if(x < 0 || x >= n || y < 0 || y >= m)
        return false;
    if(vis[x][y])
        return false;
    return a[x][y] == '.';
}


void clear_q(){
    while(!q.empty())
        q.pop();
}

void bfs_time(){
    while(!q.empty()){
        P cur = q.front();
        q.pop();
        for(int i = 0; i < 4; ++i){
            int nx = cur.x + dir[i][0];
            int ny = cur.y + dir[i][1];
            if(check(nx,ny)){
                vis[nx][ny] = true;
                times[nx][ny] = times[cur.x][cur.y] + 1;
                q.push(P(nx,ny,times[nx][ny]));
            }
        }
    }
}

int bfs(int x, int y){
    step[x][y] = 0;
    q.push(P(x,y,0));
    vis[x][y] = true;
    while(!q.empty()){
        P cur = q.front();
        q.pop();
        if(cur.x == 0 || cur.x == n-1 || cur.y == 0 || cur.y == m-1){
            return step[cur.x][cur.y] + 1;
        }
        for(int i = 0; i < 4; ++i){
            int nx = cur.x + dir[i][0];
            int ny = cur.y + dir[i][1];
            if(check(nx,ny) && step[cur.x][cur.y] + 1 < times[nx][ny]){
                step[nx][ny] = step[cur.x][cur.y] + 1;
                vis[nx][ny] = true;
                q.push(P(nx,ny,step[nx][ny]));
            }
        }
    }
    return -1;
}

int main()
    {
        ios::sync_with_stdio(false);
        int t;
        cin >> t;
        while(t--){
            cin >> n >> m;
            memset(times,INF,sizeof(times));
            memset(vis,false,sizeof(vis));
            for(int i = 0; i < n; ++i){
                for(int j = 0; j < m; ++j){
                    cin >> a[i][j];
                    if(a[i][j] == 'J'){
                        st_x = i;
                        st_y = j;
                    }
                    if(a[i][j] == 'F'){
                        vis[i][j] = true;
                        q.push(P(i,j,0));
                        times[i][j] = 0;
                    }
                }
            }
            bfs_time();
            clear_q();

            memset(vis,false,sizeof(vis));
            int ans = bfs(st_x,st_y);
            if(ans == -1){
                cout << "IMPOSSIBLE" <<endl;
            }
            else{
                cout << ans << endl;
            }
        }


        return 0;
    }

 

思路二:

对逃跑的BFS 每走一步然后自动蔓延火的位置(用fire数组标记),这里需要在每一步完全扩展完成的时候才能蔓延火

但是这种思路由于蔓延次数可能达到1000次,会超时,但是思路挺好的,这里也记录下来。

#include<iostream>
#include<stdio.h>
#include<vector>
#include<map>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<set>
#include<queue>
#include<cmath>
#define INF 0x3f3f3f3f
using namespace std;
const int N = 100000;
struct P{
    int x,y;
    int pre;
    P(){}
    P(int px,int py):x(px),y(py),pre(-1){}
    P(int px,int py,int ppre):x(px),y(py),pre(ppre){}
};
int n,m;
char a[1005][1005];
int step[1005][1005];
bool vis[1005][1005];
bool fire[1005][1005];
bool book[1005][1005];
P q[N];
int st_x,st_y;
int dir[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};

bool check(int x, int y){
    if(x <0 || x >=n || y <0 ||y >= m)
        return false;
    if(vis[x][y])
        return false;
    if(fire[x][y])
        return false;
    return a[x][y] == '.';
}


void fireSpread(){
    memset(book,false,sizeof(book));
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < m; ++j){
            if(fire[i][j]  && !book[i][j]){
                for(int k = 0; k < 4; ++k){
                    int nx = i + dir[k][0];
                    int ny = j + dir[k][1];
                    if(nx >= 0&&nx < n && ny >=0 && ny <m && !fire[nx][ny] && a[nx][ny] != '#'){
                        book[nx][ny] = true;
                        fire[nx][ny] = true;
                    }
                }
            }
        }
    }
}

void output(){
    cout << "\nfire --------: "<<endl;
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < m; ++j){
            if(fire[i][j])
                cout << "("<<i <<"," << j << ")" <<endl;
        }
    }
    cout << "end fire --------------"<<endl;
}

int bfs(int x,int y){
    memset(vis,false,sizeof(vis));
    memset(step,INF,sizeof(step));
    step[x][y] = 0;
    int tail  = 0,head = 0;
    q[tail++] = P(x,y,-1);
    vis[x][y] = true;
    int pre = -1;
    while(head != tail){
        P cur = q[head++];
//        cout << "cur " << "("<<cur.x <<"," << cur.y <<")" <<endl;
//        output();
        if(cur.pre != pre){
            pre = cur.pre;
            fireSpread();
        }
        if(fire[cur.x][cur.y])
            continue;
        if(cur.x == n-1 || cur.x == 0 || cur.y == 0 || cur.y == m-1){
            return step[cur.x][cur.y] + 1;
        }

        for(int i = 0; i < 4; ++i){
            int nx = cur.x + dir[i][0];
            int ny = cur.y + dir[i][1];
            if(check(nx,ny)){
                vis[nx][ny] = true;
                step[nx][ny] = step[cur.x][cur.y] + 1;
                q[tail++] = P(nx,ny,head-1);
            }
        }

    }
    return -1;
}

int main()
    {
        ios::sync_with_stdio(false);
        int t;
        cin >> t;
        while(t--){
            cin >> n >> m;
            memset(fire,0,sizeof(fire));
            for(int i = 0; i < n; ++i){
                for(int j = 0; j < m; ++j){
                    cin >> a[i][j];
                    if(a[i][j] == 'J'){
                        st_x = i;
                        st_y = j;
                    }
                    if(a[i][j] == 'F'){
                        fire[i][j] = true;
                    }
                }
            }
//            cout << "st " << st_x << " " << st_y <<endl;
            int ans = bfs(st_x,st_y);
            if(ans == -1){
                cout << "IMPOSSIBLE" <<endl;
            }
            else{
                cout << ans << endl;
            }
        }

        return 0;
    }

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值