This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//0-1 bfs-sel szebb lett volna...
#include<bits/stdc++.h>
using namespace std;
int n,m;
string t[1001];
int ans;
int d[4][2]={{-1,0},{1,0},{0,1},{0,-1}};
int volt[1001][1001];
vector<pair<int,int>> lst;
void dfs(int x, int y, int cnt) {
	assert(volt[x][y]==0);
	volt[x][y]=cnt;
	ans=max(ans, cnt);
	for(int i=0;i<4;++i) {
		int nx=x+d[i][0], ny=y+d[i][1];
		if(nx>=0 && ny>=0 && nx<n && ny<m && !volt[nx][ny]) {
			if(t[nx][ny]==t[x][y]) dfs(nx,ny,cnt);
		}
	}
	for(int i=0;i<4;++i) {
		int nx=x+d[i][0], ny=y+d[i][1];
		if(nx>=0 && ny>=0 && nx<n && ny<m && !volt[nx][ny]) {
			if(t[nx][ny]!=t[x][y] && t[nx][ny]!='*') lst.push_back({nx,ny});
		}
	}
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	for(int i=0;i<n;++i) cin>>t[i];
	lst={{n-1,m-1}};
	do {
		vector<pair<int,int>> kell=lst;
		lst.clear();
		ans++;
		for(auto i:kell) {
			if(!volt[i.first][i.second]) dfs(i.first,i.second,ans);
		}
	}while(!lst.empty());
	cout<<ans<<"\n";
	return 0;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |