답안 #74491

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
74491 2018-09-02T13:10:44 Z JustInCase Mecho (IOI09_mecho) C++17
100 / 100
306 ms 11704 KB
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;

const int64_t MAX_N = 800;

const int64_t DELTA_ROWS[] = { -1, 0, 1, 0 };
const int64_t DELTA_COLS[] = { 0, 1, 0, -1 };

char board[MAX_N + 5][MAX_N + 5];
int64_t n, s, startR, startC, endR, endC, beeTimes[MAX_N + 5][MAX_N + 5];
int64_t mechoTimes[MAX_N + 5][MAX_N + 5];
vector< pair< int64_t, int64_t > > hives;

void PrecomputeBeeTimes() {
	queue< pair< int64_t, int64_t > > q;

	memset(beeTimes, -1, sizeof(beeTimes));
	for(auto &x : hives) {
		q.push(x);
		beeTimes[x.first][x.second] = 0;
	}

	while(!q.empty()) {
		int64_t r = q.front().first, c = q.front().second;
		int64_t time = beeTimes[r][c];
		q.pop();

		for(int64_t p = 0; p < 4; p++) {
			int64_t newR = r + DELTA_ROWS[p];
			int64_t newC = c + DELTA_COLS[p];

			if(newR >= 0 && newR < n && newC >= 0 && newC < n 
					&& board[newR][newC] == 'G' && beeTimes[newR][newC] == -1) {
				q.push({ newR, newC });
				beeTimes[newR][newC] = time + 1;
			}
		}
	}
}

bool Check(int64_t startTime) {
	queue< pair< int64_t, int64_t > > q;

	memset(mechoTimes, -1, sizeof(mechoTimes));
	mechoTimes[startR][startC] = startTime;
	
	if(startTime >= beeTimes[startR][startC]) {
		return false;
	}

	q.push({ startR, startC });

	while(!q.empty()) {
		int64_t r = q.front().first, c = q.front().second;
		int64_t nxtTime = mechoTimes[r][c] + 1;
		q.pop();
			
		if(r == endR && c == endC) {
			return true;
		}

		int64_t maxBeeTime;
		if(nxtTime % s == 0) {
			maxBeeTime = nxtTime + 1;
		}
		else {
			maxBeeTime = nxtTime;
		}
		
		for(int64_t p = 0; p < 4; p++) {
			int64_t newR = r + DELTA_ROWS[p];
			int64_t newC = c + DELTA_COLS[p];

			if(newR >= 0 && newR < n && newC >= 0 && newC < n 
					&& (board[newR][newC] == 'G' || board[newR][newC] == 'D')
					&& mechoTimes[newR][newC] == -1
					&& (beeTimes[newR][newC] >= maxBeeTime 
						|| beeTimes[newR][newC] < 0)) {
				q.push({ newR, newC });
				mechoTimes[newR][newC] = nxtTime;
			}
		}
	}

	return false;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);

	cin >> n >> s;

	for(int64_t i = 0; i < n; i++) {
		for(int64_t j = 0; j < n; j++) {
			cin >> board[i][j];
		
			if(board[i][j] == 'M') {
				startR = i;
				startC = j;
				board[i][j] = 'G';
			}
			else if(board[i][j] == 'D') {
				endR = i;
				endC = j;
			}
			else if(board[i][j] == 'H') {
				hives.push_back({ i, j });
			}
		}
	}

	PrecomputeBeeTimes();

	for(int64_t i = 0; i < n; i++) {
		for(int64_t j = 0; j < n; j++) {
			beeTimes[i][j] *= s;
		}	
	}

	int64_t low = 0, high = MAX_N * MAX_N * MAX_N, ans = -1;
	while(low <= high) {
		int64_t mid = (low + high) / 2;

		if(Check(mid * s)) {
			low = mid + 1;
			ans = mid;
		}
		else {
			high = mid - 1;
		}
	}

	cout << ans << endl;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 10488 KB Output is correct
