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 ...
随机推荐
- (7) 将tomcat HTTP连接器启动在80端口(jsvc使用详解)
让tomcat在80端口上运行 法一: 修改连接器的端口8080为80 <Connector port="8080" protocol="HTTP/1.1" ...
- 如何实时查看mysql当前连接数?
1.查看当前所有连接的详细资料: ./mysqladmin -uadmin -p -h10.140.1.1 processlist2.只查看当前连接数(Threads就是连接数.): ./mysqla ...
- css选择器(2)——属性选择器和基于元素结构关系的选择器
在有些标记语言中,不能使用类名和id选择器,于是css2引入了属性选择器. 3.属性选择器 a)根据是否存在该属性来选择 如果希望选择有某个属性的元素,例如要选择有class属性的所有h1元素,使其文 ...
- LeetCode(11) Container With Most Water
题目 Given n non-negative integers a1, a2, -, an, where each represents a point at coordinate (i, ai). ...
- XTU 二分图和网络流 练习题 C. 方格取数(1)
C. 方格取数(1) Time Limit: 5000ms Memory Limit: 32768KB 64-bit integer IO format: %I64d Java class ...
- POJ-2590-Steps题目详解,思路分析及代码,规律题,重要的是找到规律~~
Steps Time Limit: 1000MS Memory Limit: 65536K http://poj.org/problem?id=2590 Description One ...
- [Istio]Kubernetes集群部署Istio 1.0
大部分内容都是可以根据https://istio.io/docs/setup/kubernetes/quick-start/来处理的,这里主要谈部署时一些细节的问题 首先,当我们按照 istio 官方 ...
- shell脚本简单密码加密
#!/bin/sh #输入密码 echo "请输入原密码:" read resultFirst firstPWD=$resultFirst echo "请再次输入原密码: ...
- hdu3853:LOOPS
题目大意:r*c个点,每个点有Aij的概率回到自己本身,Bij的概率向右一格,Cij的概率向下一格,求从(1,1)到(r,c)的期望步数. 题解:有了hdu4405的经验,从后往前推期望.那么,E(i ...
- BZOJ1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列
n<=100000个数表示每头牛在K<=30种物品的选取情况,该数在二进制下某位为0表示不选1表示选,求一个最大的区间使区间内选择每种物品的牛一样多. 数学转化,把不同状态间单变量的关系通 ...