답안 #392502

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
392502 2021-04-21T09:02:14 Z Berted Mecho (IOI09_mecho) C++14
100 / 100
190 ms 7008 KB
#include <iostream>
#include <queue>
#define pii pair<int, int>
#define fst first
#define snd second
using namespace std;

const int INF = 1e9;

int T, N, S;
string M[801];
int block[801][801], dist[801][801];

inline void preBFS()
{
	queue<pii> q;
	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < N; j++) block[i][j] = INF;
	}
	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < N; j++)
		{
			if (M[i][j] == 'H') 
			{
				block[i][j] = 0;
				q.push({i, j});
			}
			if (M[i][j] == 'T') {block[i][j] = 0;}
			if (M[i][j] == 'D') {block[i][j] = INF + 1;}
		}
	}
	while (q.size())
	{
		pii u = q.front(); q.pop();
		if (u.fst && block[u.fst - 1][u.snd] == INF)
		{
			block[u.fst - 1][u.snd] = block[u.fst][u.snd] + 1;
			q.push({u.fst - 1, u.snd});
		}
		if (u.fst + 1 < N && block[u.fst + 1][u.snd] == INF)
		{
			block[u.fst + 1][u.snd] = block[u.fst][u.snd] + 1;
			q.push({u.fst + 1, u.snd});
		}
		if (u.snd && block[u.fst][u.snd - 1] == INF)
		{
			block[u.fst][u.snd - 1] = block[u.fst][u.snd] + 1;
			q.push({u.fst, u.snd - 1});
		}
		if (u.snd + 1 < N && block[u.fst][u.snd + 1] == INF)
		{
			block[u.fst][u.snd + 1] = block[u.fst][u.snd] + 1;
			q.push({u.fst, u.snd + 1});
		}
	}
}

inline bool BFS(pii s, int x)
{
	queue<pii> q;
	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < N; j++) dist[i][j] = INF;
	}
	dist[s.fst][s.snd] = x * S;
	q.push({s.fst, s.snd});
	while (q.size())
	{
		pii u = q.front(); q.pop();
		if (M[u.fst][u.snd] == 'D') return 1;
		if (u.fst && block[u.fst - 1][u.snd] > (dist[u.fst][u.snd] + 1) / S && dist[u.fst - 1][u.snd] == INF)
		{
			dist[u.fst - 1][u.snd] = dist[u.fst][u.snd] + 1;
			q.push({u.fst - 1, u.snd});
		}
		if (u.fst + 1 < N && block[u.fst + 1][u.snd] > (dist[u.fst][u.snd] + 1) / S && dist[u.fst + 1][u.snd] == INF)
		{
			dist[u.fst + 1][u.snd] = dist[u.fst][u.snd] + 1;
			q.push({u.fst + 1, u.snd});
		}
		if (u.snd && block[u.fst][u.snd - 1] > (dist[u.fst][u.snd] + 1) / S && dist[u.fst][u.snd - 1] == INF)
		{
			dist[u.fst][u.snd - 1] = dist[u.fst][u.snd] + 1;
			q.push({u.fst, u.snd - 1});
		}
		if (u.snd + 1 < N && block[u.fst][u.snd + 1] > (dist[u.fst][u.snd] + 1) / S && dist[u.fst][u.snd + 1] == INF)
		{
			dist[u.fst][u.snd + 1] = dist[u.fst][u.snd] + 1;
			q.push({u.fst, u.snd + 1});
		}
	}
	return 0;
} 

int main()
{
	ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
  	cin >> N >> S;
  	for (int i = 0; i < N; i++) cin >> M[i];
  	preBFS();
  	int si, sj;
  	for (int i = 0; i < N; i++)
  	{
    	for (int j = 0; j < N; j++)
    	{
      	if (M[i][j] == 'M') {si = i; sj = j; break;}
    	}
  	}
 	int L = 0, R = block[si][sj];
  	while (L < R)
  	{
    	int M = L + R >> 1;
    	if (BFS({si, sj}, M)) {L = M + 1;}
    	else {R = M;}
  	}
  	cout << L - 1 << "\n";
	return 0;
}

Compilation message

mecho.cpp: In function 'int main()':
mecho.cpp:114:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  114 |      int M = L + R >> 1;
      |              ~~^~~
mecho.cpp:111:14: warning: 'sj' may be used uninitialized in this function [-Wmaybe-uninitialized]
  111 |   int L = 0, R = block[si][sj];
      |              ^
