-
个人简介
#include<bits/stdc++.h> using namespace std; struct node{ int x,y; }a[3000],b[3000]; int cnt1,cnt2,vis[60][60],n,m,ans=INT_MAX; bool flag=false; int fx[4]={0,0,-1,1}; int fy[4]={1,-1,0,0}; int dis(node x,node y) { return abs(y.x-x.x)+abs(y.y-x.y)-1; } bool pd(int x,int y) { if(x>=1&&x<=n&&y>=1&&y<=m) return true; else return false; } void dfs1(int x,int y) { vis[x][y]=0; cnt1++; a[cnt1].x=x; a[cnt1].y=y; for(int i=0;i<4;i++) { int X=x+fx[i],Y=y+fy[i]; if(vis[X][Y]&&pd(X,Y)) dfs1(X,Y); } } void dfs2(int x,int y) { vis[x][y]=0; b[++cnt2]={x,y}; for(int i=0;i<4;i++) { int X=x+fx[i],Y=y+fy[i]; if(vis[X][Y]&&pd(X,Y)) dfs2(X,Y); } } int main() { freopen("color.in","r",stdin); freopen("color.out","w",stdout); cin>>n>>m; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { char ch; cin>>ch; if(ch=='X') vis[i][j]=1; } for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++) if(vis[i][j]) { dfs1(i,j); flag=true; break; } if(flag) break; } flag=false; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++) if(vis[i][j]) { dfs2(i,j); flag=true; break; } if(flag) break; } for(int i=1;i<=cnt1;i++) for(int j=1;j<=cnt2;j++) ans=min(ans,dis(a[i],b[j])); cout<<ans; }
-
通过的题目
-
最近活动
This person is lazy and didn't join any contests or homework. -
最近编写的题解
This person is lazy and didn't write any solutions.