#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++; vvi add;
while(!q.empty()) {
vi v = q.front(); q.pop();
for(auto c: adj) {
vi w = {v[0]+c[0], v[1]+c[1]};
if(!in(w) || vis[g(w)] || bad(w)) continue;
if(same(v, w)) q.push(w); else
nq.push(w);
vis[g(w)] = 1;
}
}
while(!nq.empty()) {
vi v = nq.front(); nq.pop(), q.push(v);
}
}
cout << ret << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
2136 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
27 ms |
2140 KB |
Output is correct |
5 |
Correct |
5 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 |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
6 ms |
884 KB |
Output is correct |
11 |
Correct |
7 ms |
860 KB |
Output is correct |
12 |
Correct |
16 ms |
1080 KB |
Output is correct |
13 |
Correct |
5 ms |
860 KB |
Output is correct |
14 |
Correct |
4 ms |
820 KB |
Output is correct |
15 |
Correct |
35 ms |
1884 KB |
Output is correct |
16 |
Correct |
47 ms |
2136 KB |
Output is correct |
17 |
Correct |
24 ms |
1880 KB |
Output is correct |
18 |
Correct |
26 ms |
2280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
856 KB |
Output is correct |
2 |
Correct |
131 ms |
9560 KB |
Output is correct |
3 |
Correct |
700 ms |
89224 KB |
Output is correct |
4 |
Correct |
130 ms |
21476 KB |
Output is correct |
5 |
Correct |
771 ms |
50260 KB |
Output is correct |
6 |
Execution timed out |
2103 ms |
156644 KB |
Time limit exceeded |
7 |
Correct |
3 ms |
600 KB |
Output is correct |
8 |
Correct |
3 ms |
604 KB |
Output is correct |
9 |
Correct |
6 ms |
752 KB |
Output is correct |
10 |
Correct |
2 ms |
600 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
3 ms |
348 KB |
Output is correct |
13 |
Correct |
128 ms |
9428 KB |
Output is correct |
14 |
Correct |
78 ms |
5720 KB |
Output is correct |
15 |
Correct |
50 ms |
5976 KB |
Output is correct |
16 |
Correct |
68 ms |
4188 KB |
Output is correct |
17 |
Correct |
347 ms |
23064 KB |
Output is correct |
18 |
Correct |
194 ms |
22608 KB |
Output is correct |
19 |
Correct |
129 ms |
21444 KB |
Output is correct |
20 |
Correct |
165 ms |
19796 KB |
Output is correct |
21 |
Correct |
416 ms |
51792 KB |
Output is correct |
22 |
Correct |
779 ms |
50172 KB |
Output is correct |
23 |
Correct |
715 ms |
43600 KB |
Output is correct |
24 |
Correct |
508 ms |
50572 KB |
Output is correct |
25 |
Correct |
936 ms |
89168 KB |
Output is correct |
26 |
Correct |
1956 ms |
68912 KB |
Output is correct |
27 |
Execution timed out |
2037 ms |
91984 KB |
Time limit exceeded |
28 |
Execution timed out |
2070 ms |
144232 KB |
Time limit exceeded |
29 |
Execution timed out |
2075 ms |
138440 KB |
Time limit exceeded |
30 |
Execution timed out |
2068 ms |
131248 KB |
Time limit exceeded |
31 |
Execution timed out |
2066 ms |
54520 KB |
Time limit exceeded |
32 |
Execution timed out |
2041 ms |
85952 KB |
Time limit exceeded |