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>
#define all(v) v.begin(), v.end()
using namespace std;
const int sz = 4e3 + 5;
int n, m, used[sz][sz];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
string s[sz];
bool ok(int i, int j)
{
return 0 <= i && i < n && 0 <= j && j < m && !used[i][j] && s[i][j] != '.';
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for(int i = 0; i < n; i++) cin >> s[i];
queue<array<int, 2>> q[2];
bool cur = 0;
int ans = 0;
q[cur].push({0, 0});
used[0][0] = 1;
while(!q[cur].empty())
{
ans++;
while(!q[cur].empty())
{
array<int, 2> xy = q[cur].front();
q[cur].pop();
for(int i = 0; i < 4; i++)
{
int nx = dx[i] + xy[0], ny = dy[i] + xy[1];
if(ok(nx, ny))
{
assert(0 <= nx && nx < n);
assert(0 <= ny && ny < m);
assert(used[nx][ny] == 0);
assert(s[nx][ny] != '.');
if(s[nx][ny] == s[xy[0]][xy[1]]) q[cur].push({nx, ny});
else q[cur ^ 1].push({nx, ny});
used[nx][ny] = 1;
}
}
}
cur ^= 1;
}
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |