AcWing 1111. 字母
原题链接
简单
作者:
此间
,
2021-04-17 18:37:30
,
所有人可见
,
阅读 348
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int >PII;
const int N = 100;
int w[30][30];
int h[30] = {0};
int n, m;
int dx[4] = { -1, 0, 1, 0 }, dy[4] = { 0, 1, 0, -1 };
int ans = 0;
void dfs(int x, int y ,int step)
{
h[w[x][y]] = true;
for (int i = 0; i < 4; i++)
{
int a = x + dx[i], b = y + dy[i]; //判断下个点在范围内且没有走过
if (a < 0 || a >= n || b < 0 || b >= m)continue;
if (h[w[a][b]])continue;
h[w[a][b]] = true;
dfs(a, b, step + 1);
h[w[a][b]] = false;
}
ans = max(ans, step);
}
int main() {
//ios::sync_with_stdio;
cin >> n >> m;
char a;
for(int i=0;i<n;i++)
for (int j = 0; j < m; j++)
{
cin >> a;
w[i][j] = a-65;
}
dfs(0, 0, 1);
cout << ans << endl;
return 0;
}