您的位置:首页 > 其它

ZOJ seeking 【深搜问题】及深搜的理解

2015-08-06 20:54 375 查看
Seeding

Time Limit: 2 Seconds
Memory Limit: 65536 KB

It is spring time and farmers have to plant seeds in the field. Tom has a nice field, which is a rectangle with n * m squares. There are big stones in some of the squares.
Tom has a seeding-machine. At the beginning, the machine lies in the top left corner of the field. After the machine finishes one square, Tom drives it into an adjacent square, and continues seeding. In order to protect the machine, Tom will not drive it
into a square that contains stones. It is not allowed to drive the machine into a square that been seeded before, either.

Tom wants to seed all the squares that do not contain stones. Is it possible?

Input

The first line of each test case contains two integers n and m that denote the size of the field. (1 < n, m < 7) The next n lines give the field, each of which contains m characters. 'S' is a square with stones, and '.' is a square without stones.

Input is terminated with two 0's. This case is not to be processed.

Output

For each test case, print "YES" if Tom can make it, or "NO" otherwise.

Sample Input

4 4

.S..

.S..

....

....

4 4

....

...S

....

...S

0 0



Sample Output


YES

NO

关于深搜的理解:关于深搜,已经纠结我好几天了,搞不懂一件事的感觉,真不好受。今天突然就有感觉了,废话不多说,说说我的理解。

首先,关于深搜,要从宏观的方向去看待,不要拘泥于一小步上,这样一会就回把我们绕晕。对于深搜,你就理解为,先从第一个点搜索,就是

进入dfs,不要管在进入dfs之后再进行递归的过程,这样会绕晕的,就理解为把四个方向搜索一遍之后执行该执行什么,把该要实现的功能写上,

然后在进入main函数里面的循环寻找第二个点,就这样每一次下去,把每个点搜索一遍,把该要实现的功能现在就OK了。 PS(这是我对搜索的

理解,不知道对不对,如果不对的话,希望个大神指点指点)。

题意:m和n代表几行几列的田地,S代表石头,一个拖拉机从第一个点开始耕地,耕地时不能碰到石头,也不能返回,问是否能把这块地耕完。

AC代码

#include<stdio.h>
#include<string.h>
char str[10][10];
int vis,m,n,flag;
void dfs(int x,int y)
{
if(x<=0||y<0||x>m||y>=n)
return ;
if(str[x][y]=='S')
return ;
vis++;
str[x][y]='S';
if(vis==n*m)
{
flag=1;
return ;
}
//vis++;
//str[x][y]='S';
dfs(x+1,y);
dfs(x-1,y);
dfs(x,y+1);
dfs(x,y-1);
vis--;
str[x][y]='.';
}
int main()
{
int i,j;
while(scanf("%d %d",&m,&n),m|n)
{
vis=0;
for(i=1;i<=m;i++)
scanf("%s",str[i]);
for(i=1;i<=m;i++)
for(j=0;j<n;j++)
{
if(str[i][j]=='S')
vis++;
}
flag=0;
dfs(1,0);
if(flag)
printf("YES\n");
else
printf("NO\n");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: