Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

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

PREVIOUS NEXT
Code Example
Typescript :: why are lower case alphabets more preffered in html 
Typescript :: nest js decorator 
Typescript :: count number of elements in multi-dimensional array python 
Typescript :: typescript cast to parent type 
Typescript :: keynote Invite multiple users to make edits to the same document: 
Typescript :: delete in typescript with a toaster notification 
Typescript :: vim show different parts of same file 
Typescript :: site:community.nxp.com dts gpio output high active 
Typescript :: typescript allow object subset of interface 
Typescript :: how to access contents of an array from another class in java 
Typescript :: running same tests against different browsers 
Typescript :: typescript different types support 
Typescript :: how to convert js to ts 
Typescript :: npm run multiple scripts sequentially 
Typescript :: to move a directory with its contents in terminal in linux 
Typescript :: move between points in godot 
Typescript :: The State pattern allows an object to change its behavior when its internal state changes 
Typescript :: post data 
Typescript :: How to render Header on all pages except one 
Typescript :: typescript import variable from another file 
Typescript :: adding html in typescript 
Typescript :: real time charts in flutter 
Typescript :: What are the components of the environment? Explain it along with the examples. 
Typescript :: UpdateTable operation with the GlobalSecondaryIndexUpdates parameter 
Cpp :: c++ addition 
Cpp :: how to print list in c++ 
Cpp :: flake8 max line length 
Cpp :: const iterator c++ 
Cpp :: how to shut down windows in c++ 
Cpp :: inserting at start in vector c++ 
ADD CONTENT
Topic
Content
Source link
Name
7+2 =