QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#867306#1970. Social Distancingkevinyang#AC ✓72ms8320kbC++172.7kb2025-01-23 13:10:592025-01-23 13:11:01

Judging History

This is the latest submission verdict.

  • [2025-01-23 13:11:01]
  • Judged
  • Verdict: AC
  • Time: 72ms
  • Memory: 8320kb
  • [2025-01-23 13:10:59]
  • Submitted

answer

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

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

signed main() {
    cin.tie(0)->sync_with_stdio(0);
    int n,m;
    cin >> n >> m;
    vector<vector<char>>a(n+1,vector<char>(m+1));
    int sx,sy;
    int ex,ey;
    for(int i = 1; i<=n; i++){
        string s;
        cin >> s;
        for(int j = 1; j<=m; j++){
            a[i][j] = s[j-1];
            if(a[i][j] == 'S'){
                sx = i; sy = j;
            }
            if(a[i][j] == 'E'){
                ex = i; ey = j;
            }
        }
    }
    vector<int>dx = {-1,0,1,0,-1,-1,1,1};
    vector<int>dy = {0,1,0,-1,1,-1,1,-1};
    vector<vector<int>>dis2(n+1,vector<int>(m+1,(int)1e9));
    vector<vector<bool>>vis2(n+1,vector<bool>(m+1));
    queue<pii>q;
    for(int i = 1; i<=n; i++){
        for(int j = 1; j<=m; j++){
            if(a[i][j] == '*'){
                vis2[i][j] = true;
                dis2[i][j] = 0;
                q.push({i,j});
            }
        }
    }
    auto good = [&](int x, int y) -> bool {
        return x >= 1 && x <= n && y >= 1 && y <= m;
    };
    while(q.size()){
        auto [i,j] = q.front(); q.pop();
        for(int d = 0; d<8; d++){
            int x = dx[d] + i;
            int y = dy[d] + j;
            if(good(x,y) && !vis2[x][y]){
                vis2[x][y] = true;
                dis2[x][y] = dis2[i][j] + 1;
                q.push({x,y});
            }
        }
    }
    int low = -1; int high = (int)1e9;
    vector<vector<bool>>vis(n+1,vector<bool>(m+1));
    while(low + 1 < high){
        int mid = (low+high)/2;
        for(int i = 1; i<=n; i++){
            for(int j = 1; j<=m; j++){
                vis[i][j] = 0;
            }
        }
        vis[sx][sy] = true;
        q.push({sx,sy});
        while(q.size()){
            auto [i,j] = q.front(); q.pop();
            for(int d = 0; d<4; d++){
                int x = dx[d] + i;
                int y = dy[d] + j;
                if(good(x,y) && !vis[x][y] && dis2[x][y] >= mid && a[x][y] != '#' && a[x][y] != '*'){
                    vis[x][y] = true;
                    q.push({x,y});
                }
            }
        }
        if(vis[ex][ey]){
            low = mid;
        }
        else{
            high = mid;
        }
    }
    if(low == -1){
        cout << "-1\n";
        return 0;
    }
    if(low >100000){
        cout << "safe\n";
        return 0;
    }
    cout << low << '\n';
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3712kb

input:

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

output:

2

result:

ok single line: '2'

Test #2:

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

input:

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

output:

3

result:

ok single line: '3'

Test #3:

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

input:

3 3
#E#
###
#S#

output:

-1

result:

ok single line: '-1'

Test #4:

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

input:

3 3
.S.
***
.E.

output:

-1

result:

ok single line: '-1'

Test #5:

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

input:

3 3
S..
...
..E

output:

safe

result:

ok single line: 'safe'

Test #6:

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

input:

2 2
S.
.E

output:

safe

result:

ok single line: 'safe'

Test #7:

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

input:

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

output:

3

result:

ok single line: '3'

Test #8:

score: 0
Accepted
time: 23ms
memory: 6144kb

input:

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

output:

13

result:

ok single line: '13'

Test #9:

score: 0
Accepted
time: 16ms
memory: 8320kb

input:

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

output:

-1

result:

ok single line: '-1'

Test #10:

score: 0
Accepted
time: 25ms
memory: 6144kb

input:

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

output:

-1

result:

ok single line: '-1'

Test #11:

score: 0
Accepted
time: 23ms
memory: 6144kb

input:

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

output:

8

result:

ok single line: '8'

Test #12:

score: 0
Accepted
time: 72ms
memory: 6144kb

input:

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

output:

safe

result:

ok single line: 'safe'

Test #13:

score: 0
Accepted
time: 30ms
memory: 6784kb

input:

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

output:

5

result:

ok single line: '5'

Test #14:

score: 0
Accepted
time: 20ms
memory: 6144kb

input:

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

output:

38

result:

ok single line: '38'

Test #15:

score: 0
Accepted
time: 24ms
memory: 6144kb

input:

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

output:

5

result:

ok single line: '5'