您的位置:首页 > 其它

51nod 1416 两点

2017-07-30 00:24 316 查看
数据量比较小,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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: