#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
#define fast_io ios::sync_with_stdio(0), cin.tie(0)
#define vi vector<int>
#define vvi vector<vi>
#define vs vector<string>
#define pb push_back
int n, m;
vs s;
inline bool in(vi& v) {
return -1 < v[0] && v[0] < n && -1 < v[1] && v[1] < m;
}
inline char f(vi& v) {
return s[v[0]][v[1]];
}
inline int g(vi& v) {
return v[0]*m+v[1];
}
inline bool bad(vi& v) {
return f(v) == '.';
}
inline bool same(vi& v, vi& w) {
return f(v) == f(w);
}
int main() {
fast_io;
vvi adj = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
cin >> n >> m;
s = vs(n); for(auto &t: s) cin >> t;
queue<vi> q, nq; vi vis(n*m); int ret = 0;
vi src = {0, 0};
q.push(src), vis[g(src)] = 1;
while(!q.empty()) {
ret++;
while(!q.empty()) {
vi v = q.front(); q.pop(), nq.push(v);
for(auto c: adj) {
vi w = {v[0]+c[0], v[1]+c[1]};
if(!in(w) || vis[g(w)] || bad(w) || !same(v, w)) continue;
q.push(w), vis[g(w)] = 1;
}
}
while(!nq.empty()) {
vi v = nq.front(); nq.pop();
for(auto c: adj) {
vi w = {v[0]+c[0], v[1]+c[1]};
if(!in(w) || vis[g(w)] || bad(w)) continue;
q.push(w), vis[g(w)] = 1;
}
}
}
cout << ret << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
54 ms |
5076 KB |
Output is correct |
5 |
Correct |
8 ms |
856 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
3 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
10 ms |
604 KB |
Output is correct |
11 |
Correct |
16 ms |
1628 KB |
Output is correct |
12 |
Correct |
29 ms |
1116 KB |
Output is correct |
13 |
Correct |
8 ms |
860 KB |
Output is correct |
14 |
Correct |
8 ms |
856 KB |
Output is correct |
15 |
Correct |
69 ms |
2140 KB |
Output is correct |
16 |
Correct |
82 ms |
2396 KB |
Output is correct |
17 |
Correct |
41 ms |
1628 KB |
Output is correct |
18 |
Correct |
55 ms |
5212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
860 KB |
Output is correct |
2 |
Correct |
247 ms |
8284 KB |
Output is correct |
3 |
Correct |
1281 ms |
81232 KB |
Output is correct |
4 |
Correct |
237 ms |
19288 KB |
Output is correct |
5 |
Correct |
1286 ms |
45660 KB |
Output is correct |
6 |
Execution timed out |
2078 ms |
279060 KB |
Time limit exceeded |
7 |
Correct |
5 ms |
860 KB |
Output is correct |
8 |
Correct |
5 ms |
860 KB |
Output is correct |
9 |
Correct |
10 ms |
1116 KB |
Output is correct |
10 |
Correct |
3 ms |
604 KB |
Output is correct |
11 |
Correct |
3 ms |
860 KB |
Output is correct |
12 |
Correct |
4 ms |
344 KB |
Output is correct |
13 |
Correct |
247 ms |
8284 KB |
Output is correct |
14 |
Correct |
136 ms |
4956 KB |
Output is correct |
15 |
Correct |
92 ms |
5468 KB |
Output is correct |
16 |
Correct |
127 ms |
3676 KB |
Output is correct |
17 |
Correct |
653 ms |
20828 KB |
Output is correct |
18 |
Correct |
358 ms |
20572 KB |
Output is correct |
19 |
Correct |
235 ms |
19292 KB |
Output is correct |
20 |
Correct |
309 ms |
17752 KB |
Output is correct |
21 |
Correct |
770 ms |
47192 KB |
Output is correct |
22 |
Correct |
1245 ms |
45656 KB |
Output is correct |
23 |
Correct |
1301 ms |
39768 KB |
Output is correct |
24 |
Correct |
925 ms |
46172 KB |
Output is correct |
25 |
Correct |
1588 ms |
81228 KB |
Output is correct |
26 |
Execution timed out |
2094 ms |
630724 KB |
Time limit exceeded |
27 |
Execution timed out |
2136 ms |
687312 KB |
Time limit exceeded |
28 |
Execution timed out |
2092 ms |
287024 KB |
Time limit exceeded |
29 |
Execution timed out |
2017 ms |
277652 KB |
Time limit exceeded |
30 |
Execution timed out |
2098 ms |
305912 KB |
Time limit exceeded |
31 |
Execution timed out |
2021 ms |
62568 KB |
Time limit exceeded |
32 |
Execution timed out |
2059 ms |
645708 KB |
Time limit exceeded |