#include<bits/stdc++.h>
using namespace std;
int iterate(int i, int j, char temp,
vector<vector<bool>> &visited,
vector<vector<char>> &arr) {
if(i < 0 || j < 0 || i >= (int)visited.size() || j >= (int)visited[0].size())
return -1;
if(visited[i][j]) return -1;
visited[i][j] = true;
if(arr[i][j] == '.') return -1;
return arr[i][j] == temp;
}
void solve() {
int r, c;
cin >> r >> c;
vector<vector<char>> arr(r, vector<char>(c));
for(auto &x: arr) {
for(auto &y: x) cin >> y;
}
deque<pair<int, int>> d;
d.push_back({0, 0});
int answer = 1;
char temp = arr[0][0];
char prev = arr[0][0];
vector<vector<bool>> visited(r, vector<bool>(c, false));
visited[0][0] = true;
while(!d.empty()) {
pair<int, int> curr = d.front();
d.pop_front();
prev = temp;
temp = arr[curr.first][curr.second];
if(temp != prev) {
answer++;
}
int a;
a = iterate(curr.first + 1, curr.second, temp, visited, arr);
if(a == 1) d.push_front({curr.first + 1, curr.second});
else if(a == 0) d.push_back({curr.first + 1, curr.second});
a = iterate(curr.first - 1, curr.second, temp, visited, arr);
if(a == 1) d.push_front({curr.first - 1, curr.second});
else if(a == 0) d.push_back({curr.first - 1, curr.second});
a = iterate(curr.first, curr.second - 1, temp, visited, arr);
if(a == 1) d.push_front({curr.first, curr.second - 1});
else if(a == 0) d.push_back({curr.first, curr.second - 1});
a = iterate(curr.first, curr.second + 1, temp, visited, arr);
if(a == 1) d.push_front({curr.first, curr.second + 1});
else if(a == 0) d.push_back({curr.first, curr.second + 1});
}
cout << answer << endl;
}
int main() {
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
952 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
14 ms |
724 KB |
Output is correct |
5 |
Correct |
6 ms |
592 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
5 ms |
456 KB |
Output is correct |
11 |
Correct |
4 ms |
380 KB |
Output is correct |
12 |
Correct |
9 ms |
436 KB |
Output is correct |
13 |
Correct |
7 ms |
468 KB |
Output is correct |
14 |
Correct |
6 ms |
440 KB |
Output is correct |
15 |
Correct |
25 ms |
800 KB |
Output is correct |
16 |
Correct |
25 ms |
852 KB |
Output is correct |
17 |
Correct |
20 ms |
852 KB |
Output is correct |
18 |
Correct |
15 ms |
740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
724 KB |
Output is correct |
2 |
Correct |
115 ms |
3660 KB |
Output is correct |
3 |
Correct |
1002 ms |
33908 KB |
Output is correct |
4 |
Correct |
254 ms |
8344 KB |
Output is correct |
5 |
Correct |
616 ms |
19128 KB |
Output is correct |
6 |
Correct |
1390 ms |
47564 KB |
Output is correct |
7 |
Correct |
4 ms |
852 KB |
Output is correct |
8 |
Correct |
5 ms |
724 KB |
Output is correct |
9 |
Correct |
6 ms |
340 KB |
Output is correct |
10 |
Correct |
3 ms |
340 KB |
Output is correct |
11 |
Correct |
5 ms |
852 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
115 ms |
3640 KB |
Output is correct |
14 |
Correct |
67 ms |
2252 KB |
Output is correct |
15 |
Correct |
64 ms |
2428 KB |
Output is correct |
16 |
Correct |
53 ms |
1560 KB |
Output is correct |
17 |
Correct |
301 ms |
8908 KB |
Output is correct |
18 |
Correct |
257 ms |
8780 KB |
Output is correct |
19 |
Correct |
242 ms |
8216 KB |
Output is correct |
20 |
Correct |
218 ms |
7524 KB |
Output is correct |
21 |
Correct |
600 ms |
20012 KB |
Output is correct |
22 |
Correct |
627 ms |
19280 KB |
Output is correct |
23 |
Correct |
577 ms |
16656 KB |
Output is correct |
24 |
Correct |
621 ms |
19456 KB |
Output is correct |
25 |
Correct |
1063 ms |
33900 KB |
Output is correct |
26 |
Correct |
1008 ms |
76256 KB |
Output is correct |
27 |
Correct |
1316 ms |
55592 KB |
Output is correct |
28 |
Correct |
1350 ms |
47444 KB |
Output is correct |
29 |
Correct |
1364 ms |
45584 KB |
Output is correct |
30 |
Correct |
1324 ms |
49640 KB |
Output is correct |
31 |
Correct |
937 ms |
22300 KB |
Output is correct |
32 |
Correct |
1243 ms |
46784 KB |
Output is correct |