2 Correct 17 ms 10672 KB Output is correct
3 Correct 18 ms 10672 KB Output is correct
4 Correct 17 ms 10672 KB Output is correct
5 Correct 19 ms 10672 KB Output is correct
6 Correct 29 ms 10672 KB Output is correct
7 Correct 176 ms 11704 KB Output is correct
8 Correct 17 ms 11704 KB Output is correct
9 Correct 18 ms 11704 KB Output is correct
10 Correct 17 ms 11704 KB Output is correct
11 Correct 17 ms 11704 KB Output is correct
12 Correct 17 ms 11704 KB Output is correct
13 Correct 18 ms 11704 KB Output is correct
14 Correct 19 ms 11704 KB Output is correct
15 Correct 19 ms 11704 KB Output is correct
16 Correct 17 ms 11704 KB Output is correct
17 Correct 18 ms 11704 KB Output is correct
18 Correct 18 ms 11704 KB Output is correct
19 Correct 17 ms 11704 KB Output is correct
20 Correct 17 ms 11704 KB Output is correct
21 Correct 37 ms 11704 KB Output is correct
22 Correct 18 ms 11704 KB Output is correct
23 Correct 19 ms 11704 KB Output is correct
24 Correct 20 ms 11704 KB Output is correct
25 Correct 17 ms 11704 KB Output is correct
26 Correct 17 ms 11704 KB Output is correct
27 Correct 17 ms 11704 KB Output is correct
28 Correct 18 ms 11704 KB Output is correct
29 Correct 18 ms 11704 KB Output is correct
30 Correct 18 ms 11704 KB Output is correct
31 Correct 18 ms 11704 KB Output is correct
32 Correct 18 ms 11704 KB Output is correct
33 Correct 30 ms 11704 KB Output is correct
34 Correct 23 ms 11704 KB Output is correct
35 Correct 54 ms 11704 KB Output is correct
36 Correct 22 ms 11704 KB Output is correct
37 Correct 22 ms 11704 KB Output is correct
38 Correct 60 ms 11704 KB Output is correct
39 Correct 23 ms 11704 KB Output is correct
40 Correct 23 ms 11704 KB Output is correct
41 Correct 80 ms 11704 KB Output is correct
42 Correct 25 ms 11704 KB Output is correct
43 Correct 26 ms 11704 KB Output is correct
44 Correct 99 ms 11704 KB Output is correct
45 Correct 27 ms 11704 KB Output is correct
46 Correct 26 ms 11704 KB Output is correct
47 Correct 131 ms 11704 KB Output is correct
48 Correct 28 ms 11704 KB Output is correct
49 Correct 30 ms 11704 KB Output is correct
50 Correct 160 ms 11704 KB Output is correct
51 Correct 28 ms 11704 KB Output is correct
52 Correct 30 ms 11704 KB Output is correct
53 Correct 185 ms 11704 KB Output is correct
54 Correct 37 ms 11704 KB Output is correct
55 Correct 32 ms 11704 KB Output is correct
56 Correct 183 ms 11704 KB Output is correct
57 Correct 31 ms 11704 KB Output is correct
58 Correct 32 ms 11704 KB Output is correct
59 Correct 209 ms 11704 KB Output is correct
60 Correct 35 ms 11704 KB Output is correct
61 Correct 33 ms 11704 KB Output is correct
62 Correct 251 ms 11704 KB Output is correct
63 Correct 198 ms 11704 KB Output is correct
64 Correct 306 ms 11704 KB Output is correct
65 Correct 298 ms 11704 KB Output is correct
66 Correct 241 ms 11704 KB Output is correct
67 Correct 202 ms 11704 KB Output is correct
68 Correct 80 ms 11704 KB Output is correct
69 Correct 83 ms 11704 KB Output is correct
70 Correct 77 ms 11704 KB Output is correct
71 Correct 60 ms 11704 KB Output is correct
72 Correct 58 ms 11704 KB Output is correct
73 Correct 82 ms 11704 KB Output is correct
74 Correct 107 ms 11704 KB Output is correct
75 Correct 121 ms 11704 KB Output is correct
76 Correct 140 ms 11704 KB Output is correct
77 Correct 124 ms 11704 KB Output is correct
78 Correct 143 ms 11704 KB Output is correct
79 Correct 164 ms 11704 KB Output is correct
80 Correct 121 ms 11704 KB Output is correct
81 Correct 147 ms 11704 KB Output is correct
82 Correct 112 ms 11704 KB Output is correct
83 Correct 199 ms 11704 KB Output is correct
84 Correct 182 ms 11704 KB Output is correct
85 Correct 120 ms 11704 KB Output is correct
86 Correct 141 ms 11704 KB Output is correct
87 Correct 166 ms 11704 KB Output is correct
88 Correct 154 ms 11704 KB Output is correct
89 Correct 188 ms 11704 KB Output is correct
90 Correct 151 ms 11704 KB Output is correct
91 Correct 140 ms 11704 KB Output is correct
92 Correct 238 ms 11704 KB Output is correct