QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#534474#1970. Social Distancinggeorgeyucjr#AC ✓24ms11176kbC++174.5kb2024-08-27 11:01:002024-08-27 11:01:00

Judging History

This is the latest submission verdict.

  • [2024-08-27 11:01:00]
  • Judged
  • Verdict: AC
  • Time: 24ms
  • Memory: 11176kb
  • [2024-08-27 11:01:00]
  • Submitted

answer

// author : georgeyucjr
#include <bits/stdc++.h>

using ll = long long;
using ull = unsigned long long;
using i128 = __int128;
using u128 = unsigned __int128;
using arr3 = std ::array<int, 3>;
using arr4 = std ::array<int, 4>;
using pii = std ::pair<int, int>;

#define ALL(vc) std ::begin(vc), std ::end(vc)
#define rep(i, f, t, ...) for (std ::decay<decltype(f + t)>::type i = f, _END_##i = t, ##__VA_ARGS__; i <= _END_##i; ++i)
#define per(i, f, t, ...) for (std ::decay<decltype(f + t)>::type i = f, _END_##i = t, ##__VA_ARGS__; i >= _END_##i; --i)
#define eb emplace_back
#define pb push_back
#define mkp make_pair
#define FILEIO(filename) freopen(filename ".in", "r", stdin), freopen(filename ".out", "w", stdout)
template <class T, class U> inline T ceil(T &&x, U &&y) { return (x > 0 ? (x + y - 1) / y : x / y); }
template <class T, class U> inline T floor(T &&x, U &&y) { return (x > 0 ? x / y : (x - y + 1) / y); }
template <class T, class U> inline T divmod(T &&x, U &&y) { auto &&q = floor(x, y); return mkp(q, x - q * y); }
template <class T> constexpr static T inf = std ::numeric_limits<T>::max() >> 1;
template <class T> inline int SZ(T &&x) { return x.size(); }
template <typename T> inline T Abs(const T &x) { return x < 0 ? -x : x; }
template <typename T> inline T sqr(const T &x) { return x * x; }
template <typename T> inline T Max(const T &x) { return x; }
template <typename T> inline T Min(const T &x) { return x; }
template <typename T, typename U, typename... Args> inline T Max(const T &x, const U &y, const Args &...args) { return x < y ? Max(y, args...) : Max(x, args...); }
template <typename T, typename U, typename... Args> inline T Min(const T &x, const U &y, const Args &...args) { return x < y ? Min(x, args...) : Min(y, args...); }
template <typename T, typename... Args> inline void chkmax(T &d, const Args &...x) { d = Max(d, x...); }
template <typename T, typename... Args> inline void chkmin(T &d, const Args &...x) { d = Min(d, x...); }

using namespace std;

#ifdef georgeyucjr
#include <georgeyucjr/debug.h>
#else
#define dbg(...) 36
#endif

constexpr int N = 505;

# define int long long

int n, m, vis[N][N], mx, dist[N][N];
pii S, T;
char s[N][N];
constexpr int d8x[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
constexpr int d8y[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
constexpr int dx[4] = {-1, 1, 0, 0};
constexpr int dy[4] = {0, 0, -1, 1};

signed main() {	
	cin.tie(nullptr) -> sync_with_stdio(false);
	cin >> n >> m;
	mx = Max(n, m);
	rep(i, 1, n) rep(j, 1, m) cin >> s[i][j];
	queue<tuple<int, int, int> > q; while(SZ(q)) q.pop();
	rep(i, 1, n) rep(j, 1, m) vis[i][j] = false,  dist[i][j] = inf<int>; 
	rep(i, 1, n) rep(j, 1, m) if (s[i][j] == 'S') {
		S = mkp(i, j);
	} else if (s[i][j] == 'E') {
		T = mkp(i, j);
	} else if (s[i][j] == '*') {
		q.emplace(i, j, 0), dist[i][j] = 0;
	} {
		while(SZ(q)) {
			auto [x, y, cur] = q.front();
			q.pop();
			if (vis[x][y]) continue;
			vis[x][y] = true;
			rep(d, 0, 7) {
				int tx = d8x[d], ty = d8y[d];
				int xx = x + tx, yy = y + ty;
				if (xx < 1 || xx > n || yy < 1 || yy > m) continue;
				if (dist[xx][yy] > cur  + 1) {
					dist[xx][yy] = cur + 1;
					q.emplace(xx, yy, dist[xx][yy]);
				}
			}
		}
	} rep(i, 1, n) rep(j, 1, m) if (s[i][j] == '#') dist[i][j] = 0;
	// rep(i, 1, n) rep(j, 1, m) cerr << dist[i][j] << (j == m ? "\n" : " ");
	// dbg(S.first, S.second, T.first, T.second);
	int l = 1, r = mx + 1, ans = -1;
	auto chk = [&](const int &mid) -> bool {
		if (dist[S.first][S.second] < mid) return false;
		rep(i, 1, n) rep(j, 1, m) vis[i][j] = false;
		queue<pii> q;
		q.push(S);
		while(SZ(q)) {
			auto [x, y] = q.front(); q.pop();
			if (vis[x][y]) continue;
			vis[x][y] = true;
			rep(d, 0, 3)  {
				int tx = dx[d];
				int ty = dy[d];
				int xx = x + tx, yy = y + ty;
				if (xx < 1 || xx > n || yy < 1 || yy > m) continue;
				if (dist[xx][yy] >= mid) q.emplace(xx, yy);
			}
		}
		return vis[T.first][T.second];
	};
	while(l <= r) {
		// cerr << " l = " << l << ", r = " << r << "\n";
		int mid = (l + r) >> 1;
		if(!chk(mid)) r = mid - 1;
		else l = mid + 1, ans = mid;
	}
	if (ans > mx) {
		cout << "safe" << "\n";
	} else cout << ans << "\n";

#if defined(LOCAL) && !defined(CPH)
  // std::cerr << "Spend Time : " << clock() * 1. / CLOCKS_PER_SEC * 1e3 << " ms \n";
#endif
  return 0;
}

/*
10 15
#*.......*.....
*..E..*...*....
.........#.....
.....*.........
.....*#........
S......*.....#.
.....##......#.
..##...........
#..............
*.*............
*/

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 5720kb

input:

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

output:

2

result:

ok single line: '2'

Test #2:

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

input:

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

output:

3

result:

ok single line: '3'

Test #3:

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

input:

3 3
#E#
###
#S#

output:

-1

result:

ok single line: '-1'

Test #4:

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

input:

3 3
.S.
***
.E.

output:

-1

result:

ok single line: '-1'

Test #5:

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

input:

3 3
S..
...
..E

output:

safe

result:

ok single line: 'safe'

Test #6:

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

input:

2 2
S.
.E

output:

safe

result:

ok single line: 'safe'

Test #7:

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

input:

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

output:

3

result:

ok single line: '3'

Test #8:

score: 0
Accepted
time: 18ms
memory: 7976kb

input:

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

output:

13

result:

ok single line: '13'

Test #9:

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

input:

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

output:

-1

result:

ok single line: '-1'

Test #10:

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

input:

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

output:

-1

result:

ok single line: '-1'

Test #11:

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

input:

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

output:

8

result:

ok single line: '8'

Test #12:

score: 0
Accepted
time: 22ms
memory: 8064kb

input:

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

output:

safe

result:

ok single line: 'safe'

Test #13:

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

input:

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

output:

5

result:

ok single line: '5'

Test #14:

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

input:

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

output:

38

result:

ok single line: '38'

Test #15:

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

input:

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

output:

5

result:

ok single line: '5'