//用一个visit判重 #include <stdio.h> #include <string.h> int r,s; char a[25][25]; int max; int visit[30]; void DFS(int x,int y,int step) { int c[]={-1,1,0,0}; int b[]={0,0,1,-1}; int i,j; if (step>max) max=step; for (i=0;i<4;i++) if ((x+c[i]>=0)&&(x+c[i]<r)&&(y+b[i]>=0)&&(y+b[i]<s)&&visit[a[x+c[i]][y+b[i]]-'A']==0) { visit[a[x+c[i]][y+b[i]]-'A']=1; DFS(x+c[i],y+b[i],step+1); visit[a[x+c[i]][y+b[i]]-'A']=0; } } int main() { int i,j; max=0; memset(visit,0,sizeof(visit)); scanf("%d%d",&r,&s); for (i=0;i<r;i++) scanf("%s",a[i]); visit[a[0][0]-'A']=1; DFS(0,0,1); printf("%d\n",max); return 0; }