ZOJ 题目2100 Seeding(DFS)

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

水水~~

ac代码

#include<stdio.h>
#include<string.h>
int n,m,flag,ans;
char map[10][10];
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
int jud(int x,int y)
{
	if(x<0||x>=n)
		return 0;
	if(y<0||y>=m)
		return 0;
	if(map[x][y]=='S')
		return 0;
	return 1;
}
void dfs(int x,int y)
{
	int i;
	//vis[x][y]=1;
	if(ans==0)
	{
		flag=1;
		return;
	}
	if(flag)
		return;
	for(i=0;i<4;i++)
	{
		int tx=x+dx[i];
		int ty=y+dy[i];
		if(jud(tx,ty))
		{
			map[tx][ty]='S';
			ans--;
			dfs(tx,ty);
			map[tx][ty]='.';
			ans++;
		}
	}
}
int main()
{
//	int n,m;
	while(scanf("%d%d",&n,&m)!=EOF,n||m)
	{
		int i,j;
		ans=n*m;
		flag=0;
		for(i=0;i<n;i++)
		{
			scanf("%s",map[i]);
			for(j=0;j<m;j++)
			{
				if(map[i][j]=='S')
					ans--;
			}
		}
		ans--;
		map[0][0]='S';
		dfs(0,0);
		if(flag)
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-06 13:11:20

ZOJ 题目2100 Seeding(DFS)的相关文章

zoj 1008 Gnome Tetravex (dfs+枚举)

Gnome Tetravex Time Limit: 10 Seconds      Memory Limit: 32768 KB Hart is engaged in playing an interesting game, Gnome Tetravex, these days. In the game, at the beginning, the player is given n*n squares. Each square is divided into four triangles m

ZOJ 题目2734 Exchange Cards(DFS 去重OR 母函数)

Exchange Cards Time Limit: 2 Seconds      Memory Limit: 65536 KB As a basketball fan, Mike is also fond of collecting basketball player cards. But as a student, he can not always get the money to buy new cards, so sometimes he will exchange with his

ZOJ 题目1024 Additive equations(DFS)

Additive equations Time Limit: 10 Seconds      Memory Limit: 32768 KB We all understand that an integer set is a collection of distinct integers. Now the question is: given an integer set, can you find all its addtive equations? To explain what an ad

Zoj 2100 Seeding

  <pre class="html" name="code">#include<stdio.h> char map[8][8]; int visited,flag; int n,i,j,m; void dfs(int x,int y) { if( x<0 || y<0 || x>=n || y>=m) return; if(map[x][y]!='.') return; if(flag) return; map[x]

POJ 1071 &amp; HDU 1364 &amp; ZOJ 1019 Illusive Chase(DFS)

题目链接: POJ:http://poj.org/problem?id=1071 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1364 ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=19 Description Tom the robocat is presented in a Robotics Exhibition for an enthusiastic audien

zoj题目分类

饮水思源---zoj 转载自:http://bbs.sjtu.edu.cn/bbscon,board,ACMICPC,file,M.1084159773.A.html 注:所有不是太难的题都被归成了“简单题”,等到发现的时候已经太晚了,我太死脑筋 了……:( 有些题的程序我找不到了,555……:( SRbGa的题虽然都很经典……但是由于其中的大部分都是我看了oibh上的解题报告后做 的,所以就不写了…… 题目排列顺序没有规律……:( 按照个人感觉,最短路有的算做了DP,有的算做了图论. 有些比较

ZOJ 3811 Untrusted Patrol dfs

Untrusted Patrol Time Limit: 3 Seconds      Memory Limit: 65536 KB Edward is a rich man. He owns a large factory for health drink production. As a matter of course, there is a large warehouse in the factory. To ensure the safety of drinks, Edward hir

zoj 3811 Untrusted Patrol DFS SET

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5343 当时是一个学弟过的,真心没想出来,回想起来其实可能有点后悔做ACM了,确实智商不够...... 11去牡丹江比赛,如果悲剧,ACM生涯就彻底悲剧了,尽量出结果......啥不说,专心刷题 此题还是参考了答案,,, 题目要求:按照次序访问某些点,如果能满足而且能遍历全图,输出yes否则no 学到: 1.是不是能按照规定次序,那么就这么看,按照规定次序,DFS第一个点,过程

ZOJ 3436 July Number(DFS)

题意   把一个数替换为这个数相邻数字差组成的数  知道这个数只剩一位数  若最后的一位数是7  则称原来的数为 July Number  给你一个区间  求这个区间中July Number的个数 从7开始DFS  位数多的数总能由位数小的数推出 #include <bits/stdc++.h> using namespace std; const int N = 1e6; int july[N], n; set<int> ans; int pw[] = {1, 10, 100,