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;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << '['; string sep = ""; for (const auto &x : v) os << sep << x, sep = ", "; return os << ']'; }
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename A> ostream& operator<<(ostream &os, const set<A> &s) { os << '{'; string sep = ""; for (const auto &x : s) os << sep << x, sep = ", "; return os << '}'; }
template<typename A, typename B> ostream& operator<<(ostream &os, const map<A, B> &m) { os << '{'; string sep = ""; for (const auto &x : m) os << sep << x.first << " -> " << x.second, sep = ", "; return os << '}'; }
#ifdef MY_DEBUG_FLAG
void debug() { cerr << '\n'; }
template<typename Ini, typename... Fim> void debug(Ini I, Fim... F) { cerr << I; if(sizeof...(F)) cerr << ", "; debug(F...); }
#define db(...) cerr << "DEBUG (" << #__VA_ARGS__ << ") == ", debug(__VA_ARGS__)
#else
#define db(...)
#endif
#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
#define fast ios_base::sync_with_stdio(false), cout.tie(nullptr), cin.tie(nullptr)
#define sz(a) ((int)(a).size())
#define rep(i,a,b) for(int i=(a); i<(b); i++)
#define dec(i,n,a) for(int i=(n); i>=(a); i--)
#define clr(a,v) memset(a, v, sizeof(a))
#define all(a) (a).begin(),(a).end()
constexpr int inf = 0x3f3f3f3f;
constexpr int maxn = 1e3 + 10;
constexpr int mod = 1000000007;
char grid[maxn][maxn];
bool mark[maxn][maxn];
int ans, r, c;
queue<pii> q;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
void dfs(int x, int y) {
mark[x][y] = 1;
rep(cnt,0,4) {
int i = x+dx[cnt], j = y+dy[cnt];
if(i >= r || i < 0 || j >= c || j < 0 || mark[i][j]) continue; // pos invalid
if(grid[i][j] == grid[x][y]) dfs(i, j);
else if(grid[i][j] != '*') {
// we know it's the opposite type
q.push({i, j});
}
}
}
int main() {
clr(grid, '*');
scanf("%d %d", &r, &c);
rep(i,0,r) scanf(" %s", grid[i]);
#ifdef MY_DEBUG_FLAG
rep(i,0,r) {
rep(j,0,c)
printf("%c", grid[i][j]);
printf("\n");
}
#endif
q.push({0,0});
while(q.size()) {
pii u = q.front(); q.pop();
if(mark[u.ff][u.ss]) continue;
db(u);
ans++;
dfs(u.ff, u.ss);
#ifdef MY_DEBUG_FLAG
rep(i,0,r) {
rep(j,0,c)
printf("%d", mark[i][j]);
printf("\n");
}
printf("\n");
#endif
}
#ifdef MY_DEBUG_FLAG
rep(i,0,r) {
rep(j,0,c)
printf("%d", mark[i][j]);
printf("\n");
}
#endif
printf("%d\n", ans);
}
Compilation message (stderr)
zoo.cpp: In function 'int main()':
zoo.cpp:62:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &r, &c);
~~~~~^~~~~~~~~~~~~~~~~
zoo.cpp:63:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
rep(i,0,r) scanf(" %s", grid[i]);
~~~~~^~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |