ZOJ Seeding 2100【简单深搜】

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


Author: ZHANG, Zheng

Source: Zhejiang University Local Contest 2004

简单深搜

#include <stdio.h>
#include <string.h>
#include <algorithm>

using namespace std;

bool used[10][10];
char Map[10][10];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,-1,1};
int N,M;
int num;
int tot;
int flag;

void DFS(int x,int y)
{
	used[x][y]=true;
	if(tot==num) {
		flag=1;
		//printf("YES\n");
		return;// true;
	}
	for(int i=0;i<4;i++){
		int nx=x+dx[i];
		int ny=y+dy[i];
		if(Map[nx][ny]=='.'&&0<=nx&&nx<N&&0<=ny&&ny<M&&!used[nx][ny]){
			//used[nx][ny]=true;
			tot++;
			//printf("-->%d %d\n",nx,ny);
			DFS(nx,ny);
			//printf("<--%d %d\n",nx,ny);
			tot--;
			used[nx][ny]=false;
			//return;
		}
	}
	//printf("NO\n");
	return;// false;
}

int main()
{
	while(scanf("%d%d",&N,&M),N!=0||M!=0){
		for(int i=0;i<N;i++){
			scanf("%s",Map[i]);
		}
		num=0;
		tot=1;
		flag=0;
		for(int i=0;i<N;i++){
			for(int j=0;j<M;j++){
				if(Map[i][j]=='.')
				num++;
			}
		}

		memset(used,false,sizeof(used));
		DFS(0,0);
		//printf("%d %d\n",num,tot);
		if(!flag)printf("NO\n");
		else printf("YES\n");
	}
	return 0;
}

版权声明:本文为博主原创文章,转载请注明出处。

时间: 2024-08-25 14:16:00

ZOJ Seeding 2100【简单深搜】的相关文章

hdu1016(简单深搜)

这是一个简单深搜问题,要求相邻的数之间相加为素数.采用深搜,把满足条件的值放在parent[]中.最后输出parent[]. A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime. Note: th

fzu 1920 Left Mouse Button(简单深搜题)

题目地址:http://acm.fzu.edu.cn/problem.php?pid=1920 题目大意是给定一个n*n的图,模拟扫雷游戏,0代表没有雷区,1代表附近九宫格内只有一个雷-- 如果不懂的话去玩下扫雷,挺好玩的,99个雷的玩了好几百盘才赢了一次............ 此题假设神操作,点不到雷,问你最少要多少下才可以把图中非雷的点完,怎么样才最快呢? 当然先点0啦,,,,,,,, 好吧,不废话了..... ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1

简单深搜:POJ1546——Sum it up

结束了三分搜索的旅程 我开始迈入深搜的大坑.. 首先是一道比较基础的深搜题目(还是很难理解好么) POJ 1564 SUM IT UP 大体上的思路无非是通过深搜来进行穷举.匹配 为了能更好地理解深搜 可以尝试去画一下二叉树理解一下,查看遍历的路径 代码还是百度到别人的自己再参悟- -佩服别人的功底啊 先上代码: /*POJ 1546 Sum it up*/ # include<iostream> # include<algorithm> # include<cstdio&g

hdu 1241 Oil Deposits(八方向简单深搜,对新手挺经典)

Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 12461 Accepted Submission(s): 7245 Problem Description The GeoSurvComp geologic survey company is responsible for detecting underground

简单深搜(poj 3009)

题目链接:http://poj.org/problem?id=3009 题目:冰壶撞向目的地,只有遇到"1"才能停下来,并且把"1"撞成"0".只能横冲直撞,不允许蛇皮走位等等骚操作.从"2"要撞到"3",周围有"0",才能向有"0"的地方滑.运动员只能推十次,问最少要多少次才到"3"? 用深搜遍历每一个方向. 1 #include<stdi

poj1321棋盘简单深搜

这个题确实有点简单,看清楚不能同列同行就好了,用一个一维数组标记哪一列用了往下搜就好了 #include<stdio.h> int flag[8]={1,1,1,1,1,1,1,1},i,j,n,k,num=0; char arr[10][11]; void dfs(int a,int b) { int ii,jj; if(b==0) { num++; return ; } for(ii=a+1;ii<n;ii++) for(jj=0;jj<n;jj++) { if(flag[jj

HDOJ1015(简单深搜)

简单的深度优先搜索.求最大字典序,注意要先排序. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int SIZE=13; char a[5]; int vis[SIZE*2+10]; bool cmp(char a,char b) { return b > a; } int npow(int x,i

nyoj587 hdu1045 简单深搜

#include<iostream> #include<cstdio> #include<queue> #include<vector> #include<map> #include<list> #include<set> #include<stack> #include<cstring> #include<string> #include<algorithm> #inclu

简单深搜

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=27 #include <stdio.h> #include <string.h> int t; int r,c; int maps[105][105]; int color[105][105]; int to[4][2]= {{0,1},{0,-1},{1,0},{-1,0}}; void dfs(int i,int j) { if(maps[i][j]==1&&a