QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#212377#1970. Social DistancingZanite#AC ✓22ms6508kbC++173.2kb2023-10-13 15:25:162023-10-13 15:25:16

Judging History

This is the latest submission verdict.

  • [2023-10-13 15:25:16]
  • Judged
  • Verdict: AC
  • Time: 22ms
  • Memory: 6508kb
  • [2023-10-13 15:25:16]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;

using ll  = long long;
using pii = pair<int, int>;

#define fi       first
#define se       second
#define All(x)   x.begin(), x.end()
#define debug(x) cout << #x << " = " << (x) << '\n';

const int iINF = 1'000'000'000;
const ll INF   = 1'000'000'000'000'000'000;

const int maxN  = 523;
const int dx[]  = { 0, -1, -1, -1, 0, 1, 1,  1};
const int dy[]  = {-1, -1,  0,  1, 1, 1, 0, -1};

const int dx2[] = {0,  0, 1, -1};
const int dy2[] = {1, -1, 0,  0};

int N, M, sr, sc, er, ec;
int dist[maxN][maxN];
char grid[maxN][maxN];
bool vis[maxN][maxN];

void bfsDist() {
    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= M; j++) {
            dist[i][j] = iINF;
        }
    }

    queue<pair<pii, int>> Q;
    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= M; j++) {
            if (grid[i][j] == '*') {
                Q.push({{i, j}, 0});
                dist[i][j] = 0;
            }
        }
    }

    while (!Q.empty()) {
        auto [pos, cdist] = Q.front();
        auto [r, c] = pos;
        Q.pop();
        if (vis[r][c]) continue;

        vis[r][c] = true;
        for (int i = 0; i < 8; i++) {
            int nr = r + dx[i], nc = c + dy[i];
            if (nr < 1 || nr > N || nc < 1 || nc > M) continue;

            if (dist[nr][nc] > cdist + 1) {
                dist[nr][nc] = cdist + 1;
                Q.push({{nr, nc}, cdist + 1});
            }
        }
    }

    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= M; j++) {
            if (grid[i][j] == '#') {
                dist[i][j] = 0;
            }
        }
    }
}

bool check(int x) {
    if (dist[sr][sc] < x) return false;

    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= M; j++) {
            vis[i][j] = false;
        }
    }

    queue<pii> Q;
    Q.push({sr, sc});

    while (!Q.empty()) {
        auto [r, c] = Q.front();
        Q.pop();
        if (vis[r][c]) continue;

        vis[r][c] = true;
        for (int i = 0; i < 4; i++) {
            int nr = r + dx2[i], nc = c + dy2[i];
            if (nr < 1 || nr > N || nc < 1 || nc > M) continue;

            if (dist[nr][nc] >= x) {
                Q.push({nr, nc});
            }
        }
    }

    return vis[er][ec];
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);

    cin >> N >> M;
    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= M; j++) {
            cin >> grid[i][j];
            if (grid[i][j] == 'S') {
                sr = i; sc = j;
            }
            if (grid[i][j] == 'E') {
                er = i; ec = j;
            }
        }
    }

    bfsDist();
    // for (int i = 1; i <= N; i++) {
    //     for (int j = 1; j <= M; j++) {
    //         cout << dist[i][j] << ' ';
    //     }
    //     cout << '\n';
    // }

    int B = max(N, M);
    int L = 1, R = B + 1, ans = -1;
    while (L <= R) {
        int mid = (L + R) >> 1;
        if (check(mid)) {
            ans = mid;
            L = mid + 1;
        } else {
            R = mid - 1;
        }
    }

    if (ans == B + 1) {
        cout << "safe\n";
    } else {
        cout << ans << '\n';
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3496kb

input:

4 5
.*#..
.*..E
..##.
S....

output:

2

result:

ok single line: '2'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3456kb

input:

6 8
.......E
........
........
.....**.
........
S.......

output:

3

result:

ok single line: '3'

Test #3:

score: 0
Accepted
time: 0ms
memory: 3496kb

input:

3 3
#E#
###
#S#

output:

-1

result:

ok single line: '-1'

Test #4:

score: 0
Accepted
time: 0ms
memory: 3508kb

input:

3 3
.S.
***
.E.

output:

-1

result:

ok single line: '-1'

Test #5:

score: 0
Accepted
time: 0ms
memory: 3528kb

input:

3 3
S..
...
..E

output:

safe

result:

ok single line: 'safe'

Test #6:

score: 0
Accepted
time: 1ms
memory: 3456kb

input:

2 2
S.
.E

output:

safe

result:

ok single line: 'safe'

Test #7:

score: 0
Accepted
time: 0ms
memory: 3684kb

input:

50 50
.............##.........*....#...##....#..........
.##..........#...#............##.....#....#.....#.
.........##...#....#.......#......#...............
#..##..#.........*...#..........*..............#..
....................*..#.....#.......#........#...
...#...............#................##....

output:

3

result:

ok single line: '3'

Test #8:

score: 0
Accepted
time: 10ms
memory: 5068kb

input:

495 496
S......................................................................................................................................................................................................................................................................................................

output:

13

result:

ok single line: '13'

Test #9:

score: 0
Accepted
time: 4ms
memory: 6508kb

input:

500 500
#.*#.##.**.#...#.*.*#.#.*.*#.#.*.##*.#.*..#..*...*##.#.##.#...**#*#.#*...#.#.......#*#...#...*#.#.*#.##..##...*.#.##.*#*..#...*##..##**...*#..##...##...#....*#..##*##....#.#**.#...##..#.##..#*.*#.*.....*#...##...###.##..#**....#####..*..#...#...*.....##*#*##.#..#*.*.*.*.#..*.#*#*#**.......#....

output:

-1

result:

ok single line: '-1'

Test #10:

score: 0
Accepted
time: 22ms
memory: 5076kb

input:

500 500
....................................................................................................................................................................................S..................................................................................................................

output:

-1

result:

ok single line: '-1'

Test #11:

score: 0
Accepted
time: 6ms
memory: 5076kb

input:

500 500
*......................................................................................................................................................................................................................................................................................................

output:

8

result:

ok single line: '8'

Test #12:

score: 0
Accepted
time: 17ms
memory: 4996kb

input:

500 500
S###################################################################################################################################################################################################################################################################################################...

output:

safe

result:

ok single line: 'safe'

Test #13:

score: 0
Accepted
time: 13ms
memory: 5552kb

input:

499 499
.#.....#...#.....#.............##..............#....#.#...#..#......#......#.........................#.#.....#.....#....#..#.#........#....*...#......#..........................#...#......#..............#...*........#.......#...............................................#......................

output:

5

result:

ok single line: '5'

Test #14:

score: 0
Accepted
time: 4ms
memory: 5112kb

input:

500 500
S......................................................................................................................................................................................................................................................................................................

output:

38

result:

ok single line: '38'

Test #15:

score: 0
Accepted
time: 12ms
memory: 5100kb

input:

498 493
*......................................................................................................................................................................................................................................................................................................

output:

5

result:

ok single line: '5'