#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int h, w;
vector<string> grid;
vector<vector<bool>> vis;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen("input.in", "r", stdin);
cin >> h >> w;
grid = vector<string>(h);
vis = vector<vector<bool>>(h, vector<bool>(w, false));
for(int i=0; i<h; i++){
cin >> grid[i];
}
//bfs tree with connected components
//ans is depth of the tree
//can treat as edges with 0 weight to same component
//1 weight to edge to diff component
//use a kind of floodfill with bfs for the edges
deque<pair<int, int>> dq; dq.push_front({0,0});
char type = grid[0][0]; vis[0][0] = true;
int ans = 1;
while(!dq.empty()){
//floodfill bfs
pair<int, int> cur = dq.front(); dq.pop_front();
if(grid[cur.first][cur.second] != type){
ans++;
type = grid[cur.first][cur.second];
}
// cout << "CHECK" << endl;
for(int i=cur.first-1; i<=cur.first+1; i++){
if(i<0 || i>=h) continue;
// cout << "cHECKPINTS" << endl;
for(int j=cur.second-1; j<=cur.second+1; j++){
if(j<0 || j>=w) continue;
// cout << "testing " << i << " " << j << " with type " << grid[i][j] << endl;
if(vis[i][j] == true || grid[i][j] == '.' || abs((cur.first + cur.second) - (i+j)) != 1){
// cout << vis[i][j] << " " << grid[i][j] << " " << abs((cur.first+cur.second) - (i+j)) << endl;
continue;
}
else if(grid[i][j] != type){
dq.push_back({i, j});
// cout << "pushing to back" << endl;
}
else{
dq.push_front({i,j});
// cout << "pushing to front" << endl;
}
vis[i][j] = true;
}
}
}
cout << ans << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
848 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
7 ms |
848 KB |
Output is correct |
5 |
Correct |
2 ms |
592 KB |
Output is correct |
6 |
Correct |
1 ms |
508 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
2 ms |
592 KB |
Output is correct |
11 |
Correct |
2 ms |
592 KB |
Output is correct |
12 |
Correct |
4 ms |
592 KB |
Output is correct |
13 |
Correct |
2 ms |
600 KB |
Output is correct |
14 |
Correct |
3 ms |
592 KB |
Output is correct |
15 |
Correct |
11 ms |
848 KB |
Output is correct |
16 |
Correct |
11 ms |
848 KB |
Output is correct |
17 |
Correct |
9 ms |
848 KB |
Output is correct |
18 |
Correct |
6 ms |
848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
848 KB |
Output is correct |
2 |
Correct |
38 ms |
3936 KB |
Output is correct |
3 |
Correct |
171 ms |
36040 KB |
Output is correct |
4 |
Correct |
43 ms |
8492 KB |
Output is correct |
5 |
Correct |
121 ms |
20296 KB |
Output is correct |
6 |
Correct |
591 ms |
51096 KB |
Output is correct |
7 |
Correct |
2 ms |
848 KB |
Output is correct |
8 |
Correct |
3 ms |
848 KB |
Output is correct |
9 |
Correct |
2 ms |
472 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
2 ms |
848 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
30 ms |
3768 KB |
Output is correct |
14 |
Correct |
18 ms |
2384 KB |
Output is correct |
15 |
Correct |
12 ms |
2812 KB |
Output is correct |
16 |
Correct |
19 ms |
2040 KB |
Output is correct |
17 |
Correct |
85 ms |
9296 KB |
Output is correct |
18 |
Correct |
46 ms |
9040 KB |
Output is correct |
19 |
Correct |
45 ms |
8528 KB |
Output is correct |
20 |
Correct |
52 ms |
8016 KB |
Output is correct |
21 |
Correct |
102 ms |
20808 KB |
Output is correct |
22 |
Correct |
123 ms |
20204 KB |
Output is correct |
23 |
Correct |
165 ms |
17488 KB |
Output is correct |
24 |
Correct |
108 ms |
20296 KB |
Output is correct |
25 |
Correct |
261 ms |
36080 KB |
Output is correct |
26 |
Correct |
378 ms |
77664 KB |
Output is correct |
27 |
Correct |
502 ms |
76552 KB |
Output is correct |
28 |
Correct |
595 ms |
51016 KB |
Output is correct |
29 |
Correct |
577 ms |
48080 KB |
Output is correct |
30 |
Correct |
517 ms |
52680 KB |
Output is correct |
31 |
Correct |
413 ms |
23428 KB |
Output is correct |
32 |
Correct |
462 ms |
58980 KB |
Output is correct |