QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#55726#1970. Social DistancingSa3tElSefr#AC ✓24ms13308kbC++2.9kb2022-10-14 23:10:252022-10-14 23:10:26

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-14 23:10:26]
  • Judged
  • Verdict: AC
  • Time: 24ms
  • Memory: 13308kb
  • [2022-10-14 23:10:25]
  • Submitted

answer

#include <bits/stdc++.h>

#define ll long long
#define ld  double
using namespace std;

const int N = 500 + 5,  mod = 998244353;


int n, m, dis[N][N], id, vis[N][N], vis2[N][N];


int fromR, fromC, toR, toC;

vector<pair<int, int> > src;

char a[N][N];

int dr[] = {0, 0, 1, -1};
int dc[] = {1, -1, 0, 0};


bool valid(int r, int c) {
    return r >= 0 && r < n && c >= 0 && c < m;
}

void bfs() {
    queue<pair< pair<int, int>, pair<int, int> > > q;
    for(auto i: src) {
        q.push({i, {0, 0}});
        dis[i.first][i.second] = 0;
    }

    while((int) q.size() > 0) {
        int r = q.front().first.first, c = q.front().first.second;
        int kamR = q.front().second.first, kamC = q.front().second.second;
        q.pop();
        for(int i = 0; i < 4; i++) {
            int nr = r + dr[i], nc = c + dc[i];
            int nkamR = kamR + dr[i], nkamC = kamC + dc[i];
            if(valid(nr, nc) && dis[nr][nc] > max(abs(nkamR), abs(nkamC))) {
                dis[nr][nc] = max(abs(nkamR), abs(nkamC));
                q.push({{nr, nc}, {nkamR, nkamC}});
            }
        }
    }
}

void dfs(int r, int c) {
    vis[r][c] = 1;

    for(int i = 0; i < 4; i++) {
        int nr = r + dr[i], nc = c + dc[i];
        if(valid(nr, nc) && a[nr][nc] != '#' && a[nr][nc] != '*' && vis[nr][nc] == 0) {
            dfs(nr, nc);
        }
    }
}

void dfs2(int r, int c, int d) {
    if(dis[r][c] < d) {
        return;
    }
    vis2[r][c] = id;

    for(int i = 0; i < 4; i++) {
        int nr = r + dr[i], nc = c + dc[i];
        if(valid(nr, nc) && a[nr][nc] != '#' && vis2[nr][nc] != id) {
            dfs2(nr, nc, d);
        }
    }

}



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

    cin >> n >> m;
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            cin >> a[i][j];
            dis[i][j] = mod;
            if(a[i][j] == 'S') {
                fromR = i;
                fromC = j;
            }
            else if(a[i][j] == 'E') {
                toR = i;
                toC = j;
            }
            else if(a[i][j] == '*') {
                src.push_back({i, j});
            }
        }
    }

    bfs();

//    for(int i = 0; i < n; i++) {
//        for(int j = 0; j < m; j++) {
//            cout << dis[i][j] << ' ';
//        }
//        cout << '\n';
//    }

    dfs(fromR, fromC);


    if(vis[toR][toC] == 0) {
        cout << -1;
        return 0;
    }

    if((int) src.size() == 0) {
        cout << "safe";
        return 0;
    }

    int low = 1, high = 1200, ans = 1500;
    while(high >= low) {
        id++;
        int mid = low + high >> 1;
        dfs2(fromR, fromC, mid);
        if(vis2[toR][toC] == id) {
            ans = mid;
            low = mid + 1;
        }
        else high = mid - 1;
    }
    cout << ans << '\n';




    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 2ms
memory: 3592kb

input:

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

output:

2

result:

ok single line: '2'

Test #2:

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

input:

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

output:

3

result:

ok single line: '3'

Test #3:

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

input:

3 3
#E#
###
#S#

output:

-1

result:

ok single line: '-1'

Test #4:

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

input:

3 3
.S.
***
.E.

output:

-1

result:

ok single line: '-1'

Test #5:

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

input:

3 3
S..
...
..E

output:

safe

result:

ok single line: 'safe'

Test #6:

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

input:

2 2
S.
.E

output:

safe

result:

ok single line: 'safe'

Test #7:

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

input:

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

output:

3

result:

ok single line: '3'

Test #8:

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

input:

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

output:

13

result:

ok single line: '13'

Test #9:

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

input:

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

output:

-1

result:

ok single line: '-1'

Test #10:

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

input:

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

output:

-1

result:

ok single line: '-1'

Test #11:

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

input:

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

output:

8

result:

ok single line: '8'

Test #12:

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

input:

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

output:

safe

result:

ok single line: 'safe'

Test #13:

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

input:

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

output:

5

result:

ok single line: '5'

Test #14:

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

input:

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

output:

38

result:

ok single line: '38'

Test #15:

score: 0
Accepted
time: 14ms
memory: 10908kb

input:

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

output:

5

result:

ok single line: '5'