#include <bits/stdc++.h>
using namespace std;
#undef _GLIBCXX_DEBUG
#define rep(i, a, b) for(ll i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;
vector<vector<char>> grid;
bool visited[4010][4010];
ll H, W;
vector<pair<ll,ll>> q;
vector<pair<ll,ll>> q2;
ll dfs(ll row, ll col, char curr){
if (row >= H || col >= W || col < 0 || row <0) return 0;
if (visited[row][col] == 1) return 0;
if (grid[row][col] =='.') return 0;
if (grid[row][col] != curr){
q2.push_back(make_pair(row,col));
//cout << "in bfs " << row << " " << col << "\n";
return 0;
}
visited[row][col] = 1;
dfs(row+1, col,curr);
dfs(row-1, col,curr);
dfs(row, col+1,curr);
dfs(row, col-1,curr);
return 0;
}
int main(){
//cin.tie(0)->sync_with_stdio(0);
memset(visited, 0, sizeof(visited));
cin >>H >> W;
grid.resize(H, vector<char>(W));
char temp;
rep(i,0,H)
rep (j,0,W){
cin >> temp;
grid[i][j] = temp;
}
char ani = grid[0][0];
q.push_back(make_pair(0,0));
ll cnt =0;
while (q.size()){
while(!q.empty()){
pll s = q.back();
q.pop_back();
dfs(s.first, s.second, grid[s.first][s.second]);
}
//cout << "new q " << "\n";
cnt ++;
swap(q, q2);
q2.clear();
if (ani == 'R') ani = 'F';
if (ani == 'F') ani = 'R';
}
cout << cnt << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
17368 KB |
Output is correct |
2 |
Correct |
2 ms |
15964 KB |
Output is correct |
3 |
Correct |
3 ms |
15964 KB |
Output is correct |
4 |
Correct |
12 ms |
18132 KB |
Output is correct |
5 |
Correct |
6 ms |
16220 KB |
Output is correct |
6 |
Correct |
2 ms |
15964 KB |
Output is correct |
7 |
Correct |
3 ms |
15964 KB |
Output is correct |
8 |
Correct |
3 ms |
16220 KB |
Output is correct |
9 |
Correct |
3 ms |
15960 KB |
Output is correct |
10 |
Correct |
6 ms |
16220 KB |
Output is correct |
11 |
Correct |
6 ms |
16744 KB |
Output is correct |
12 |
Correct |
8 ms |
16604 KB |
Output is correct |
13 |
Correct |
6 ms |
16340 KB |
Output is correct |
14 |
Correct |
5 ms |
16220 KB |
Output is correct |
15 |
Correct |
17 ms |
16948 KB |
Output is correct |
16 |
Correct |
19 ms |
17368 KB |
Output is correct |
17 |
Correct |
14 ms |
16732 KB |
Output is correct |
18 |
Correct |
12 ms |
18132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
16476 KB |
Output is correct |
2 |
Correct |
72 ms |
19288 KB |
Output is correct |
3 |
Correct |
605 ms |
40580 KB |
Output is correct |
4 |
Correct |
136 ms |
22100 KB |
Output is correct |
5 |
Correct |
364 ms |
30036 KB |
Output is correct |
6 |
Correct |
1006 ms |
144148 KB |
Output is correct |
7 |
Correct |
4 ms |
16472 KB |
Output is correct |
8 |
Correct |
5 ms |
16476 KB |
Output is correct |
9 |
Correct |
5 ms |
16636 KB |
Output is correct |
10 |
Correct |
4 ms |
16472 KB |
Output is correct |
11 |
Correct |
4 ms |
16476 KB |
Output is correct |
12 |
Correct |
3 ms |
16228 KB |
Output is correct |
13 |
Correct |
69 ms |
19024 KB |
Output is correct |
14 |
Correct |
43 ms |
18000 KB |
Output is correct |
15 |
Correct |
38 ms |
17928 KB |
Output is correct |
16 |
Correct |
35 ms |
17576 KB |
Output is correct |
17 |
Correct |
179 ms |
22864 KB |
Output is correct |
18 |
Correct |
145 ms |
22608 KB |
Output is correct |
19 |
Correct |
129 ms |
22100 KB |
Output is correct |
20 |
Correct |
134 ms |
21840 KB |
Output is correct |
21 |
Correct |
336 ms |
30544 KB |
Output is correct |
22 |
Correct |
365 ms |
30032 KB |
Output is correct |
23 |
Correct |
348 ms |
28448 KB |
Output is correct |
24 |
Correct |
345 ms |
30032 KB |
Output is correct |
25 |
Correct |
609 ms |
40520 KB |
Output is correct |
26 |
Correct |
1095 ms |
795404 KB |
Output is correct |
27 |
Correct |
981 ms |
240712 KB |
Output is correct |
28 |
Correct |
934 ms |
134816 KB |
Output is correct |
29 |
Correct |
943 ms |
127520 KB |
Output is correct |
30 |
Correct |
948 ms |
179952 KB |
Output is correct |
31 |
Correct |
691 ms |
30144 KB |
Output is correct |
32 |
Correct |
954 ms |
256576 KB |
Output is correct |