Num 36 : ZOJ 2100 [ 深度优先搜索算法 ] [ 回溯 ]
该题是用回溯法来解决的题:
题目:
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 [ 深度优先搜索算法 ] [ 回溯 ]的更多相关文章
- 深度优先搜索算法(DFS)以及leetCode的subsets II
深度优先搜索算法(depth first search),是一个典型的图论算法.所遵循的搜索策略是尽可能“深”地去搜索一个图. 算法思想是: 对于新发现的顶点v,如果它有以点v为起点的未探测的边,则沿 ...
- 图的深度优先搜索算法DFS
1.问题描写叙述与理解 深度优先搜索(Depth First Search.DFS)所遵循的策略.如同其名称所云.是在图中尽可能"更深"地进行搜索. 在深度优先搜索中,对最新发现的 ...
- Leetcode之深度优先搜索&回溯专题-679. 24 点游戏(24 Game)
Leetcode之深度优先搜索&回溯专题-679. 24 点游戏(24 Game) 深度优先搜索的解题详细介绍,点击 你有 4 张写有 1 到 9 数字的牌.你需要判断是否能通过 *,/,+, ...
- 深度优先搜索算法(Depth-First-Search,DFS)
深度优先搜索算法的概念 与广度优先搜索算法不同,深度优先搜索算法类似与树的先序遍历.这种搜索算法所遵循的搜索策略是尽可能"深"地搜索一个图.它的基本思想如下:首先访问图中某一个起始 ...
- Leetcode之深度优先搜索&回溯专题-491. 递增子序列(Increasing Subsequences)
Leetcode之深度优先搜索&回溯专题-491. 递增子序列(Increasing Subsequences) 深度优先搜索的解题详细介绍,点击 给定一个整型数组, 你的任务是找到所有该数组 ...
- Leetcode之深度优先搜索&回溯专题-980. 不同路径 III(Unique Paths III)
Leetcode之深度优先搜索&回溯专题-980. 不同路径 III(Unique Paths III) 深度优先搜索的解题详细介绍,点击 在二维网格 grid 上,有 4 种类型的方格: 1 ...
- Leetcode之深度优先搜索&回溯专题-638. 大礼包(Shopping Offers)
Leetcode之深度优先搜索&回溯专题-638. 大礼包(Shopping Offers) 深度优先搜索的解题详细介绍,点击 在LeetCode商店中, 有许多在售的物品. 然而,也有一些大 ...
- Python数据结构与算法之图的广度优先与深度优先搜索算法示例
本文实例讲述了Python数据结构与算法之图的广度优先与深度优先搜索算法.分享给大家供大家参考,具体如下: 根据维基百科的伪代码实现: 广度优先BFS: 使用队列,集合 标记初始结点已被发现,放入队列 ...
- 【2018.07.29】(深度优先搜索/回溯)学习DFS算法小记
参考网站:https://blog.csdn.net/ldx19980108/article/details/76324307 这个网站里有动态图给我们体现BFS和DFS的区别:https://www ...
随机推荐
- centos7 中源码安装nginx
使用nginx有一段时间了,还是有很多东西不懂的,在这里做一下自己学习过程中的一些整理,能使自己得到提升. 1.环境:centos7 1511 最小化安装 2.下载nginx,可以在系统中下载,也可 ...
- JAVA:ssm框架搭建
文章来源:http://www.cnblogs.com/hello-tl/p/8328071.html 环境简介 : jdk1.7.0_25/jdk1.8.0_31 tomcat-7.0.81 m ...
- 剑指Offer(书):重建二叉树
题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2, ...
- npm start问题
问题:在执行命令npm start 是出现下列问题: npm [] WARN invalid config loglevel="notice" [] npm WARN invali ...
- Saving James Bond - Easy Version 原创 2017年11月23日 13:07:33
06-图2 Saving James Bond - Easy Version(25 分) This time let us consider the situation in the movie &q ...
- [第一波模拟\day3\T3]{益智游戏}(game.cpp)
[问题描述] 小P和小R在玩一款益智游戏.游戏在一个正权有向图上进行. 小P控制的角色要从A点走最短路到B点,小R控制的角色要从C点走最短路到D点. 一个玩家每回合可以有两种选择,移动到一个相邻节点或 ...
- 实验:iscsi共享存储
实验名称: iscsi共享存储 实验环境: 我们需要准备一个磁盘,对于这个磁盘我们需要使用,将这个磁盘空间共享给iscsi客户端: 实验需求: 我们这里使用两台服务器来实现iscsi共享存储: 1.指 ...
- 如何使用Visual Studio 2008(VS2008)编译C语言
大家在学习C语言的时候接触的一般都是VC6.0.但是VC6.0只能编译C或者C++,不支持C#,集成度不是很高.而且界面并不十分友好,不能自动猜测关键字,函数的参数也不能自动标示.最关键的是,编译的时 ...
- Struts2的Action配置的各项默认值
1 如果没有为action指定class,默认是ActionSupport 2 如果没有为action指定method,默认执行action中的execute()方法 3 如果没有指定result的n ...
- jsp页面遍历输出
<c:foreach>类似于for和foreach循环 以下是我目前见过的用法: 1.循环遍历,输出所有的元素.<c:foreach items="${list}" ...