#include <iostream>
#include <vector>
#include <limits>
#include <map>
#include <algorithm>
#include <set>
#include <string>
#include <cmath>
#include <climits>
#include <string.h>
#include <fstream>
#include <stack>
#include <queue>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vb = vector<bool>;
using vvi= vector<vector<int> >;
using vll = vector<long long>;
using vpi = vector<pair<int, int> >;
#define pb push_back
#define rsz resize
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
using pi = pair<int, int>;
#define f first
#define s second
#define mp make_pair
//cin.tie(0)->sync_with_stdio(0);
int N, S;
vi dx, dy;
pi end;
vector<vector<char> > field;
vvi fromHive;
void BFS(int a, int b, vvi& fromMecho, int ad){
queue<pair<pair<int, int>, int> > q;
q.push(mp(mp(a, b), 0));
while(!q.empty()){
pair<pi, int> curr = q.front(); q.pop();
if(field[curr.f.f][curr.f.s] == 'T'){
continue;
}
if(fromHive[curr.f.f][curr.f.s] < curr.s/S + ad + (curr.s%S == 0 ? 0 : 1)){
continue;
}
if(curr.s%S == 0){
if(fromHive[curr.f.f][curr.f.s] == curr.s/S + ad + (curr.s%S == 0 ? 0 : 1)){
continue;
}
}
if(fromMecho[curr.f.f][curr.f.s] > curr.s/S + ad + (curr.s%S == 0 ? 0 : 1)){
fromMecho[curr.f.f][curr.f.s] = curr.s/S + ad + (curr.s%S == 0 ? 0 : 1);
for (int i = 0; i < 4; i++)
{
if(curr.f.f + dx[i] < 0 || curr.f.f + dx[i] >= N || curr.f.s + dy[i] < 0 || curr.f.s >= N){
continue;
}
q.push(mp(mp(curr.f.f + dx[i], curr.f.s + dy[i]), curr.s + 1));
}
}
}
}
int main(){
cin >> N >> S;
dx = {-1, 1, 0, 0};
dy = {0, 0, -1, 1};
vi row(N, INT_MAX);
fromHive.rsz(N);
field.rsz(N);
for (int i = 0; i < N; i++)
{
fromHive[i] = row;
field[i].rsz(N);
}
pi start, end;
vpi hive;
for (int i = 0; i < N; i++)
{
string str; cin >> str;
for (int j = 0; j < N; j++)
{
field[i][j] = str[j];
if(str[j] == 'M'){
start = mp(i, j);
}
if(str[j] == 'D'){
end = mp(i, j);
}
if(str[j] == 'H'){
hive.pb(mp(i, j));
}
}
}
queue<pair<pair<int, int>, int> > q;
for (int i = 0; i < sz(hive); i++)
{
q.push(mp(hive[i], 0));
}
while(!q.empty()){
pair<pi, int> curr = q.front(); q.pop();
if(field[curr.f.f][curr.f.s] != 'G' && field[curr.f.f][curr.f.s] != 'H'){
continue;
}
if(fromHive[curr.f.f][curr.f.s] > curr.s){
fromHive[curr.f.f][curr.f.s] = curr.s;
for (int i = 0; i < 4; i++)
{
if(curr.f.f + dx[i] < 0 || curr.f.f + dx[i] >= N || curr.f.s + dy[i] < 0 || curr.f.s + dy[i] >= N){
continue;
}
q.push(mp(mp(curr.f.f + dx[i], curr.f.s + dy[i]), curr.s + 1));
}
}
}
int lo = 0, hi = INT_MAX/2;
while(lo < hi){
int mid = (lo + hi + 1)/2;
vi row(N, INT_MAX);
vvi fromMecho(N);
for (int i = 0; i < N; i++)
{
fromMecho[i] = row;
}
BFS(start.f, start.s, fromMecho, mid);
// for (int i = 0; i < sz(fromMecho); i++)
// {
// for (int j = 0; j < N; j++)
// {
// cout << fromMecho[i][j] << " ";
// }
// cout << endl;
// }
// cout << endl;
if(fromMecho[end.f][end.s] == INT_MAX){
hi = mid - 1;
} else {
lo = mid;
}
}
cout << lo;
// for (int i = 0; i < N; i++)
// {
// for (int j = 0; j < N; j++)
// {
// cout << fromHive[i][j] << " ";
// }
// cout << endl;
// }
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 6 |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
149 ms |
6072 KB |
Output is correct |
8 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
13 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 6 |
14 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
7 ms |
1308 KB |
Output is correct |
34 |
Correct |
8 ms |
1420 KB |
Output is correct |
35 |
Runtime error |
13 ms |
2668 KB |
Execution killed with signal 6 |
36 |
Correct |
10 ms |
1776 KB |
Output is correct |
37 |
Correct |
8 ms |
1748 KB |
Output is correct |
38 |
Correct |
46 ms |
1760 KB |
Output is correct |
39 |
Correct |
10 ms |
2096 KB |
Output is correct |
40 |
Correct |
11 ms |
2132 KB |
Output is correct |
41 |
Runtime error |
17 ms |
4092 KB |
Execution killed with signal 6 |
42 |
Correct |
13 ms |
2556 KB |
Output is correct |
43 |
Correct |
15 ms |
2516 KB |
Output is correct |
44 |
Correct |
73 ms |
2644 KB |
Output is correct |
45 |
Correct |
14 ms |
2968 KB |
Output is correct |
46 |
Correct |
15 ms |
2968 KB |
Output is correct |
47 |
Runtime error |
24 ms |
5984 KB |
Execution killed with signal 6 |
48 |
Correct |
17 ms |
3508 KB |
Output is correct |
49 |
Correct |
19 ms |
3640 KB |
Output is correct |
50 |
Correct |
125 ms |
3652 KB |
Output is correct |
51 |
Correct |
20 ms |
4084 KB |
Output is correct |
52 |
Correct |
22 ms |
4088 KB |
Output is correct |
53 |
Runtime error |
33 ms |
8148 KB |
Execution killed with signal 6 |
54 |
Correct |
25 ms |
4704 KB |
Output is correct |
55 |
Correct |
27 ms |
4700 KB |
Output is correct |
56 |
Correct |
155 ms |
4760 KB |
Output is correct |
57 |
Correct |
24 ms |
5332 KB |
Output is correct |
58 |
Correct |
24 ms |
5280 KB |
Output is correct |
59 |
Runtime error |
55 ms |
10684 KB |
Execution killed with signal 6 |
60 |
Correct |
29 ms |
5920 KB |
Output is correct |
61 |
Correct |
30 ms |
6012 KB |
Output is correct |
62 |
Correct |
213 ms |
6108 KB |
Output is correct |
63 |
Correct |
145 ms |
6116 KB |
Output is correct |
64 |
Correct |
230 ms |
6188 KB |
Output is correct |
65 |
Correct |
229 ms |
6092 KB |
Output is correct |
66 |
Correct |
193 ms |
6112 KB |
Output is correct |
67 |
Incorrect |
174 ms |
6088 KB |
Output isn't correct |
68 |
Correct |
76 ms |
6132 KB |
Output is correct |
69 |
Correct |
69 ms |
6132 KB |
Output is correct |
70 |
Correct |
65 ms |
6148 KB |
Output is correct |
71 |
Correct |
65 ms |
6144 KB |
Output is correct |
72 |
Incorrect |
62 ms |
6128 KB |
Output isn't correct |
73 |
Incorrect |
113 ms |
6188 KB |
Output isn't correct |
74 |
Correct |
96 ms |
6164 KB |
Output is correct |
75 |
Correct |
114 ms |
6196 KB |
Output is correct |
76 |
Correct |
104 ms |
6116 KB |
Output is correct |
77 |
Correct |
106 ms |
6160 KB |
Output is correct |
78 |
Incorrect |
135 ms |
6200 KB |
Output isn't correct |
79 |
Correct |
89 ms |
6188 KB |
Output is correct |
80 |
Correct |
104 ms |
6160 KB |
Output is correct |
81 |
Correct |
118 ms |
6184 KB |
Output is correct |
82 |
Correct |
108 ms |
6192 KB |
Output is correct |
83 |
Correct |
132 ms |
6084 KB |
Output is correct |
84 |
Correct |
120 ms |
6100 KB |
Output is correct |
85 |
Correct |
122 ms |
6152 KB |
Output is correct |
86 |
Correct |
131 ms |
6160 KB |
Output is correct |
87 |
Correct |
130 ms |
6176 KB |
Output is correct |
88 |
Correct |
135 ms |
6156 KB |
Output is correct |
89 |
Correct |
135 ms |
6148 KB |
Output is correct |
90 |
Correct |
144 ms |
6156 KB |
Output is correct |
91 |
Correct |
135 ms |
6064 KB |
Output is correct |
92 |
Correct |
136 ms |
6076 KB |
Output is correct |