QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#212505#1970. Social Distancingvioalbert#AC ✓25ms7924kbC++173.7kb2023-10-13 16:38:062023-10-13 16:38:07

Judging History

This is the latest submission verdict.

  • [2023-10-13 16:38:07]
  • Judged
  • Verdict: AC
  • Time: 25ms
  • Memory: 7924kb
  • [2023-10-13 16:38:06]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(x) (x).begin(), (x).end()
#ifdef LOCAL
#define debug(...) __VA_ARGS__;
#else
#define debug(...) 24;
#endif

template<class A, class B>
ostream& operator<<(ostream &os, pair<A, B> &p);
template<class T>
ostream& operator<<(ostream &os, vector<T> &v);
template<class T, size_t sz>
ostream& operator<<(ostream &os, array<T, sz> &v);

template<class A, class B>
ostream& operator<<(ostream &os, pair<A, B> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}

template<class T>
ostream& operator<<(ostream &os, vector<T> &v) {
  os << '{';
  bool f = 0; for(auto &i : v) { if(f) os << ',';os << i; f = 1;}
  return os << '}';
}

template<class T, size_t sz>
ostream& operator<<(ostream &os, array<T, sz> &v) {
  os << '{';
  bool f = 0; for(auto &i : v) { if(f) os << ',';os << i; f = 1;}
  return os << '}';
}

using pii = pair<int, int>;
#define fi first
#define se second

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

int main() {
  ios::sync_with_stdio(0); cin.tie(0);
  int n, m; cin >> n >> m;
  vector a(n, vector<char>(m));
  for(auto &v : a) for(auto &c : v) cin >> c;

  vector<pii> free;
  vector<pii> p;
  pii s, e;
  for(int i = 0; i < n; i++) {
    for(int j = 0; j < m; j++) {
      if(a[i][j] == 'S') {
        s = {i, j};
      }
      if(a[i][j] == 'E') {
        e = {i, j};
      }
      if(a[i][j] == '*') {
        p.emplace_back(i, j);
        a[i][j] = '#';
      }
      if(a[i][j] != '#')
        free.emplace_back(i, j);
    }
  }

  if(p.empty()) { // bfs
    vector vis(n, vector<int>(m, 0));
    queue<pii> q;
    q.push({s.fi, s.se});
    vis[s.fi][s.se] = 1;
    while(!q.empty()) {
      auto [i, j] = q.front(); q.pop();
      for(int x = 0; x < 4; x++) {
        int ni = i+dx[x], nj = j+dy[x];
        if(ni < 0 || nj < 0 || ni >= n || nj >= m) continue;
        if(a[ni][nj] == '#') continue;
        if(!vis[ni][nj]) {
          vis[ni][nj] = 1;
          q.push({ni, nj});
        }
      }
    }
    if(vis[e.fi][e.se]) cout << "safe" << '\n';
    else cout << -1 << '\n';
    return 0;
  }

  vector val(n, vector<int>(m, -1));
  queue<pii> q;
  for(auto &[i, j] : p) {
    val[i][j] = 0;
    q.push({i, j});
  }
  while(!q.empty()) {
    auto [i, j] = q.front(); q.pop();
    for(int x = 0; x < 8; x++) {
      int ni = i+dx[x], nj = j+dy[x];
      if(ni < 0 || nj < 0 || ni >= n || nj >= m) continue;
      if(val[ni][nj] == -1) {
        val[ni][nj] = val[i][j] + 1;
        q.push({ni, nj});
      }
    }
  }
  
  sort(all(free), [&](pii i, pii j){
    return val[i.fi][i.se] > val[j.fi][j.se];
  });

  const auto toidx = [&](pii x) {
    return x.fi*m + x.se;
  };
  vector<int> par(n*m, -1);
  function<int(int)> find = [&](int x) {
    return x == par[x] ? x : (par[x] = find(par[x]));
  };
  function<bool(int, int)> join = [&](int x, int y) {
    if(find(x) == find(y)) return false;
    debug(cerr << " join " << x << ' ' << y << '\n');
    par[find(x)] = find(y);
    return true;
  };
  int idxs = toidx(s), idxe = toidx(e);

  for(pii nw : free) {
    debug(cerr << nw << '\n');
    int idx = toidx(nw);
    par[idx] = idx;
    for(int i = 0; i < 4; i++) {
      pii to;
      to.fi = nw.fi + dx[i];
      to.se = nw.se + dy[i];
      if(to.fi < 0 || to.fi >= n || to.se < 0 || to.se >= m) continue;
      if(par[toidx(to)] == -1) continue;
      join(idx, toidx(to));
    }
    if(par[idxs] != -1 && par[idxe] != -1 && find(idxs) == find(idxe)) {
      cout << val[nw.fi][nw.se] << '\n';
      return 0;
    }
  }
  cout << -1 << '\n';
}

詳細信息

Test #1:

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

input:

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

output:

2

result:

ok single line: '2'

Test #2:

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

input:

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

output:

3

result:

ok single line: '3'

Test #3:

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

input:

3 3
#E#
###
#S#

output:

-1

result:

ok single line: '-1'

Test #4:

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

input:

3 3
.S.
***
.E.

output:

-1

result:

ok single line: '-1'

Test #5:

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

input:

3 3
S..
...
..E

output:

safe

result:

ok single line: 'safe'

Test #6:

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

input:

2 2
S.
.E

output:

safe

result:

ok single line: 'safe'

Test #7:

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

input:

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

output:

3

result:

ok single line: '3'

Test #8:

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

input:

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

output:

13

result:

ok single line: '13'

Test #9:

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

input:

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

output:

-1

result:

ok single line: '-1'

Test #10:

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

input:

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

output:

-1

result:

ok single line: '-1'

Test #11:

score: 0
Accepted
time: 15ms
memory: 6628kb

input:

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

output:

8

result:

ok single line: '8'

Test #12:

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

input:

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

output:

safe

result:

ok single line: 'safe'

Test #13:

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

input:

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

output:

5

result:

ok single line: '5'

Test #14:

score: 0
Accepted
time: 8ms
memory: 6072kb

input:

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

output:

38

result:

ok single line: '38'

Test #15:

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

input:

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

output:

5

result:

ok single line: '5'