UVa-11624

双BFS,现在觉得可以换用结构体,这样可以用一个队列,在结构体成员中标明该元素是火还是人即可。

我还是开了两个队列

#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <stack>
#include <vector>
#include <queue>
#include <utility>
#include <functional>
#include <ctime>
#include <climits>
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
using namespace std;
typedef long long lovelive;
typedef pair<int, int> P;
const double pai = acos(-1.0);
const int inf = INT_MAX;
const double gold = (sqrt(5.0) + 1) / 2.0;
int map_arr[1117][1117], dis_arr[1117][1117][2], r, c;
bool vis_arr[1117][1117][2];
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}, sx, sy, step;
queue <P> fire_que, joe_que;

void init()
{
    while(!fire_que.empty())
    {
        fire_que.pop();
    }
    while(!joe_que.empty())
    {
        joe_que.pop();
    }
    sx = sy = 0;
    step = inf;
    memset(map_arr, 0, sizeof(map_arr));
    memset(vis_arr, 0, sizeof(vis_arr));
    memset(dis_arr, -1, sizeof(dis_arr));
return ;
}

void input_data()
{
    int i, j, k;
    scanf("%d %d", &r, &c);
    getchar();
    for(i = 0; i < r; ++i)
    {
        for(j = 0; j < c; ++j)
        {
            scanf("%c", &map_arr[i][j]);
            if(map_arr[i][j] == 'J')
            {
                sx = i, sy = j;
                dis_arr[i][j][1] = 0;
                vis_arr[i][j][1] = 1;
                joe_que.push(P(i, j));
                map_arr[i][j] = '.';
            }
            if(map_arr[i][j] == 'F')
            {
                dis_arr[i][j][0] = 0;
                vis_arr[i][j][0] = 1;
                fire_que.push(P(i, j));
                map_arr[i][j] = '.';
            }
        }
        getchar();
    }
return ;
}

void bfs()
{
    int i, j, k;
    P tmp;
    while(!fire_que.empty())
    {
        tmp = fire_que.front();
        fire_que.pop();
        int x = tmp.first, y = tmp.second;
        for(i = 0; i < 4; ++i)
        {
            int dx = x + dir[i][0];
            int dy = y + dir[i][1];
            if(0 <= dx && dx < r && 0 <= dy && dy < c && map_arr[dx][dy] == '.' && vis_arr[dx][dy][0] == 0)
            {
                vis_arr[dx][dy][0] = 1;
                dis_arr[dx][dy][0] = dis_arr[x][y][0] + 1;
                fire_que.push(P(dx, dy));
            }
        }
    }
    while(!joe_que.empty())
    {
        tmp = joe_que.front();
        joe_que.pop();
        int x = tmp.first, y = tmp.second;
        for(i = 0; i < 4; ++i)
        {
            int dx = x + dir[i][0];
            int dy = y + dir[i][1];
            if(0 <= dx && dx < r && 0 <= dy && dy < c &&  vis_arr[dx][dy][1] == 0 && map_arr[dx][dy] == '.')
            {
                vis_arr[dx][dy][1] = 1;
                dis_arr[dx][dy][1] = dis_arr[x][y][1] + 1;
                joe_que.push(P(dx, dy));
            }
        }
    }
return ;
}

int check(int x, int y)
{
    if(map_arr[x][y] != '.')  return inf;
    if(vis_arr[x][y][1] == 0) return inf;
    if(dis_arr[x][y][1] < dis_arr[x][y][0] || vis_arr[x][y][0] == 0)
    {
        return dis_arr[x][y][1] + 1;
    }
    else return inf;
}

void solve()
{
    bfs();
    int i;
    for(i = 0; i < r; ++i)
    {
        step = min(step, check(i, 0));
        step = min(step, check(i, c - 1));
    }
    for(i = 0; i < c; ++i)
    {
        step = min(step, check(0, i));
        step = min(step, check(r - 1, i));
    }
return ;
}

void output_data()
{
    if(step == inf)
    {
        printf("IMPOSSIBLE\n");
    }
    else printf("%d\n", step);
}

int main(int argc, char * argv[])
{
    int n, m, i, j, k;
    int t;
    scanf("%d", &t);
    while(t--)
    {
        init();
        input_data();
        solve();
        output_data();
    }
return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值