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 ...
随机推荐
- vue-loader 细节
vue-loader 能根据 .vue 文件,导入一个vue组件.我这里从 vue-cli 的构建项目中抽取了vue-loader 一个小例子出来:vuedemo/demo02 vue-loader ...
- Django 多个字段关联同一外键
# -*- coding: utf-8 -*- """ Tencent is pleased to support the open source community b ...
- SpringMVC中的几种事务管理器
转载https://blog.csdn.net/qq_26222859/article/details/52032853 1JDBC及iBATIS.MyBatis框架事务管理器 <bean id ...
- 算法导论 第六章 堆排序(python)
6.1堆 卫星数据:一个带排序的的数通常是有一个称为记录的数据集组成的,每一个记录有一个关键字key,记录的其他数据称为卫星数据. 原地排序:在排序输入数组时,只有常数个元素被存放到数组以外的空间中去 ...
- robotframework使用requestsLibrary进行接口测试
一.定义 接口测试:接口测试通常是系统之间交互的接口,或者某个系统对外提供的一些接口服务 分类:RESTful.webservice接口 二.安装 进入C:\Pyhon27\scripts 先要安装r ...
- Vmware改成bridge方式联网
1.在使用桥接之前,先在真机的'更改适配器设置中'禁用vmnet1和vmnet8 2.在VMware中定义一个桥接器 3.设置这个Linux虚拟机使用前一个步骤定义的桥接器--进入桥接器选择界面 4. ...
- String字符串去掉双引号
public static String stringReplace(String str) { //去掉" "号 String str= str.replace("\& ...
- jar包、war包、ear包傻傻分不清?
在工作中,需要在jboss上deploy一个health check的war包,因此了解一下: Jar文件(扩展名为. Jar,Java Application Archive)包含Java类的普通库 ...
- FZU- Problem 1147 Tiling,递推坑题,大数水过~~
Problem 1147 Tiling Time Limit: 1000 mSec Memory Limit : 32768 KB http://acm.fzu.edu.cn/problem.php? ...
- SDWebImage实现分析
该博文来自南峰子的技术博客,文章从下载和缓存俩个大的组件分析到里面一些核心方法的实现,条理清晰,相对于一些一上来就通篇分析实现思路的技术文章, 这篇的讲解思路明确,框架架构也讲的比较清楚.看完这篇再去 ...