0
Q:

Jane and the Frost Giants "c++"

#include <bits/stdc++.h>

using namespace std;

int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};
char grid[201][201];
int peek[201][201], step[201][201];
bool grid_visited[201][201];
queue<int> xfire, yfire;
int Y = 0, X = 0;

void rise_spred_BFS() {
    while (!xfire.empty()) {
        int y = yfire.front();
        int x = xfire.front();
        yfire.pop();
        xfire.pop();
        for (int i = 0; i < 4; i++) {
            int y_temp = y + dy[i], x_temp = x + dx[i];
            if (-1 < y_temp && -1 < x_temp && y_temp < Y && x_temp < X && grid[y_temp][x_temp] == '.' &&
                peek[y_temp][x_temp] < 1) {
                yfire.push(y_temp);
                xfire.push(x_temp);
                peek[y_temp][x_temp] = peek[y][x] + 1;
            }
        }
    }
}


int run_BFS(int y, int x) {
    if (y == 0 || x == 0 || y == Y - 1 || x == X - 1) {
        return 1;
    }
    step[y][x] = 1;
    queue<int> Yfire, Xfire;
    Yfire.push(y);
    Xfire.push(x);
    grid_visited[y][x] = true;
    while (!Yfire.empty()) {
        int yy = Yfire.front(), xx = Xfire.front();
        Yfire.pop();
        Xfire.pop();
        for (int i = 0; i < 4; i++) {
            int y_temp = yy + dy[i], x_temp = xx + dx[i];
            if (-1 < y_temp && -1 < x_temp && y_temp < Y && x_temp < X && !grid_visited[y_temp][x_temp] && (peek[y_temp][x_temp] == 0 || step[yy][xx] + 1 < peek[y_temp][x_temp]) && grid[y_temp][x_temp] == '.') {
                if (y_temp == 0 || x_temp == 0 || y_temp == Y - 1 || x_temp == X - 1) {
                    return step[yy][xx] + 1;
                }
                Yfire.push(y_temp);
                Xfire.push(x_temp);
                step[y_temp][x_temp] = step[yy][xx] + 1;
                grid_visited[y_temp][x_temp] = true;
            }
        }
    }
    return -1;
}

bool found;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int till;
    cin >> till;
    for (int z = 0; z < till; z++) {
        cin >> Y >> X;
        found = false;
        int gy = 0, gx = 0, fy = 0, fx = 0;
        memset(peek, 0, sizeof(peek));
        for (int y = 0; y < Y; y++) {
            cin >> grid[y];
            for (int x = 0; x < X; x++) {
                if (grid[y][x] == 'F') {
                    yfire.push(y);
                    xfire.push(x);
                    peek[y][x] = 1;
                } else if (!found && grid[y][x] == 'J') {
                    gy = y;
                    gx = x;
                    found = true;
                }
            }
        }
        rise_spred_BFS();
        memset(grid_visited, false, sizeof(grid_visited));
        memset(step, 0, sizeof(step));
        int answer = run_BFS(gy, gx);
        if (answer == -1) {
            cout << "Case " << (z + 1) << ": IMPOSSIBLE" << endl;
        } else
            cout << "Case " << (z + 1) << ": " << answer << endl;
    }
    return 0;
}
0

New to Communities?

Join the community