#include<iostream> #include<stdio.h> #define MAXN 20 using namespace std; int DFS(int i,int j,int len,int r,int c); bool key[26]; char a[MAXN][MAXN]; //bool bool_lean[MAXN][MAXN]; int main() { //freopen("acm.acm","r",stdin); int i; int j; int r; int c; cin>>r>>c; // memset(bool_lean,false,sizeof(bool_lean)); for(i = 0; i < r; ++ i) { for(j = 0; j < c; ++ j) { cin>>a[i][j]; } } key[a[0][0]-'A'] = true; cout<<DFS(0,0,1,r,c)<<endl; } int DFS(int i,int j,int len,int r,int c) { int tem; tem = len; int max = 0; if(i > 0) { if(!key[a[i-1][j] - 'A']) { key[a[i-1][j] - 'A'] = true; // bool_lean[i-1][j] = true; len = DFS(i-1,j,len+1,r,c); key[a[i-1][j] - 'A'] = false; // bool_lean[i-1][j] = false; if(len > max) max = len; len = tem; } } if(i < r-1) { if(!key[a[i+1][j] - 'A']) { key[a[i+1][j] - 'A'] = true; // bool_lean[i+1][j] = true; len = DFS(i+1,j,len+1,r,c); key[a[i+1][j] - 'A'] = false; // bool_lean[i+1][j] = false; if(len > max) max = len; len = tem; } } if(j > 0) { if(!key[a[i][j-1] - 'A']) { key[a[i][j-1] - 'A'] = true; // bool_lean[i][j-1] = true; len = DFS(i,j-1,len+1,r,c); key[a[i][j-1] - 'A'] = false; // bool_lean[i][j-1] = false; if(len>max) max = len; len = tem; } } if(j < c-1) { if(!key[a[i][j+1] - 'A']) { key[a[i][j+1] - 'A'] = true; // bool_lean[i][j+1] = true; len = DFS(i,j+1,len+1,r,c); key[a[i][j+1] - 'A'] = false; // bool_lean[i][j+1] = false; if(len>max) max = len; len = tem; } } if(len > max) max = len; return max; }
关注我的公众号,当然,如果你对Java, Scala, Python等技术经验,以及编程日记,感兴趣的话。
技术网站地址: vmfor.com