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>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define all(v) v.begin(), v.end()
#define pb push_back
#define ss second
#define ff first
#define vt vector
using namespace std;
//using namespace __gnu_pbds;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, int> pdi;
const int inf = 1e9 + 10000;
const int mod = 1e9+7;
const int maxn = 1e4 + 12;
char a[maxn][maxn];
bool was[maxn][maxn];
queue<pair<pii, int>> q;
void go(int x, int y, int s){
q.push({{x, y}, s});
was[x][y] = 1;
if(!was[x - 1][y] && a[x][y] == a[x - 1][y])go(x - 1, y, s);
if(!was[x + 1][y] && a[x][y] == a[x + 1][y])go(x + 1, y, s);
if(!was[x][y - 1] && a[x][y] == a[x][y - 1])go(x, y - 1, s);
if(!was[x][y + 1] && a[x][y] == a[x][y + 1])go(x, y + 1, s);
}
void solve() {
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; i++){
was[i][0] = was[i][m + 1] = 1;
for(int j = 1; j <= m; j++){
cin >> a[i][j];
if(a[i][j] == '*')was[i][j] = 1;
}
}
for(int i = 1; i <= m; i++)was[0][i] = was[n + 1][i] = 1;
int ans = 0;
go(n, m, 1);
while(!q.empty()){
pair<pii, int> nw = q.front();
q.pop();
int x = nw.ff.ff, y = nw.ff.ss, s = nw.ss;
ans = max(ans, s);
if(!was[x - 1][y] && a[x][y] != a[x - 1][y])go(x - 1, y, s + 1);
if(!was[x + 1][y] && a[x][y] != a[x + 1][y])go(x + 1, y, s + 1);
if(!was[x][y - 1] && a[x][y] != a[x][y - 1])go(x, y - 1, s + 1);
if(!was[x][y + 1] && a[x][y] != a[x][y + 1])go(x, y + 1, s + 1);
}
cout << ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int times = 1;
//cin >> times;
for(int i = 1; i <= times; i++) {
solve();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |