深搜
简单题

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
using namespace std;
#define maxn 25
int n, m;
char map[maxn][maxn];
bool vis[30];
int ans;
int dir[4][2] =
{
{ 0, 1 },
{ 1, 0 },
{ -1, 0 },
{ 0, -1 } };
bool ok(int x, int y)
{
if (x < 0 || y < 0 || x >= n || y >= m)
return false;
return !vis[map[x][y] - 'A'];
}
void dfs(int x, int y, int step)
{
ans = max(ans, step);
vis[map[x][y] - 'A'] = true;
for (int i = 0; i < 4; i++)
if (ok(x + dir[i][0], y + dir[i][1]))
{
dfs(x + dir[i][0], y + dir[i][1], step + 1);
}
vis[map[x][y] - 'A'] = false;
}
int main()
{
//freopen("t.txt", "r", stdin);
memset(vis, 0, sizeof(vis));
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++)
scanf("%s", map[i]);
ans = 1;
dfs(0, 0, 1);
printf("%d\n", ans);
return 0;
}