#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vl vector<ll>
#define mp make_pair
#define pb push_back
using namespace std;
int n, s,sx, sy, ex, ey;
const int Nmax = 1000;
char mat[Nmax][Nmax];
int dist[Nmax][Nmax];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
vector<pii> hives;
class state{
public:
int x;
int y;
int steps_made;
int cur_minute;
};
bool in_range(int i, int j){
if(i >= 0 && j >= 0 && i < n && j < n)
return true;
return false;
}
void calculate_dist(){
queue<vector<pii> > q;
q.push(hives);
while(!q.empty()){
vector<pii> current = q.front(); q.pop();
vector<pii> next_current;
for(int i = 0; i < current.size(); i++){
int x = current[i].first, y = current[i].second;
for(int dir = 0; dir < 4; dir++){
int nx = x + dx[dir], ny = y + dy[dir];
if(in_range(nx, ny) && mat[nx][ny] != 'T' && dist[nx][ny] == -1){
dist[nx][ny] = 1 + dist[x][y];
next_current.pb(mp(nx,ny));
}
}
}
if(next_current.size() > 0)
q.push(next_current);
}
}
bool solve(int minutes){
queue<state> q;
q.push({sx,sy,0,minutes+1});
bool finished = false;
bool vis[801][801];
memset(vis, false, sizeof(vis));
vis[sx][sy] = true;
while(!q.empty()){
state current = q.front(); q.pop();
int x = current.x, y = current.y, steps_made = current.steps_made, cur_minute = current.cur_minute;
//cout<<x<<" "<<y<<" "<<steps_made<<" "<<cur_minute<<endl;
if(steps_made == s){
steps_made = 0;
cur_minute++;
}
for(int dir = 0; dir < 4; dir++){
int nx = x + dx[dir], ny = y + dy[dir];
//cout<<dist[nx][ny]<<" "<<cur_minute<<endl;
if(in_range(nx,ny) && mat[nx][ny] != 'T' && !vis[nx][ny] && dist[nx][ny] >= cur_minute){
if(dist[nx][ny] == cur_minute && steps_made + 1 == s)
continue;
if(nx == ex && ny == ey){
finished = true;
break;
}
vis[nx][ny] = true;
q.push({nx, ny, steps_made + 1, cur_minute});
}
}
if(finished)
break;
}
return finished;
}
int main(){
cin>>n>>s;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
cin>>mat[i][j];
if(mat[i][j] == 'M')
sx = i, sy = j;
if(mat[i][j] == 'D')
ex = i, ey = j;
if(mat[i][j] == 'H')
hives.pb(mp(i,j));
}
}
memset(dist, -1, sizeof(dist));
for(int i = 0; i < hives.size(); i++)
dist[hives[i].first][hives[i].second] = 0;
calculate_dist();
int l = 0, r = 10000, solution=-1;
while(l <= r){
int mid = (l + r) / 2;
bool sol = solve(mid);
if(sol){
solution = mid;
l = mid + 1;
}
else
r = mid - 1;
}
cout<<solution<<endl;
}
Compilation message
mecho.cpp: In function 'void calculate_dist()':
mecho.cpp:35:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for(int i = 0; i < current.size(); i++){
| ~~^~~~~~~~~~~~~~~~
mecho.cpp: In function 'int main()':
mecho.cpp:98:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
98 | for(int i = 0; i < hives.size(); i++)
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4812 KB |
Output is correct |
2 |
Incorrect |
2 ms |
4812 KB |
Output isn't correct |
3 |
Correct |
2 ms |
4812 KB |
Output is correct |
4 |
Incorrect |
2 ms |
4812 KB |
Output isn't correct |
5 |
Correct |
2 ms |
4812 KB |
Output is correct |
6 |
Incorrect |
3 ms |
4812 KB |
Output isn't correct |
7 |
Incorrect |
115 ms |
5680 KB |
Output isn't correct |
8 |
Correct |
2 ms |
4812 KB |
Output is correct |
9 |
Correct |
3 ms |
4812 KB |
Output is correct |
10 |
Correct |
2 ms |
4812 KB |
Output is correct |
11 |
Correct |
3 ms |
4812 KB |
Output is correct |
12 |
Incorrect |
3 ms |
4808 KB |
Output isn't correct |
13 |
Incorrect |
3 ms |
4836 KB |
Output isn't correct |
14 |
Correct |
3 ms |
4812 KB |
Output is correct |
15 |
Correct |
3 ms |
4812 KB |
Output is correct |
16 |
Correct |
3 ms |
4812 KB |
Output is correct |
17 |
Correct |
2 ms |
4812 KB |
Output is correct |
18 |
Correct |
3 ms |
4812 KB |
Output is correct |
19 |
Correct |
3 ms |
4812 KB |
Output is correct |
20 |
Correct |
2 ms |
4812 KB |
Output is correct |
21 |
Correct |
3 ms |
4812 KB |
Output is correct |
22 |
Correct |
3 ms |
4812 KB |
Output is correct |
23 |
Correct |
3 ms |
4812 KB |
Output is correct |
24 |
Correct |
3 ms |
4812 KB |
Output is correct |
25 |
Correct |
3 ms |
4812 KB |
Output is correct |
26 |
Correct |
3 ms |
4812 KB |
Output is correct |
27 |
Correct |
3 ms |
4812 KB |
Output is correct |
28 |
Correct |
3 ms |
4812 KB |
Output is correct |
29 |
Correct |
3 ms |
4812 KB |
Output is correct |
30 |
Correct |
3 ms |
4812 KB |
Output is correct |
31 |
Correct |
3 ms |
4812 KB |
Output is correct |
32 |
Correct |
3 ms |
4812 KB |
Output is correct |
33 |
Incorrect |
14 ms |
5100 KB |
Output isn't correct |
34 |
Incorrect |
13 ms |
5068 KB |
Output isn't correct |
35 |
Correct |
27 ms |
5252 KB |
Output is correct |
36 |
Incorrect |
16 ms |
5188 KB |
Output isn't correct |
37 |
Incorrect |
16 ms |
5156 KB |
Output isn't correct |
38 |
Correct |
34 ms |
5200 KB |
Output is correct |
39 |
Incorrect |
21 ms |
5196 KB |
Output isn't correct |
40 |
Incorrect |
19 ms |
5196 KB |
Output isn't correct |
41 |
Correct |
42 ms |
5324 KB |
Output is correct |
42 |
Incorrect |
23 ms |
5188 KB |
Output isn't correct |
43 |
Incorrect |
24 ms |
5204 KB |
Output isn't correct |
44 |
Correct |
51 ms |
5380 KB |
Output is correct |
45 |
Incorrect |
28 ms |
5324 KB |
Output isn't correct |
46 |
Incorrect |
28 ms |
5316 KB |
Output isn't correct |
47 |
Correct |
61 ms |
5332 KB |
Output is correct |
48 |
Incorrect |
34 ms |
5284 KB |
Output isn't correct |
49 |
Incorrect |
37 ms |
5288 KB |
Output isn't correct |
50 |
Correct |
93 ms |
5492 KB |
Output is correct |
51 |
Incorrect |
38 ms |
5436 KB |
Output isn't correct |
52 |
Incorrect |
37 ms |
5444 KB |
Output isn't correct |
53 |
Correct |
91 ms |
5544 KB |
Output is correct |
54 |
Incorrect |
44 ms |
5384 KB |
Output isn't correct |
55 |
Incorrect |
44 ms |
5464 KB |
Output isn't correct |
56 |
Correct |
122 ms |
5480 KB |
Output is correct |
57 |
Incorrect |
56 ms |
5536 KB |
Output isn't correct |
58 |
Incorrect |
48 ms |
5480 KB |
Output isn't correct |
59 |
Correct |
118 ms |
5640 KB |
Output is correct |
60 |
Incorrect |
57 ms |
5500 KB |
Output isn't correct |
61 |
Incorrect |
62 ms |
5488 KB |
Output isn't correct |
62 |
Correct |
141 ms |
5692 KB |
Output is correct |
63 |
Correct |
122 ms |
5572 KB |
Output is correct |
64 |
Correct |
222 ms |
5664 KB |
Output is correct |
65 |
Correct |
185 ms |
5680 KB |
Output is correct |
66 |
Correct |
143 ms |
5684 KB |
Output is correct |
67 |
Correct |
134 ms |
5572 KB |
Output is correct |
68 |
Correct |
68 ms |
5624 KB |
Output is correct |
69 |
Correct |
84 ms |
5732 KB |
Output is correct |
70 |
Correct |
65 ms |
5656 KB |
Output is correct |
71 |
Correct |
63 ms |
5632 KB |
Output is correct |
72 |
Incorrect |
53 ms |
5620 KB |
Output isn't correct |
73 |
Incorrect |
65 ms |
6328 KB |
Output isn't correct |
74 |
Correct |
78 ms |
6368 KB |
Output is correct |
75 |
Correct |
90 ms |
6320 KB |
Output is correct |
76 |
Correct |
102 ms |
6420 KB |
Output is correct |
77 |
Correct |
84 ms |
6392 KB |
Output is correct |
78 |
Correct |
103 ms |
5988 KB |
Output is correct |
79 |
Correct |
81 ms |
6040 KB |
Output is correct |
80 |
Correct |
81 ms |
5980 KB |
Output is correct |
81 |
Correct |
99 ms |
6088 KB |
Output is correct |
82 |
Correct |
110 ms |
5940 KB |
Output is correct |
83 |
Correct |
99 ms |
5992 KB |
Output is correct |
84 |
Correct |
89 ms |
5996 KB |
Output is correct |
85 |
Correct |
97 ms |
5932 KB |
Output is correct |
86 |
Correct |
100 ms |
6060 KB |
Output is correct |
87 |
Correct |
103 ms |
6000 KB |
Output is correct |
88 |
Correct |
100 ms |
5976 KB |
Output is correct |
89 |
Correct |
109 ms |
5908 KB |
Output is correct |
90 |
Correct |
124 ms |
5920 KB |
Output is correct |
91 |
Correct |
103 ms |
6000 KB |
Output is correct |
92 |
Correct |
108 ms |
5996 KB |
Output is correct |