#include<iostream>
#include<algorithm>
using namespace std;
int r = 0;
int c = 0;
int R1 = 0;//统计最多不同字母,建立全局变量,使递归时数据不会失效
//int R2;
char a[21][21] = { 0 };//输入的字母
int b[129] = { 0 };//比对,判断是否重复,利用ASCII表,判断是否重复
int x[] = { 1, 0, -1, 0 };//下 上
int y[] = { 0, 1, 0, -1 };// 右 左 ,两个结合一起看
void dfs(int x1,int y1,int R2)
{
if (R2 > R1)//2.取最多不同字母的路线
{
R1 = R2;
}
for (int i = 0; i < 4; i++)
{
int nx = x1 + x[i];
int ny = y1 + y[i];
if (!b[a[nx][ny]] && nx >= 1 && nx <= r && ny >= 1 && ny <= c)
{
b[a[nx][ny]] = 1;
dfs(nx, ny, R2 + 1);
b[a[nx][ny]] = 0;//回溯,方便之后的递归
}
}
}
int main()
{
cin >> r >> c;
for (int i = 1; i <= r; i++)
{
for (int j = 1; j <= c; j++)
{
cin >> a[i][j];
}
}
b[a[1][1]] = 1;//起始点,起始点的下标确定后,之后的代码,要根据确定了的起始点下标,相应的改变
dfs(1, 1, 1);//1.根
cout << R1 << endl;
return 0;
}
06-26
836
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-27
1767
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交