#include <iostream> #include <fstream> #include <string> #include <bitset> #include <deque> #define M 405 using namespace std; fstream fin("ferma.in",ios::in),fout("ferma.out",ios::out); int di[]={-1,0,1,0},dj[]={0,1,0,-1}; deque <pair<int,int> > d; int cost[M*M],who[M][M]; bitset <M> bit[M]; string s[M]; int n,m,t,ir,jr; char color; void citire(){ fin>>t>>n>>m; for(int i=0;i<n;i++) fin>>s[i]; } bool ok(int i,int j){ if(-1<i&&i<n&&-1<j&&j<m) return 1; return 0; } int bfs(int i,int j,int nr){ int cate=1; bit[i][j]=1; d.clear(); d.push_back(make_pair(i,j)); while(!d.empty()){ i=d.front().first; j=d.front().second; who[i][j]=nr; for(int k=0;k<4;k++){ if(ok(i+di[k],j+dj[k])==1&&s[i+di[k]][j+dj[k]]==s[i][j]&&bit[i+di[k]][j+dj[k]]==0){ bit[i+di[k]][j+dj[k]]=1; cate++; d.push_back(make_pair(i+di[k],j+dj[k])); } } d.pop_front(); } return cate; } int solv(){ int actual,maxim=-999,r,nr=0; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(bit[i][j]==0) { nr++; actual=bfs(i,j,nr); cost[nr]=actual; maxim=max(maxim,actual); } } } return maxim; } void cauta(int a,int b,int&maxim) { int i,j,c,d,e,f; for(i=0;i<4;i++) { for(j=0;j<4;j++) { c=a+di[i];d=b+dj[i]; e=a+di[j];f=b+dj[j]; if(ok(c,d)==1&&ok(e,f)==1&&s[c][d]==s[e][f]&&s[c][d]!=s[a][b]) { if(who[c][d]==who[e][f]) { if(cost[who[c][d]]+1>=maxim) { color=s[c][d]; ir=a; jr=b; maxim=cost[who[c][d]]+1; } } else { if(cost[who[c][d]]+cost[who[e][f]]+1>=maxim) { color=s[c][d]; ir=a; jr=b; maxim=cost[who[c][d]]+cost[who[e][f]]+1; } } } } } } int search() { int maxim=-9999,r; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { cauta(i,j,maxim); } } //cauta(230,42,maxim); //cauta(243,1,maxim); cout<<ir+1<<" "<<jr+1<<"\n"<<color<<"\n"<<maxim; fout<<ir+1<<" "<<jr+1<<"\n"<<color<<"\n"; } int main(){ int a; citire(); a=solv(); if(t==1) fout<<a<<"\n"; else search(); return 0; } /*#include <iostream> #include <fstream> #include <bitset> #include <queue> #define M 405 using namespace std; fstream fin("ferma.in",ios::in),fout("ferma.out",ios::out); int di[]={-1,0,1,0},dj[]={0,1,0,-1},n,m,t,who[M][M],cost[M*M],aux[M*M]; queue <pair<int,int> > v; char color,s[M][M]; void citire(){ fin>>t>>n>>m; for(int i=1;i<=n;i++) fin>>s[i]+1; } int bfs(int i,int j,int ind) { int total=1,k; who[i][j]=ind; v.push(make_pair(i,j)); while(!v.empty()) { i=v.front().first; j=v.front().second; for(k=0;k<4;k++) { if(s[i+di[k]][j+dj[k]]==s[i][j]&&0<i+di[k]&&i+di[k]<=n&&0<j+dj[k]&&j+dj[k]<=m) { if(who[i+di[k]][j+dj[k]]==0) { who[i+di[k]][j+dj[k]]=ind; total++; v.push(make_pair(i+di[k],j+dj[k])); } } } v.pop(); } return total; } void cmp(int x1,int y1,int x2,int y2) { if(who[x1][y1]!=who[x2][y2]&& 0<x1&&x1<=n&& 0<x2&&x2<=n&& 0<y1&&y1<=m&& 0<y2&&y2<=m) { if(s[x1][y1]==s[x2][y2]) { aux[who[x1][y1]]=max(aux[who[x1][y1]],cost[who[x1][y1]]+cost[who[x2][y2]]); } } } int main() { int a,i,j,ind=1,maxim1=-99999,maxim2=-99999,xi,xj,k,r; citire(); for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { if(who[i][j]==0) { cost[ind]=bfs(i,j,ind); maxim1=max(maxim1,cost[ind]); ind++; } } } if(t==2) { for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { //prima //aux[who[i-1][j]]=-M; cmp(i-1,j,i,j+1); cmp(i-1,j,i+1,j); cmp(i-1,j,i,j-1); //a doua //aux[who[i][j+1]]=-M; cmp(i,j+1,i+1,j); cmp(i,j+1,i,j-1); //a treia //aux[who[i+1][j]]=-M; cmp(i+1,j,i,j-1); for(k=0;k<4;k++) { if(who[i][j]!=who[i+di[k]][j+dj[k]]&&s[i][j]!=s[i+di[k]][j+dj[k]]) { if(maxim2<aux[who[i+di[k]][j+dj[k]]]) { maxim2=aux[who[i+di[k]][j+dj[k]]]; xi=i; xj=j; color=s[i+di[k]][j+dj[k]]; } } } } } fout<<xi<<" "<<xj<<"\n"<<color<<"\n"; return 0; } fout<<maxim1<<"\n"; return 0; }*/