QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#223552#1970. Social DistancingBUET_Twilight#AC ✓19ms7928kbC++234.6kb2023-10-22 13:07:222023-10-22 13:07:22

Judging History

This is the latest submission verdict.

  • [2023-10-22 13:07:22]
  • Judged
  • Verdict: AC
  • Time: 19ms
  • Memory: 7928kb
  • [2023-10-22 13:07:22]
  • Submitted

answer

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp> 
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;
#define getbit(n, i) (((n) & (1LL << (i))) != 0) 
#define setbit0(n, i) ((n) & (~(1LL << (i)))) 
#define setbit1(n, i) ((n) | (1LL << (i))) 
#define togglebit(n, i) ((n) ^ (1LL << (i))) 
#define lastone(n) ((n) & (-(n))) 
char gap = 32;
#define ll long long 
#define int long long 
#define lll __int128_t
#define pb push_back
typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef pair<int, int> ii;
int n, m;
const int inf = 1e9;
vector<vector<int>> dist;
vector<int> dx = {0, 0, 1, -1};
vector<int> dy = {1, -1, 0, 0};
vector<string> grid;

bool inBox(int x, int y) {
    return x >= 0 and x < n and y >= 0 and y < m;
}

bool reach(ii a, ii b, int lim) {
    queue<ii> q;
    if (dist[a.first][a.second] < lim) return false;
    q.push(a);
    vector<vector<bool>> vis(n, vector<bool>(m, false));
    vis[a.first][a.second] = 1;
    while(!q.empty()) {
        ii cur = q.front(); q.pop();
        int x = cur.first, y = cur.second;
        // cout << x << " " << y << endl;
        
        if (x == b.first and y == b.second) return true;
        for (int i=0; i<4; i++) {
            int nx = x + dx[i], ny = y + dy[i];
            if (inBox(nx, ny) == false) continue;
            if (dist[nx][ny] < lim) continue;
            if (grid[nx][ny] == '#') continue;
            if (vis[nx][ny]) continue;
            vis[nx][ny] = true;
            q.push({nx, ny});
        }
    }
    return false;
}

void calc(vector<ii> &pts) {
    dist = vector<vector<int>>(n, vector<int>(m, inf));
    if (pts.empty()) return;
    
    vector<vector<int>> pref(n, vector<int>(m, 0));
    for (int i=0; i<pts.size(); i++) {
        int x = pts[i].first, y = pts[i].second;
        pref[x][y]++;
    }
    for (int i=0; i<n; i++) {
        for (int j=0; j<m; j++) {
            if (j > 0) pref[i][j] += pref[i][j-1];
            if (i > 0) pref[i][j] += pref[i-1][j];
            if (i > 0 and j > 0) pref[i][j] -= pref[i-1][j-1];
        }
    }


    auto get = [&](int x1, int y1, int x2, int y2) {
        x1 = max(x1, 0LL);
        y1 = max(y1, 0LL);
        x2 = min(x2, n-1);
        y2 = min(y2, m-1);
        int res = pref[x2][y2];
        if (x1 > 0) res -= pref[x1-1][y2];
        if (y1 > 0) res -= pref[x2][y1-1];
        if (x1 > 0 and y1 > 0) res += pref[x1-1][y1-1];
        return res;
    };

    // for (int i=0; i<n; i++) {
    //     for (int j=0; j<m; j++) {
    //         cout << pref[i][j] << " ";
    //     }
    //     cout << endl;
    // }

    // cout << get(0, 1, 2, 2) << endl;

    for (int i=0; i<n; i++) {
        for (int j=0; j<m; j++) {
            int l = 0, h = 1000;
            while(l < h) {
                int mid = (l + h) / 2;
                int x1 = i - mid, y1 = j - mid, x2 = i + mid, y2 = j + mid;
                int cnt = get(x1, y1, x2, y2);
                if (cnt == 0) {
                    l = mid + 1;
                }
                else {
                    h = mid;
                }
            }
            dist[i][j] = l;
        }
    }
}


void solve(){
    
    cin >> n >> m;

    grid.resize(n);
    for (int i=0; i<n; i++) cin >> grid[i];

    ii start, end;
    vector<ii> pts;
    for (int i=0; i<n; i++) {
        for (int j=0; j<m; j++) {
            if (grid[i][j] == 'S') start = {i, j};
            if (grid[i][j] == 'E') end = {i, j};
            if (grid[i][j] == '*') pts.push_back({i, j});
        }
    }
    calc(pts);
    // for (int i=0; i<n; i++) {
    //     for (int j=0; j<m; j++) cout << dist[i][j] << " ";
    //     cout << endl;
    // }

    // cout << reach(start, end, 4) << endl;;
    if (!reach(start, end, 1)) {
        cout << -1 << endl;
        return;
    }

    if (reach(start, end, 1e8)) {
        cout << "safe" << endl;
        return;
    }

    int low = 1, high = 1e8;
    //cout << reach(start, end, 5) << endl;
    while(low < high) {
        int mid = (low + high + 1) / 2;
        // cout << low << " " << high << endl;
        if (!reach(start, end, mid)) {
            high = mid - 1;
        }
        else {
            low = mid;
        }
    }
    cout << low << endl;

}
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    solve();
    return 0;
}

详细

Test #1:

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

input:

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

output:

2

result:

ok single line: '2'

Test #2:

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

input:

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

output:

3

result:

ok single line: '3'

Test #3:

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

input:

3 3
#E#
###
#S#

output:

-1

result:

ok single line: '-1'

Test #4:

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

input:

3 3
.S.
***
.E.

output:

-1

result:

ok single line: '-1'

Test #5:

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

input:

3 3
S..
...
..E

output:

safe

result:

ok single line: 'safe'

Test #6:

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

input:

2 2
S.
.E

output:

safe

result:

ok single line: 'safe'

Test #7:

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

input:

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

output:

3

result:

ok single line: '3'

Test #8:

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

input:

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

output:

13

result:

ok single line: '13'

Test #9:

score: 0
Accepted
time: 7ms
memory: 7928kb

input:

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

output:

-1

result:

ok single line: '-1'

Test #10:

score: 0
Accepted
time: 5ms
memory: 7388kb

input:

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

output:

-1

result:

ok single line: '-1'

Test #11:

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

input:

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

output:

8

result:

ok single line: '8'

Test #12:

score: 0
Accepted
time: 5ms
memory: 6164kb

input:

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

output:

safe

result:

ok single line: 'safe'

Test #13:

score: 0
Accepted
time: 19ms
memory: 7500kb

input:

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

output:

5

result:

ok single line: '5'

Test #14:

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

input:

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

output:

38

result:

ok single line: '38'

Test #15:

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

input:

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

output:

5

result:

ok single line: '5'