深度优先搜索练习之神奇的矩环
Time Limit: 1000MSMemory Limit: 65536KB
ProblemDescription
小鑫的女朋友被魔王抢走了!
魔王留给小鑫一张n*m大的表,上面有各种各样的颜色,用A-Z这26个字母来表示。魔王留给他一个任务,如果小鑫可以在这张表中找出任意一个长度大于1的环,并且这个环的颜色是相同的,魔王就把小鑫的女朋友还给他。为了从魔王手中夺回他的女朋友,小鑫请你帮忙,你能帮帮他吗?
Input
多组输入。
每组的第一行有两个整数n,m。代表表的大小。
接下来是由A-Z的一些字母所构成的n行m列的表。
1<=n,m<=200
Output
如果可以救回他的女朋友,输出Yes,否则输出No
ExampleInput
4 7
ABCBBAA
BCBCBCB
AABBCCA
ACCCBBB
10 3
AAC
ABB
BBA
AAC
CBC
CCA
CBB
CCA
CCB
BAA
ExampleOutput
No
Yes
Hint
Author
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
char tu[202][202];
int v[202][202],bug;
void dfs(int x,int y,int x1,int y1,int n,int m)//x1,y1记录上一次走过的位置
{
v[x][y] = 1;//走过的就不再走了
if(bug==1)
{
return ;
}
if(y-1>=0&&tu[x][y]==tu[x][y-1])
{
if(v[x][y-1]==1&&(x!=x1||y-1!=y1))
{
bug = 1;
}
else if(v[x][y-1]==0)
{
dfs(x,y-1,x,y,n,m);
}
}
if(y+1<=m&&tu[x][y]==tu[x][y+1])
{
if(v[x][y+1]==1&&(x!=x1||y+1!=y1))
{
bug = 1;
}
else if(v[x][y+1]==0)
{
dfs(x,y+1,x,y,n,m);
}
}
if(x-1>=0&&tu[x][y]==tu[x-1][y])
{
if(v[x-1][y]==1&&(x-1!=x1||y!=y1))
{
bug = 1;
}
else if(v[x-1][y]==0)
{
dfs(x-1,y,x,y,n,m);
}
}
if(x+1<=n&&tu[x][y]==tu[x+1][y])
{
if(v[x+1][y]==1&&(x+1!=x1||y1!=y))
{
bug = 1;
}
else if(v[x+1][y]==0)
dfs(x+1,y,x,y,n,m);
}
}
int main()
{
int n,m,i,j;
while(~scanf("%d%d",&n,&m))
{
memset(v,0,sizeof(v));
bug=0;
for(i=0;i<n;i++)
{
scanf("%s",tu[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(v[i][j]==0)
{
dfs(i,j,i,j,n-1,m-1);
}
if(bug==1)
{
break; //剪枝
}
}
if(bug==1)
{
break; //剪枝
}
}
if(bug==1)
{
printf("Yes\n");
}
else
{
printf("No\n");
}
}
return 0;
}
/***************************************************
User name: jk160505徐红博
Result: Accepted
Take time: 4ms
Take Memory: 336KB
Submit time: 2017-02-16 11:13:58
****************************************************/
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
char tu[202][202];
int v[202][202],bug;
void dfs(int x,int y,int x1,int y1,int n,int m)//x1,y1记录上一次走过的位置
{
v[x][y] = 1;//走过的就不再走了
if(bug==1)
{
return ;
}
if(y-1>=0&&tu[x][y]==tu[x][y-1])
{
if(v[x][y-1]==1&&(x!=x1||y-1!=y1))
{
bug = 1;
}
else if(v[x][y-1]==0)
{
dfs(x,y-1,x,y,n,m);
}
}
if(y+1<=m&&tu[x][y]==tu[x][y+1])
{
if(v[x][y+1]==1&&(x!=x1||y+1!=y1))
{
bug = 1;
}
else if(v[x][y+1]==0)
{
dfs(x,y+1,x,y,n,m);
}
}
if(x-1>=0&&tu[x][y]==tu[x-1][y])
{
if(v[x-1][y]==1&&(x-1!=x1||y!=y1))
{
bug = 1;
}
else if(v[x-1][y]==0)
{
dfs(x-1,y,x,y,n,m);
}
}
if(x+1<=n&&tu[x][y]==tu[x+1][y])
{
if(v[x+1][y]==1&&(x+1!=x1||y1!=y))
{
bug = 1;
}
else if(v[x+1][y]==0)
dfs(x+1,y,x,y,n,m);
}
}
int main()
{
int n,m,i,j;
while(~scanf("%d%d",&n,&m))
{
memset(v,0,sizeof(v));
bug=0;
for(i=0;i<n;i++)
{
scanf("%s",tu[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(v[i][j]==0)
{
dfs(i,j,i,j,n-1,m-1);
}
if(bug==1)
{
break; //剪枝
}
}
if(bug==1)
{
break; //剪枝
}
}
if(bug==1)
{
printf("Yes\n");
}
else
{
printf("No\n");
}
}
return 0;
}
/***************************************************
User name: jk160505徐红博
Result: Accepted
Take time: 4ms
Take Memory: 336KB
Submit time: 2017-02-16 11:13:58
****************************************************/