#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define pp pop_back
#define mp make_pair
#define bb back
#define ff first
#define ss second
using namespace std;
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// freopen("tmp.in", "r", stdin);
// freopen("tmp.out", "w", stdout);
int H, W;
cin >> H >> W;
vector<string> grid(H);
for (int i = 0; i < H; i++)
cin >> grid[i];
vector<vector<bool> > used(H, vector<bool>(W, 0));
deque<pair<int, int> > q;
q.push_back({0, 0});
char last = '?';
int ans = 0;
pair<int, int> dir[4] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
while (!q.empty()) {
pair<int, int> now = q.front();
q.pop_front();
if (grid[now.ff][now.ss] != last) {
ans++;
last = grid[now.ff][now.ss];
}
for (pair<int, int> mv : dir) {
if (now.ff+mv.ff < 0 || now.ff+mv.ff >= H) continue;
if (now.ss+mv.ss < 0 || now.ss+mv.ss >= W) continue;
if (used[now.ff+mv.ff][now.ss+mv.ss]) continue;
if (grid[now.ff+mv.ff][now.ss+mv.ss] == '.') continue;
if (grid[now.ff+mv.ff][now.ss+mv.ss] == grid[now.ff][now.ss])
q.push_front({now.ff+mv.ff, now.ss+mv.ss});
else
q.push_back({now.ff+mv.ff, now.ss+mv.ss});
used[now.ff+mv.ff][now.ss+mv.ss] = 1;
}
}
cout << ans << '\n';
return 0;
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
724 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
4 ms |
724 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
4 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
7 ms |
596 KB |
Output is correct |
16 |
Correct |
9 ms |
596 KB |
Output is correct |
17 |
Correct |
6 ms |
596 KB |
Output is correct |
18 |
Correct |
4 ms |
680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
27 ms |
2212 KB |
Output is correct |
3 |
Correct |
144 ms |
20180 KB |
Output is correct |
4 |
Correct |
34 ms |
4880 KB |
Output is correct |
5 |
Correct |
116 ms |
11348 KB |
Output is correct |
6 |
Correct |
402 ms |
34028 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
724 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
27 ms |
2276 KB |
Output is correct |
14 |
Correct |
17 ms |
1364 KB |
Output is correct |
15 |
Correct |
13 ms |
1580 KB |
Output is correct |
16 |
Correct |
14 ms |
1108 KB |
Output is correct |
17 |
Correct |
79 ms |
5204 KB |
Output is correct |
18 |
Correct |
38 ms |
5204 KB |
Output is correct |
19 |
Correct |
41 ms |
4872 KB |
Output is correct |
20 |
Correct |
37 ms |
4504 KB |
Output is correct |
21 |
Correct |
83 ms |
11732 KB |
Output is correct |
22 |
Correct |
115 ms |
11352 KB |
Output is correct |
23 |
Correct |
144 ms |
9868 KB |
Output is correct |
24 |
Correct |
89 ms |
11464 KB |
Output is correct |
25 |
Correct |
251 ms |
20220 KB |
Output is correct |
26 |
Correct |
240 ms |
65628 KB |
Output is correct |
27 |
Correct |
321 ms |
48812 KB |
Output is correct |
28 |
Correct |
394 ms |
34060 KB |
Output is correct |
29 |
Correct |
402 ms |
34876 KB |
Output is correct |
30 |
Correct |
358 ms |
40516 KB |
Output is correct |
31 |
Correct |
377 ms |
13332 KB |
Output is correct |
32 |
Correct |
325 ms |
38360 KB |
Output is correct |