#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define all(x) x.begin(),x.end()
#define pb push_back
#define po pop_back
#define pi(i, v) v[i]=v.back(); v.pop_back()
#define li long long
#define debug(x) cerr << #x << " is " << x << endl;
#define INF INT_MAX
// #define TEST_CASES
constexpr int mod = 1e9 + 7;
void IO () {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
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;
}
void init() {
// Write your predefined logic here
}
int main() {
fast;
IO();
init();
#ifdef TEST_CASES
int T;
cin >> T;
while(T--)
#endif
solve();
}
Compilation message
tracks.cpp: In function 'void IO()':
tracks.cpp:19:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
19 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
tracks.cpp:20:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
20 | freopen("output.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
216 ms |
272604 KB |
Execution killed with signal 11 |
2 |
Runtime error |
183 ms |
272584 KB |
Execution killed with signal 11 |
3 |
Runtime error |
183 ms |
272772 KB |
Execution killed with signal 11 |
4 |
Runtime error |
184 ms |
272592 KB |
Execution killed with signal 11 |
5 |
Runtime error |
181 ms |
272560 KB |
Execution killed with signal 11 |
6 |
Runtime error |
180 ms |
272660 KB |
Execution killed with signal 11 |
7 |
Runtime error |
185 ms |
272604 KB |
Execution killed with signal 11 |
8 |
Runtime error |
190 ms |
272660 KB |
Execution killed with signal 11 |
9 |
Runtime error |
198 ms |
272608 KB |
Execution killed with signal 11 |
10 |
Runtime error |
206 ms |
272668 KB |
Execution killed with signal 11 |
11 |
Runtime error |
195 ms |
272632 KB |
Execution killed with signal 11 |
12 |
Runtime error |
190 ms |
272620 KB |
Execution killed with signal 11 |
13 |
Runtime error |
199 ms |
272604 KB |
Execution killed with signal 11 |
14 |
Runtime error |
192 ms |
272756 KB |
Execution killed with signal 11 |
15 |
Runtime error |
196 ms |
272592 KB |
Execution killed with signal 11 |
16 |
Runtime error |
193 ms |
272632 KB |
Execution killed with signal 11 |
17 |
Runtime error |
200 ms |
272612 KB |
Execution killed with signal 11 |
18 |
Runtime error |
183 ms |
272572 KB |
Execution killed with signal 11 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
180 ms |
272664 KB |
Execution killed with signal 11 |
2 |
Runtime error |
196 ms |
272600 KB |
Execution killed with signal 11 |
3 |
Runtime error |
185 ms |
272676 KB |
Execution killed with signal 11 |
4 |
Runtime error |
183 ms |
272564 KB |
Execution killed with signal 11 |
5 |
Runtime error |
191 ms |
272588 KB |
Execution killed with signal 11 |
6 |
Runtime error |
189 ms |
272600 KB |
Execution killed with signal 11 |
7 |
Runtime error |
180 ms |
272580 KB |
Execution killed with signal 11 |
8 |
Runtime error |
197 ms |
272620 KB |
Execution killed with signal 11 |
9 |
Runtime error |
186 ms |
272608 KB |
Execution killed with signal 11 |
10 |
Runtime error |
188 ms |
272648 KB |
Execution killed with signal 11 |
11 |
Runtime error |
186 ms |
272548 KB |
Execution killed with signal 11 |
12 |
Runtime error |
206 ms |
272592 KB |
Execution killed with signal 11 |
13 |
Runtime error |
199 ms |
272556 KB |
Execution killed with signal 11 |
14 |
Runtime error |
186 ms |
272564 KB |
Execution killed with signal 11 |
15 |
Runtime error |
197 ms |
272624 KB |
Execution killed with signal 11 |
16 |
Runtime error |
225 ms |
272676 KB |
Execution killed with signal 11 |
17 |
Runtime error |
196 ms |
272556 KB |
Execution killed with signal 11 |
18 |
Runtime error |
193 ms |
272588 KB |
Execution killed with signal 11 |
19 |
Runtime error |
185 ms |
272668 KB |
Execution killed with signal 11 |
20 |
Runtime error |
209 ms |
272684 KB |
Execution killed with signal 11 |
21 |
Runtime error |
189 ms |
272676 KB |
Execution killed with signal 11 |
22 |
Runtime error |
185 ms |
272552 KB |
Execution killed with signal 11 |
23 |
Runtime error |
193 ms |
272620 KB |
Execution killed with signal 11 |
24 |
Runtime error |
195 ms |
272680 KB |
Execution killed with signal 11 |
25 |
Runtime error |
182 ms |
272676 KB |
Execution killed with signal 11 |
26 |
Runtime error |
212 ms |
272684 KB |
Execution killed with signal 11 |
27 |
Runtime error |
201 ms |
272588 KB |
Execution killed with signal 11 |
28 |
Runtime error |
212 ms |
272596 KB |
Execution killed with signal 11 |
29 |
Runtime error |
189 ms |
272684 KB |
Execution killed with signal 11 |
30 |
Runtime error |
184 ms |
272672 KB |
Execution killed with signal 11 |
31 |
Runtime error |
181 ms |
272640 KB |
Execution killed with signal 11 |
32 |
Runtime error |
190 ms |
272656 KB |
Execution killed with signal 11 |