QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#55778#1970. Social DistancingThree_SanninAC ✓116ms8568kbC++3.1kb2022-10-15 02:00:022022-10-15 02:00:03

Judging History

This is the latest submission verdict.

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-10-15 02:00:03]
  • Judged
  • Verdict: AC
  • Time: 116ms
  • Memory: 8568kb
  • [2022-10-15 02:00:02]
  • Submitted

answer

// Hey there.. I love you <3

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/numeric>

using namespace std;
using namespace __gnu_pbds;

using ll = long long;
using ld = long double;
using L = __int128;

#define fast cin.tie(0), cin.sync_with_stdio(0)
FILE *stream;
#define input(x) freopen_s(&stream, (x), "r", stdin)
#define output(x) freopen_s(&stream, (x), "w", stdout)
#define all(x) x.begin(), x.end()
#define sz(x) ((int)(x).size())
#define maxz(x, y) x = max(x, y)
#define minz(x, y) x = min(x, y)
#define modz(x) x = (x % mod + mod) % mod
#define mp(x, y) make_pair(x, y)
#define f first
#define s second
#define pii pair<int, int>
#define ordered_set(x) tree<x, null_type,less<x>, rb_tree_tag,tree_order_statistics_node_update> //less_equal
#define eps 1e-6
mt19937 rnd(time(nullptr));

const int N = 500 + 5, M = 500, mod = 998244353;
const ld pi = acos(-1);

#define point pii

int n, m, a[N][N], dist[N][N];
pii best[N][N], st, en, neu = {-1, -1};

vector<point> e(point u) {
    vector<point> ret;
    for (int i = u.f - 1; i <= u.f + 1; ++i)
        for (int j = u.s - 1; j <= u.s + 1; ++j)
            if (i && i <= n && j && j <= m)
                ret.push_back({i, j});
    return ret;
}

vector<point> e2(point u) {
    vector<point> ret;
    for (int i = u.f - 1; i <= u.f + 1; ++i)
        for (int j = u.s - 1; j <= u.s + 1; ++j)
            if (abs(i - u.f) + abs(j - u.s) < 2 && a[i][j] && (i != u.f || j != u.s))
                ret.push_back({i, j});
    return ret;
}

void hi(int tc) {
    
    cin >> n >> m;
    char c;
    queue<point> q;
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            cin >> c, a[i][j] = !(c == '#' || c == '*');
            if (c == '*') dist[i][j] = 0, q.push({i, j});
            if (c == 'S') st = {i, j};
            if (c == 'E') en = {i, j};
        }
    }
    
    while (!q.empty()) {
        auto u = q.front();
        q.pop();
        for (auto v: e(u)) if (dist[v.f][v.s] > dist[u.f][u.s] + 1) dist[v.f][v.s] = dist[u.f][u.s] + 1, q.push(v);
    }
    
    best[st.f][st.s] = {dist[st.f][st.s], 0};
    
    priority_queue<pair<pii, pii>> pq;
    
    pq.push({best[st.f][st.s], st});
    
    while (!pq.empty()) {
        auto it = pq.top();
        pq.pop();
        pii me = it.f, u = it.s;
        if (me != best[u.f][u.s]) continue;
        for (auto v: e2(u)) {
            if (best[v.f][v.s] < mp(min(me.f, dist[v.f][v.s]), me.s - 1)) {
                best[v.f][v.s] = mp(min(me.f, dist[v.f][v.s]), me.s - 1), pq.push({best[v.f][v.s], v});
            }
        }
        
    }
    if (best[en.f][en.s] == neu) cout << -1 << endl;
    else if (best[en.f][en.s].f > 1e8) cout << "safe" << endl;
    else cout << best[en.f][en.s].f << endl;
    
}


void init() {
    fast;
    for (int i = 0; i < N; ++i) for (int j = 0; j < N; ++j) dist[i][j] = 1e9, best[i][j] = neu;
}

int32_t main() {
    init();
    
    int t = 1;
    //cin >> t;
    for (int tc = 1; tc <= t; ++tc) hi(tc);
    
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 6628kb

input:

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

output:

2

result:

ok single line: '2'

Test #2:

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

input:

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

output:

3

result:

ok single line: '3'

Test #3:

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

input:

3 3
#E#
###
#S#

output:

-1

result:

ok single line: '-1'

Test #4:

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

input:

3 3
.S.
***
.E.

output:

-1

result:

ok single line: '-1'

Test #5:

score: 0
Accepted
time: 3ms
memory: 6504kb

input:

3 3
S..
...
..E

output:

safe

result:

ok single line: 'safe'

Test #6:

score: 0
Accepted
time: 2ms
memory: 6680kb

input:

2 2
S.
.E

output:

safe

result:

ok single line: 'safe'

Test #7:

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

input:

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

output:

3

result:

ok single line: '3'

Test #8:

score: 0
Accepted
time: 54ms
memory: 7704kb

input:

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

output:

13

result:

ok single line: '13'

Test #9:

score: 0
Accepted
time: 45ms
memory: 8568kb

input:

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

output:

-1

result:

ok single line: '-1'

Test #10:

score: 0
Accepted
time: 65ms
memory: 7760kb

input:

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

output:

-1

result:

ok single line: '-1'

Test #11:

score: 0
Accepted
time: 58ms
memory: 7764kb

input:

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

output:

8

result:

ok single line: '8'

Test #12:

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

input:

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

output:

safe

result:

ok single line: 'safe'

Test #13:

score: 0
Accepted
time: 116ms
memory: 7968kb

input:

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

output:

5

result:

ok single line: '5'

Test #14:

score: 0
Accepted
time: 54ms
memory: 7708kb

input:

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

output:

38

result:

ok single line: '38'

Test #15:

score: 0
Accepted
time: 60ms
memory: 7660kb

input:

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

output:

5

result:

ok single line: '5'