QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#142205#1970. Social DistancingRed_uncleAC ✓654ms6156kbC++144.6kb2023-08-18 17:08:292023-08-18 17:08:32

Judging History

This is the latest submission verdict.

  • [2023-08-18 17:08:32]
  • Judged
  • Verdict: AC
  • Time: 654ms
  • Memory: 6156kb
  • [2023-08-18 17:08:29]
  • Submitted

answer

#pragma GCC optimize(3)
#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#include<bits/stdc++.h>
using namespace std;
const int N = 510;
char s[N][N];
int n, m;
bool st[N][N], vis1[N * N];
int dx1[] = {-1, 0, 1, 0};
int dy1[] = {0, 1, 0, -1};
pair<int, int> tmp[N * N];
pair<int, int> start = {-1, -1}, ed = {-1, -1};
bool bfs() {
	bool vis[N][N] = {0};
	queue<pair<int, int>> q;
	vis[start.first][start.second] = 1;
	q.push(start);
	while (q.size()) {
		auto t = q.front();
		q.pop();
		if (t == ed) {
			return 1;
		}
		for (int i = 0; i < 4; ++i) {
			int nx = t.first + dx1[i];
			int ny = t.second + dy1[i];
			if (nx >= n || nx < 0 || ny < 0 || ny >= m || st[nx][ny] || s[nx][ny] == '#' || vis[nx][ny]) {
				continue;
			}
			vis[nx][ny] = 1;
			q.push({nx, ny});
		}
	}
	return 0;
}
int main() {
//    ios::sync_with_stdio(0);
//    cin.tie(0);
//    cout.tie(0);
	scanf("%d %d", &n, &m);
	getchar();
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			scanf("%c", &s[i][j]);
		}
		getchar();
	}
	int cnt = 0;
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			if (s[i][j] == '*') tmp[++cnt] = {i, j}, st[i][j] = 1;
			else if (s[i][j] == 'S') start = {i, j};
			else if (s[i][j] == 'E') ed = {i, j};
		}
	}
	swap(start, ed);
	for (int len = 0; len <= max(n, m); ++len) {
		for (int i = 1; i <= cnt; ++i) {
			//cout << len << ' ' << i << ' ' << vis1[i] << "\n";
			int now = 0;
			if (!vis1[i] && len) {
				int nx = tmp[i].first;
				int ny = tmp[i].second;
				if (nx - len >= 0) {
					for (int j = max(0, ny - len); j <= min(ny + len, m); ++j) {
						if (j >= 0 && j < m) {
							if (!st[nx - len][j])
								st[nx - len][j] = 1, now++;
						}
					}
				}
				if (nx + len < n) {
					for (int j = max(0, ny - len); j <= min(ny + len, m); ++j) {
						if (j >= 0 && j < m) {
							if (!st[nx + len][j])
								st[nx + len][j] = 1, now++;
						}
					}
				}
				if (ny - len >= 0) {
					for (int j = max(0, nx - len); j <= min(n, nx + len); ++j) {
						if (j >= 0 && j < n) {
							if (!st[j][ny - len])
								st[j][ny - len] = 1, now++;
						}
					}
				}
				if (ny + len < m) {
					for (int j = max(0, nx - len); j <= min(n, nx + len); ++j) {
						if (j >= 0 && j < n) {
							if (!st[j][ny + len])
								st[j][ny + len] = 1, now++;
						}
					}
				}
				if (!now) {
					vis1[i] = 1;
				}
			}
		}
		if (st[ed.first][ed.second] || st[start.first][start.second]) {
			if (!len) {
				printf("-1");
			} else
				printf("%d", len);
			return 0;
		}
		bool f = bfs();
		if (!f) {
			if (!len) {
				printf("-1");
			} else
				printf("%d", len);
			return 0;
		}
	}
	if (!cnt) {
		puts("safe");
		return 0;
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

2

result:

ok single line: '2'

Test #2:

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

input:

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

output:

3

result:

ok single line: '3'

Test #3:

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

input:

3 3
#E#
###
#S#

output:

-1

result:

ok single line: '-1'

Test #4:

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

input:

3 3
.S.
***
.E.

output:

-1

result:

ok single line: '-1'

Test #5:

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

input:

3 3
S..
...
..E

output:

safe

result:

ok single line: 'safe'

Test #6:

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

input:

2 2
S.
.E

output:

safe

result:

ok single line: 'safe'

Test #7:

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

input:

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

output:

3

result:

ok single line: '3'

Test #8:

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

input:

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

output:

13

result:

ok single line: '13'

Test #9:

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

input:

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

output:

-1

result:

ok single line: '-1'

Test #10:

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

input:

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

output:

-1

result:

ok single line: '-1'

Test #11:

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

input:

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

output:

8

result:

ok single line: '8'

Test #12:

score: 0
Accepted
time: 654ms
memory: 4076kb

input:

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

output:

safe

result:

ok single line: 'safe'

Test #13:

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

input:

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

output:

5

result:

ok single line: '5'

Test #14:

score: 0
Accepted
time: 34ms
memory: 6156kb

input:

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

output:

38

result:

ok single line: '38'

Test #15:

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

input:

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

output:

5

result:

ok single line: '5'