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

题意:一个播种的机器,不可以走走过的区域,也不可以走有石头(S)的区域,问是否能走遍图中所有的不含石头的区域

题解:将走过的区域标记为有石头的区域,一直走如果可以走完即ans==s(走过的区域等于所有不含石头的区域)则返回输出YES否则回溯

并清除标记,换其他路线继续搜索,直至结束

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

  

zoj 2100 Seeding的更多相关文章

  1. Num 36 : ZOJ 2100 [ 深度优先搜索算法 ] [ 回溯 ]

    该题是用回溯法来解决的题: 题目: Seeding Time Limit: 2 Seconds      Memory Limit: 65536 KB It is spring time and fa ...

  2. HZNU Training 1 for Zhejiang Provincial Collegiate Programming Contest

    赛后总结: TJ:今天我先到实验室,开始看题,一眼就看了一道防AK的题目,还居然觉得自己能做wwww.然后金姐和彭彭来了以后,我和他们讲了点题目.然后金姐开始搞dfs,我和彭彭看榜研究F题.想了很久脑 ...

  3. 【转】POJ百道水题列表

    以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...

  4. ZOJ题目分类

    ZOJ题目分类初学者题: 1001 1037 1048 1049 1051 1067 1115 1151 1201 1205 1216 1240 1241 1242 1251 1292 1331 13 ...

  5. ZOJ People Counting

    第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ  3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...

  6. ZOJ 3686 A Simple Tree Problem

    A Simple Tree Problem Time Limit: 3 Seconds      Memory Limit: 65536 KB Given a rooted tree, each no ...

  7. ZOJ Problem Set - 1394 Polar Explorer

    这道题目还是简单的,但是自己WA了好几次,总结下: 1.对输入的总结,加上上次ZOJ Problem Set - 1334 Basically Speaking ac代码及总结这道题目的总结 题目要求 ...

  8. ZOJ Problem Set - 1392 The Hardest Problem Ever

    放了一个长长的暑假,可能是这辈子最后一个这么长的暑假了吧,呵呵...今天来实验室了,先找了zoj上面简单的题目练练手直接贴代码了,不解释,就是一道简单的密文转换问题: #include <std ...

  9. ZOJ Problem Set - 1049 I Think I Need a Houseboat

    这道题目说白了是一道平面几何的数学问题,重在理解题目的意思: 题目说,弗雷德想买地盖房养老,但是土地每年会被密西西比河淹掉一部分,而且经调查是以半圆形的方式淹没的,每年淹没50平方英里,以初始水岸线为 ...

随机推荐

  1. 打印出不同顺序的字符串&单引号和双引号的差异

    发现一个很好玩的打印顺序 package com.liaojianya.chapter1; /** * This program demonstrates the string. * @author ...

  2. Fibonacci 数列递归 重复计算

    public class Fibonacci{ public static long F(long n){ System.out.println("call F" + n); ) ...

  3. Activity singleTop启动模式

    栈顶单例模式 和standard模式一样,  只有Activity已经存在并且位于栈顶时, 不会重新创建. 其他时候都会创建新的Activity,然后放在栈顶

  4. Ubuntu14.04不支持U盘exfat格式该如何解决

    转: http://www.jb51.net/os/Ubuntu/275158.html exfat是U盘的文件系统,很多系统都支持exfat格式的使用,但Ubuntu系统并不支持exfat格式,要如 ...

  5. 用vscode写博客和发布

    最近想开始写点博客什么的,然后看到在博客园注册了一个账号这么久,也没有写过文章,就想在博客园写点什么来刷个存在感,而且觉得用Markdown编辑器来写文章挺不错,但是博客园自带的Markdown编辑器 ...

  6. about Red_Hat_Enterprise_Linux_7

    systemd systemd 是 Linux 的系统和服务管理程序,替换了 Red Hat Enterprise Linux 之前的发行本中使用的 SysV.systemd 与 SysV 和 Lin ...

  7. 2016022603 - redis数据类型

    Redis支持5种类型的数据类型 1.字符串:Redis字符串是字节序列.Redis字符串是二进制安全的,这意味着他们有一个已知的长度没有任何特殊字符终止,所以你可以存储任何东西,512兆为上限.[类 ...

  8. LightOj_1342 Aladdin and the Magical Sticks

    题目链接 题意: 地上有n种棍子, 其中有两种类型, 一种类型是可识别, 一种类型是不可识别, 每个棍子都有一个权值. 当你捡到可识别的, 那么你以后就不会再捡这个棍子, 如果是不可识别的, 那么你有 ...

  9. Welcome to JimmyCheung's blog!

    博客开通,写写学习笔记,写写心情,写写生活中的点点滴滴~ 有钱的捧个钱场嘞,没钱的贡献个点击量,新鲜的博客出炉咯,来五毛钱的博文呗~ By Jimmy 2014.09.16

  10. python 运行 hadoop 2.0 mapreduce 程序

    要点:#!/usr/bin/python 因为要发送到各个节点,所以py文件必须是可执行的. 1) 统计(所有日志)独立ip数目,即不同ip的总数 ####################本地测试## ...