数据量比较小,dfs水过
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 55;
char g[MAXN][MAXN];
int book[MAXN][MAXN];
int n,m;
bool mark = false;
int dir[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};
void dfs(int x, int y, int cnt)
{
if(mark) return;
int tx,ty;
for(int i = 0; i < 4; ++i)
{
tx = x + dir[i][0];
ty = y + dir[i][1];
if(g[tx][ty] == g[x][y] && book[tx][ty] == 0)
{
book[tx][ty] = 1;
dfs(tx,ty,cnt+1);
book[tx][ty] = 0;
}
else if(g[tx][ty] == g[x][y] && book[tx][ty] == 2)
{
if((cnt+1) >= 4)
{
mark = true;
return;
}
}
}
}
int main()
{
scanf("%d %d",&n,&m);
for(int i = 0; i < n; ++i)
scanf(" %s",g[i]);
for(int i = 0; i < n; ++i)
{
for(int j = 0; j < m; ++j)
{
book[i][j] = 2;
dfs(i,j,1);
if(mark)
{
printf("Yes\n");
return 0;
}
book[i][j] = 0;
}
}
printf("No\n");
return 0;
}