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 fi first
#define se second
#define mp make_pair
using namespace std;
const int N = 4e3 + 5;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
char a[N][N];
int n,m,ans;
bool vis[N][N];
void bfs(int i, int j)
{
queue <pair <int, int>> q;
q.push(mp(i, j));
vis[i][j] = true;
while (q.size())
{
pair <int, int> cur = q.front();
q.pop();
int x = cur.fi;
int y = cur.se;
for (int dir = 0; dir < 4; dir++)
{
int u = x + dx[dir];
int v = y + dy[dir];
if (a[u][v] == a[i][j] && min(u, v) > 0 && u <= n && v <= m && !vis[u][v])
{
vis[u][v] = true;
q.push(mp(u, v));
}
}
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("file.inp","r",stdin);
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
string s;
cin >> s;
for (int j = 1; j <= m; j++)
a[i][j] = s[j - 1];
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) if (!vis[i][j] && a[i][j] == a[n][m])
{
bfs(i, j);
ans++;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (a[i][j] != a[n][m])
return cout << ans + 1, 0;
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |