题目描述 给出一个R×C的大写字母矩阵,一开始的位置为左上角,你可以向上下左右四个方向移动,并且不能移向曾经经过的字母。问最多可以经过几个字母。
输入 第一行,输入字母矩阵行数R和列数C,1≤R,C≤20。
接着输出R行C列字母矩阵。
输出
最多能走过的不同字母的个数。
样例输入
3 6
HFDFFB
AJHGDH
DGAGEH
样例输出
6
#include<bits/stdc++.h>
using namespace std;
int r,c,ans,m,v[20][20],v1[30],tx[5]={0,0,1,0,-1},ty[5]={0,1,0,-1,0};
string s[25];
void dfs(int x,int y){
if(v1[s[x][y]-'A'+1]){
m=max(m,ans);
return;
}
for(int i=1;i<=4;i++){
int x1=x+tx[i],y1=y+ty[i];
if(!v[x1][y1] && x1>0 && x1<=r && y1>0 && y1<=c){
v[x1][y1]=1;
ans++;
v1[s[x][y]-'A'+1]=1;
dfs(x1,y1);//A B A C
v1[s[x][y]-'A'+1]=0;
v[x1][y1]=0;
ans--;
}
}
}
int main(){
cin>>r>>c;
for(int i=1;i<=r;i++){
for(int j=1;j<=c;j++) cin>>s[i][j];
}
v[1][1]=1;
dfs(1,1);
cout<<m;
return 0;
}