This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |