求大佬指点,好像死循环了
  • 板块UVA11624 Fire!
  • 楼主Qiushunan
  • 当前回复1
  • 已保存回复1
  • 发布时间2023/3/7 00:59
  • 上次更新2023/10/23 22:48:46
查看原帖
求大佬指点,好像死循环了
859998
Qiushunan楼主2023/3/7 00:59
#include <bits/stdc++.h>
#define N 1005
#define M 20005
typedef long long ll;
using namespace std;
int r, c, key[1005][1005], x, y, fx[1005], fy[1005]; // fx,fy记录火的初始坐标,key是火到达的时间,初始全为最大
char a[1005][1005];                                  // 记录地图
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, -1, -1};
bool vis[1005][1005]; // 是否走过格子
bool get_ans = false;
int step = 0;
queue<pair<int, int>> q;
void fire_bfs(int x, int y)
{
    q.push(make_pair(x, y));
    while (!q.empty())
    {
        int xx = q.front().first, yy = q.front().second;
        q.pop();
        for (int i = 0; i < 4; i++)
        {
            int u = xx + dx[i], v = yy + dy[i];
            if (u < 1 || u > c || v < 1 || v > r)
                continue;
            q.push(make_pair(u, v));
            if (key[u][v] > key[x][y] + 1)
                key[u][v] = key[x][y] + 1;
        }
    }
}

void joe_bfs(int x, int y)
{

    q.push(make_pair(x, y));
    while (!q.empty())
    {
        int xx = q.front().first, yy = q.front().second;
        q.pop();
        if (xx == c || xx == 1 || yy == r || yy == 1)
        {
            get_ans = true;
            break;
        }
        for (int i = 0; i < 4; i++)
        {
            int u = xx + dx[i], v = yy + dy[i];
            if (step + 1 < key[u][v] && !vis[u][v])
            {
                vis[u][v] = true;
                step++;
                q.push(make_pair(u, v));
            }
        }
    }
}
int main()
{
    get_ans = false;
    step = 0;
    int cas;
    cin >> cas;
    while (cas--)
    {
        cin >> r >> c;
        int k = 1; // 火的数量
        for (int i = 1; i <= r; i++)
        {
            for (int j = 1; j <= c; j++)
            {
                key[i][j] = r * c + 1;
                vis[i][j] = false;
            }
        }
        for (int i = 1; i <= r; i++)
        {
            for (int j = 1; j <= c; j++)
            {

                cin >> a[i][j];
                if (a[i][j] == 'J')
                {
                    x = i;
                    y = j;
                }
                if (a[i][j] == 'F')
                {
                    fx[k] = i;
                    fy[k++] = j; // 火的数量+1
                }
                if (a[i][j] == '#' || a[i][j] == 'F')
                    key[i][j] = 0;
            }
        }
        for (int i = 1; i <= k; i++)
            fire_bfs(fx[i], fy[i]);
        joe_bfs(x, y);
        if (!get_ans)
        {
            cout << "IMPOSSIBLE" << endl;
        }
        else
        {
            cout << step + 1 << endl;
        }
    }
    return 0;
}

不知道哪里做错了,求大佬帮忙Orz

2023/3/7 00:59
加载中...