UVA11624——简单搜索

题目链接:https://vjudge.net/contest/168082#problem/I

题目大意:给你一个迷宫,其中有人,障碍物和火,人和火的移动速度都是一单位长度/一单位时间,火经过的地方就都是火了,人不能通过障碍物或火,火也不能通过障碍物,问你人能否逃脱?(只要到达边界就是逃脱)

这题注意一点,就是当有多个起点跑BFS时,用一个队列存储所有的起点就行。简单的BFS过程。

但为啥我的代码tuntime error辽.....

别人的代码(和我的差不多)

#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;
    }

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值