1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 5 const int N=30; 6 int r,c,ans,a[N][N],cap[N]; 7 void solve(int,int,int); 8 bool check(int x,int y,int d){ 9 if(x>0&&x<=r&&y>0&&y<=c&&!cap[a[x][y]]){ 10 solve(x,y,d+1); 11 return 1; 12 }else return 0; 13 } 14 void solve(int x,int y,int d){ 15 cap[a[x][y]]=1; 16 bool f=1; 17 if(check(x-1,y,d))f=0; 18 if(check(x+1,y,d))f=0; 19 if(check(x,y-1,d))f=0; 20 if(check(x,y+1,d))f=0; 21 if(f)ans=max(ans,d); 22 cap[a[x][y]]=0; 23 } 24 int main(){ 25 cin>>r>>c; 26 for(int i=1;i<=r;i++) 27 for(int j=1;j<=c;j++){ 28 char c; 29 cin>>c; 30 a[i][j]=c-‘A‘; 31 } 32 solve(1,1,1); 33 cout<<ans; 34 return 0; 35 }
原文:https://www.cnblogs.com/sxrekord/p/letters.html