mecho.cpp:111:14: warning: 'si' may be used uninitialized in this function [-Wmaybe-uninitialized]
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 95 ms 6764 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 716 KB Output is correct
13 Correct 1 ms 592 KB Output is correct
14 Correct 1 ms 716 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 1 ms 460 KB Output is correct
17 Correct 1 ms 480 KB Output is correct
18 Correct 1 ms 460 KB Output is correct
19 Correct 1 ms 460 KB Output is correct
20 Correct 1 ms 460 KB Output is correct
21 Correct 1 ms 592 KB Output is correct
22 Correct 1 ms 588 KB Output is correct
23 Correct 1 ms 588 KB Output is correct
24 Correct 1 ms 588 KB Output is correct
25 Correct 1 ms 716 KB Output is correct
26 Correct 1 ms 716 KB Output is correct
27 Correct 1 ms 716 KB Output is correct
28 Correct 1 ms 716 KB Output is correct
29 Correct 1 ms 716 KB Output is correct
30 Correct 1 ms 716 KB Output is correct
31 Correct 1 ms 716 KB Output is correct
32 Correct 1 ms 716 KB Output is correct
33 Correct 5 ms 2796 KB Output is correct
34 Correct 5 ms 2764 KB Output is correct
35 Correct 20 ms 2804 KB Output is correct
36 Correct 6 ms 3148 KB Output is correct
37 Correct 5 ms 3148 KB Output is correct
38 Correct 28 ms 3152 KB Output is correct
39 Correct 6 ms 3564 KB Output is correct
40 Correct 7 ms 3496 KB Output is correct
41 Correct 36 ms 3592 KB Output is correct
42 Correct 8 ms 3916 KB Output is correct
43 Correct 8 ms 3916 KB Output is correct
44 Correct 45 ms 3916 KB Output is correct
45 Correct 9 ms 4300 KB Output is correct
46 Correct 11 ms 4320 KB Output is correct
47 Correct 53 ms 4316 KB Output is correct
48 Correct 11 ms 4844 KB Output is correct
49 Correct 10 ms 4852 KB Output is correct
50 Correct 71 ms 4844 KB Output is correct
51 Correct 12 ms 5196 KB Output is correct
52 Correct 12 ms 5196 KB Output is correct
53 Correct 71 ms 5196 KB Output is correct
54 Correct 14 ms 5720 KB Output is correct
55 Correct 13 ms 5740 KB Output is correct
56 Correct 82 ms 5708 KB Output is correct
57 Correct 16 ms 6092 KB Output is correct
58 Correct 16 ms 6172 KB Output is correct
59 Correct 99 ms 6092 KB Output is correct
60 Correct 18 ms 6596 KB Output is correct
61 Correct 16 ms 6656 KB Output is correct
62 Correct 135 ms 6604 KB Output is correct
63 Correct 117 ms 6596 KB Output is correct
64 Correct 190 ms 6652 KB Output is correct
65 Correct 185 ms 6648 KB Output is correct
66 Correct 156 ms 6652 KB Output is correct
67 Correct 137 ms 6604 KB Output is correct
68 Correct 49 ms 6612 KB Output is correct
69 Correct 33 ms 6620 KB Output is correct
70 Correct 30 ms 6604 KB Output is correct
71 Correct 33 ms 6672 KB Output is correct
72 Correct 26 ms 6604 KB Output is correct
73 Correct 25 ms 6888 KB Output is correct
74 Correct 70 ms 6868 KB Output is correct
75 Correct 65 ms 6860 KB Output is correct
76 Correct 63 ms 6852 KB Output is correct
77 Correct 66 ms 6924 KB Output is correct
78 Correct 81 ms 6900 KB Output is correct
79 Correct 52 ms 6860 KB Output is correct
80 Correct 64 ms 7008 KB Output is correct
81 Correct 73 ms 6848 KB Output is correct
82 Correct 61 ms 6900 KB Output is correct
83 Correct 83 ms 6860 KB Output is correct
84 Correct 68 ms 6860 KB Output is correct
85 Correct 76 ms 6852 KB Output is correct
86 Correct 81 ms 6864 KB Output is correct
87 Correct 77 ms 6812 KB Output is correct
88 Correct 82 ms 6808 KB Output is correct
89 Correct 79 ms 6732 KB Output is correct
90 Correct 88 ms 6748 KB Output is correct
91 Correct 76 ms 6732 KB Output is correct
92 Correct 85 ms 6732 KB Output is correct