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 <random>
#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <chrono>
#define fr first
#define sc second
#define ad push_back
using namespace std;
using ll = long long;
mt19937 rnd(348502);
const ll N = 1005;
int a[N][N];
int obshi[N][N];
int color[N][N];
void solve()
{
int n, i, j, m, x, y, qan = 0;
cin >> n >> m;
for ( i = 0; i < n; i++)
{
for ( j = 0; j < m; j++)
{
char c;
cin >> c;
if (c == 'T')
{
a[i][j] = 1;
qan++;
}
else if (c == 'B')
{
a[i][j] = 2;
qan++;
}
else
{
color[i][j] = 1;
}
}
}
int hanqan = 0, ans = 0;
queue<pair<int, int>> q, hajord;
hajord.push({ 0,0 });
color[0][0] = 1;
while (!hajord.empty())
{
ans++;
q = hajord;
while (!hajord.empty())
{
hajord.pop();
}
while (!q.empty())
{
x = q.front().first;
y = q.front().second;
q.pop();
if (x - 1 >= 0 && color[x - 1][y] == 0)
{
if (a[x - 1][y] == a[x][y])
q.push({ x - 1, y });
else
hajord.push({ x - 1, y });
color[x - 1][y] = 1;
}
if (x + 1 < n && color[x + 1][y] == 0)
{
if (a[x + 1][y] == a[x][y])
q.push({ x + 1, y });
else
hajord.push({ x + 1, y });
color[x + 1][y] = 1;
}
if (y - 1 >= 0 && color[x][y - 1] == 0)
{
if (a[x][y - 1] == a[x][y])
q.push({ x, y - 1 });
else
hajord.push({ x, y - 1});
color[x][y - 1] = 1;
}
if (y + 1 < m && color[x][y + 1] == 0)
{
if (a[x][y + 1] == a[x][y])
q.push({ x, y + 1 });
else
hajord.push({ x, y + 1 });
color[x][y + 1] = 1;
}
}
}
cout << ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
ll tt = 1;
//cin >> tt;
while (tt--) {
solve();
}
return 0;
}
Compilation message (stderr)
zoo.cpp: In function 'void solve()':
zoo.cpp:60:9: warning: unused variable 'hanqan' [-Wunused-variable]
60 | int hanqan = 0, ans = 0;
| ^~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |