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. ArcSDE for Oracle表空间管理——暂时(TEMP)表空间

    Oracle暂时表空间主要用来做查询和存放一些缓冲区数据.暂时表空间消耗的主要原因是须要对查询的中间结果进行排序. 重新启动数据库能够释放暂时表空间,假设不能重新启动实例,而一直保持问题sql语句的运 ...

  2. Android 内部存储相关的函数(getCacheDir,getDir, getFileStreamPath,getFilesDir,openFileInput, ...)

    为了保证应用程序存储数据的安全性,开发人员在开发应用程序的过程中须要注意使用Android 应用程序的内部存储空间. 依据不同的要求.将相应的数据文件.缓存文件.暂时文件等分别存储在相应的位置. 注意 ...

  3. C#.NET学习笔记11,12---布尔表达式2组合,if语句

    C#.NET学习笔记11---布尔表达式2组合 2013/9/6 技术qq交流群:JavaDream:251572072  教程下载,在线交流:创梦IT社区:www.credream.com int ...

  4. ORACLE数据库、表空间、表的容量相关查询--1

    未完待续……未完待续……未完待续……未完待续…… 1.查询某个表所占空间大小 col tablespace_name for a15 col segment_name for a15 col segm ...

  5. Sybase isql常用命令

    isql -Usa -S服务名 --常规登录 isql -Usa -S服务名 -Jcp936 --sa用户在客户端使用字符集cp936登录 C:\Users\Administrator>isql ...

  6. java 成神之路

    一.基础篇 1.1 JVM 1.1.1. Java内存模型,Java内存管理,Java堆和栈,垃圾回收 http://www.jcp.org/en/jsr/detail?id=133 http://i ...

  7. java 包之 BeanUtils包的使用

    BeanUtils工具包是由Apache公司所开发,主要是方便程序员对Bean类能够进行简便的操作. 在使用BeanUtils工具包之前我们需要的Jar包有以下几种: (1)   BeanUtils相 ...

  8. IE10以下placeholder不兼容

    做页面的时候在谷歌中是显示的,但是换了IE之后总是不显示,一个文本框还好,如果有多个的话,如图: 添加以下一段Jquery代码: <script> var JPlaceHolder = { ...

  9. fdisk -l 找不到分区怎么办?想办法找到隐藏分区。

    centos6.6 装系统的时候,选取系统默认分区.装好后发现少了一大部分空间,通过fdisk -l 找不到分区,另外使用 parted -l 同样找不到消失的柱面. 如: [root@mysql]# ...

  10. SignalR实现实时日志监控

    .net SignalR实现实时日志监控   摘要 昨天吃饭的时候,突然想起来一个好玩的事,如果能有个页面可以实时的监控网站或者其他类型的程序的日志,其实也不错.当然,网上也有很多成熟的类似的监控系统 ...