Seeding

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 101   Accepted Submission(s) : 52
Problem Description
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

题解:跟南阳最小步数差不多,这个是耕过的地就不再耕了,所以带个回溯,记录步数,如果走的步数等于‘.’的总个数就符合找不到就不对;

代码:

 #include<stdio.h>
int m,n,tot,nos;
bool flag;
char table[][];
void dfs(int x,int y){
    //max=tot>max?tot:max;
    if(table[x][y]=='S'||x<||x>=n||y>=m||y<)return;
    table[x][y]='S';
    tot++;
    if(tot==nos){
        flag=true;
        return;
    }
    dfs(x+,y);
    dfs(x,y+);
    dfs(x-,y);
    dfs(x,y-);
    tot--;//回溯;
    table[x][y]='.';//回溯;
    return ;
}
int main(){
    int x,y,i,j;
    while(~scanf("%d%d",&n,&m),n||m){//max=0;
        for(x=;x<n;x++)scanf("%s",table[x]);
        nos=;tot=;flag=false;
        for(x=;x<n;x++){
            for(y=;y<m;y++){
                if(table[x][y]=='.'){
                    nos++;
                    if(nos==)i=x,j=y;
                }
            }
        }
        dfs(i,j);
    //    printf("%d\n",max);
        //for(x=0;x<n;x++)printf("%s\n",table[x]);
        if(flag)puts("YES");
        else puts("NO");
    }
    return ;
}

Seeding(dfs)的更多相关文章

  1. 素数环(dfs+回溯)

    题目描述: 输入正整数n,把整数1,2...n组成一个环,使得相邻两个数和为素数.输出时从整数1开始逆时针排列并且不能重复: 例样输入: 6 例样输出: 1 4 3 2 5 6 1 6 5 2 3 4 ...

  2. (DFS)codevs1004-四子连棋

    题目地址 方法是建立dfs,并在其中加入pre变量,记录之前移动的是W还是B.外面套for循环,从1步开始逐次递增,直到在i步时可以走完(dfs返回1),break退出循环,即为最短步. 本题的关键主 ...

  3. LeetCode Subsets II (DFS)

    题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...

  4. LeetCode Subsets (DFS)

    题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...

  5. HDU 2553 N皇后问题(dfs)

    N皇后问题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 在 ...

  6. POJ 4003 Bob’s Race && HDU4123 Bob’s Race (dfs+rmq)

    Bob’s Race Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 378   Accepted: 119 Descript ...

  7. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  8. 【算法导论】图的深度优先搜索遍历(DFS)

    关于图的存储在上一篇文章中已经讲述,在这里不在赘述.下面我们介绍图的深度优先搜索遍历(DFS). 深度优先搜索遍历实在访问了顶点vi后,访问vi的一个邻接点vj:访问vj之后,又访问vj的一个邻接点, ...

  9. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

随机推荐

  1. python-pcap模块解析mac地址

    python-pcap模块解析mac地址 作者:vpoet mail:vpoet_sir@163.com import pcap import binascii a = pcap.pcap() a.s ...

  2. ZOJ3578(Matrix)

    Matrix Time Limit: 10 Seconds      Memory Limit: 131072 KB A N*M coordinate plane ((0, 0)~(n, m)). I ...

  3. apache http配置https

    <一,Lamp系统搭建> yum install httpd httpd-devel mysql mysql-server mysql-devel php php-mysql php-co ...

  4. Android消息机制不完全解析(上)

        Handler和Message是Android开发者常用的两个API,我一直对于它的内部实现比较好奇,所以用空闲的时间,阅读了一下他们的源码.    相关的Java Class: androi ...

  5. 点击链接直接跳转到 App Store 指定应用下载页面

    //跳转到应用页面 NSString *str = [NSString stringWithFormat:@"http://itunes.apple.com/us/app/id%d" ...

  6. UESTC-888-Absurdistan Roads(kruskal+floyd)

    The people of Absurdistan discovered how to build roads only last year. After the discovery, every c ...

  7. 自定义view(自定义view的时候,三个构造函数各自的作用)

    package com.timeshare.tmband.Utils; import android.content.Context; import android.content.res.Typed ...

  8. AppSettings

    1.winform中读写配置文件appSettings 一节中的配置. #region 读写配置文件 /// <summary> /// 修改配置文件中某项的值 /// </summ ...

  9. Visual Studio宏注释模板

    前言 有时写代码需要写注释的时候 甚是苦恼 写吧 怕麻烦 不写吧 似乎这代码估计自己都看不懂 权衡之下 似乎找一个自动写注释的方法最靠谱 一直在VS下开发 偶尔听人说过有一个宏工具可以帮助开发者快速注 ...

  10. GPS坐标转换

    由于经常涉及到GPS程序的编写,现在貌似这个GPS是越来越火,越来越多的朋友在编写GPS程序,估计是个人都会遇到这个GPS坐标转换的问题,很惭愧的是,作为一个测量专业出身的学生,我还得时不时的要把这些 ...