QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#509714#1970. Social DistancingPetroTarnavskyi#AC ✓161ms16492kbC++202.1kb2024-08-08 17:40:112024-08-08 17:40:12

Judging History

This is the latest submission verdict.

  • [2024-08-08 17:40:12]
  • Judged
  • Verdict: AC
  • Time: 161ms
  • Memory: 16492kb
  • [2024-08-08 17:40:11]
  • Submitted

answer

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second

typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;

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

template<typename T>
void updMin(T& a, T b)
{
	a = min(a, b);
}

int n, m;
int is = -1, js = -1, it = -1, jt = -1;

bool dfs(int i, int j, const vector<string>& grid, vector<VI>& used)
{
	used[i][j] = true;
	if (i == it && j == jt)
		return true;
	FOR(k, 0, 4)
	{
		int ni = i + dx[k], nj = j + dy[k];
		if (0 <= ni && ni < n && 0 <= nj && nj < m && grid[ni][nj] == '.' && !used[ni][nj] && dfs(ni, nj, grid, used))
		{
			return true;
		}
	}
	return false;
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> n >> m;
	vector<string> s(n);
	for (string& row : s)
		cin >> row;
	FOR(i, 0, n)
		FOR(j, 0, m)
		{
			if (s[i][j] == 'S')
			{
				is = i;
				js = j;
				s[i][j] = '.';
			}
			else if (s[i][j] == 'E')
			{
				it = i;
				jt = j;
				s[i][j] = '.';
			}
		}
	assert(is != -1 && it != -1);
	const int D = max(n, m) + 7;
	// special for Yarema
	vector<VI> distInRow(n, VI(m, D)), dist(n, VI(m, D));
	FOR(i, 0, n)
	{
		FOR(j, 0, m)
		{
			if (s[i][j] == '*')
				distInRow[i][j] = 0;
		}
		FOR(j, 1, m)
			updMin(distInRow[i][j], distInRow[i][j - 1] + 1);
		RFOR(j, m - 1, 0)
			updMin(distInRow[i][j], distInRow[i][j + 1] + 1);
	}
	FOR(i, 0, n)
		FOR(j, 0, m)
			FOR(k, 0, n)
				updMin(dist[i][j], max(abs(k - i), distInRow[k][j]));
	vector<string> grid(n, string(m, '.'));
	int l = -1, r = D;
	while (r - l > 1)
	{
		int mid = (l + r) / 2;
		grid = s;
		FOR(i, 0, n)
			FOR(j, 0, m)
				if (dist[i][j] < mid)
					grid[i][j] = '#';
		vector<VI> used(n, VI(m));
		if (dfs(is, js, grid, used))
			l = mid;
		else
			r = mid;
	}
	if (r == D)
		cout << "safe\n";
	else
		cout << l << "\n";
	return 0;
}

詳細信息

Test #1:

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

input:

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

output:

2

result:

ok single line: '2'

Test #2:

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

input:

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

output:

3

result:

ok single line: '3'

Test #3:

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

input:

3 3
#E#
###
#S#

output:

-1

result:

ok single line: '-1'

Test #4:

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

input:

3 3
.S.
***
.E.

output:

-1

result:

ok single line: '-1'

Test #5:

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

input:

3 3
S..
...
..E

output:

safe

result:

ok single line: 'safe'

Test #6:

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

input:

2 2
S.
.E

output:

safe

result:

ok single line: 'safe'

Test #7:

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

input:

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

output:

3

result:

ok single line: '3'

Test #8:

score: 0
Accepted
time: 133ms
memory: 8560kb

input:

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

output:

13

result:

ok single line: '13'

Test #9:

score: 0
Accepted
time: 136ms
memory: 6708kb

input:

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

output:

-1

result:

ok single line: '-1'

Test #10:

score: 0
Accepted
time: 158ms
memory: 12928kb

input:

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

output:

-1

result:

ok single line: '-1'

Test #11:

score: 0
Accepted
time: 136ms
memory: 6852kb

input:

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

output:

8

result:

ok single line: '8'

Test #12:

score: 0
Accepted
time: 161ms
memory: 16492kb

input:

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

output:

safe

result:

ok single line: 'safe'

Test #13:

score: 0
Accepted
time: 141ms
memory: 8420kb

input:

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

output:

5

result:

ok single line: '5'

Test #14:

score: 0
Accepted
time: 131ms
memory: 7356kb

input:

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

output:

38

result:

ok single line: '38'

Test #15:

score: 0
Accepted
time: 128ms
memory: 6976kb

input:

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

output:

5

result:

ok single line: '5'