该题是用回溯法来解决的题:

题目:

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代表障碍物 )。

题目分析:

从左上角開始,利用回溯法进行深搜。推断有没有一种情况。能不回头的把一条路走完;

AC代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int x,y,m,n,snum,max=0,flag;
char map[10][10];
void fun(int x,int y)
{
if(x<1||x>m||y<1||y>n) return;
if(map[x][y]=='S') return;
if(flag==1) return;
map[x][y]='S';
snum++;
if(snum==m*n)
{
flag=1;
return;
}
fun(x-1,y);
fun(x+1,y);
fun(x,y-1);
fun(x,y+1);
snum--;
map[x][y]='.';
}
int main()
{
int i,j;
while(scanf("%d%d",&m,&n),m|n)
{
snum=0;
for(i=1; i<=m; i++)
for(j=1; j<=n; j++)
{
scanf(" %c",&map[i][j]);
if(map[i][j]=='S') snum++;
}
flag=0;
fun(1,1);
if(flag) printf("YES\n");
else printf("NO\n");
}
return 0;
}

Num 36 : ZOJ 2100 [ 深度优先搜索算法 ] [ 回溯 ]的更多相关文章

  1. 深度优先搜索算法(DFS)以及leetCode的subsets II

    深度优先搜索算法(depth first search),是一个典型的图论算法.所遵循的搜索策略是尽可能“深”地去搜索一个图. 算法思想是: 对于新发现的顶点v,如果它有以点v为起点的未探测的边,则沿 ...

  2. 图的深度优先搜索算法DFS

    1.问题描写叙述与理解 深度优先搜索(Depth First Search.DFS)所遵循的策略.如同其名称所云.是在图中尽可能"更深"地进行搜索. 在深度优先搜索中,对最新发现的 ...

  3. Leetcode之深度优先搜索&回溯专题-679. 24 点游戏(24 Game)

    Leetcode之深度优先搜索&回溯专题-679. 24 点游戏(24 Game) 深度优先搜索的解题详细介绍,点击 你有 4 张写有 1 到 9 数字的牌.你需要判断是否能通过 *,/,+, ...

  4. 深度优先搜索算法(Depth-First-Search,DFS)

    深度优先搜索算法的概念 与广度优先搜索算法不同,深度优先搜索算法类似与树的先序遍历.这种搜索算法所遵循的搜索策略是尽可能"深"地搜索一个图.它的基本思想如下:首先访问图中某一个起始 ...

  5. Leetcode之深度优先搜索&回溯专题-491. 递增子序列(Increasing Subsequences)

    Leetcode之深度优先搜索&回溯专题-491. 递增子序列(Increasing Subsequences) 深度优先搜索的解题详细介绍,点击 给定一个整型数组, 你的任务是找到所有该数组 ...

  6. Leetcode之深度优先搜索&回溯专题-980. 不同路径 III(Unique Paths III)

    Leetcode之深度优先搜索&回溯专题-980. 不同路径 III(Unique Paths III) 深度优先搜索的解题详细介绍,点击 在二维网格 grid 上,有 4 种类型的方格: 1 ...

  7. Leetcode之深度优先搜索&回溯专题-638. 大礼包(Shopping Offers)

    Leetcode之深度优先搜索&回溯专题-638. 大礼包(Shopping Offers) 深度优先搜索的解题详细介绍,点击 在LeetCode商店中, 有许多在售的物品. 然而,也有一些大 ...

  8. Python数据结构与算法之图的广度优先与深度优先搜索算法示例

    本文实例讲述了Python数据结构与算法之图的广度优先与深度优先搜索算法.分享给大家供大家参考,具体如下: 根据维基百科的伪代码实现: 广度优先BFS: 使用队列,集合 标记初始结点已被发现,放入队列 ...

  9. 【2018.07.29】(深度优先搜索/回溯)学习DFS算法小记

    参考网站:https://blog.csdn.net/ldx19980108/article/details/76324307 这个网站里有动态图给我们体现BFS和DFS的区别:https://www ...

随机推荐

  1. python中unicode, hex, bin之间的转换

    python中unicode, hex, bin之间的转换 背景 在smb中有个feature change notify, 需要改动文件权限dacl,然后确认是否有收到notify.一直得不到这个d ...

  2. tornado框架基础11-tornado异步

    01 同步和异步 生活中常常会遇到在超市排队买东西的情况,排在你前面的人没有结算完成,你就无法付账,在计算机中也有类似的情形,一个程序在执行之前,需要等待其他的程序执行完成,大家还能举出其他的例子吗? ...

  3. Vijos 1308 埃及分数(迭代加深搜索)

    题意: 输入a.b, 求a/b 可以由多少个埃及分数组成. 埃及分数是形如1/a , a是自然数的分数. 如2/3 = 1/2 + 1/6, 但埃及分数中不允许有相同的 ,如不可以2/3 = 1/3 ...

  4. 【数据传输 2】批量导入的前奏:将Excel表中的数据转换为DataTable类型

    导读:我们知道,在数据库中,数据集DataSet是由多张DataTable表组成.所以,如果我们需要将数据从外部导入到数据库中,那么要做的很重要的一步是将这些数据转换为数据库可以接受的结构.今天在用S ...

  5. POJ 1006-Biorhythms,中国剩余定理,学信安的路过!

                                                       Biorhythms 我竟然1A了, 终于从一天的浑噩中找回点自信了.人生第一次做中国剩余定理的题 ...

  6. [codevs3622] 假期(单调队列)

    传送门 首先考虑暴力做法,可以先求一遍前缀和 sum,然后ans = max(ans, sum[i] - sum[k]) (i - q <= k <= i - p) 但这个肯定会超时. 仔 ...

  7. jQuery根据属性模糊匹配元素

    1.查看带有指定属性的元素: [attribute] 例如: $("div[id]") 2.查看属性值是某个特定值的元素: [attribute=value] 例如: $(&quo ...

  8. AndroidUI的基本结构

    AndroidUI的基本结构 创建时间: 2013-9-13 11:05 更新时间: 2013-9-13 11:05

  9. gerrit ssh 登陆设置

    [root@web ~]# cat ~/.ssh/config Host gerrit User deploy-gerrit Port Hostname gerrit.demo.com Identit ...

  10. CSS属性操作二

    9.float属性 基本浮动规则 先来了解一下block元素和inline元素在文档流中的排列方式. block元素通常被现实为独立的一块,独占一行,多个block元素会各自新起一行,默认block元 ...