DFS入门之一
深度优先搜索实现较为简单,需要控制两个因素:
1.已经访问过的元素不能再访问,在实际题目中还要加上不能访问的元素(障碍)
2.越界这种情况是不允许的
以杭电的1312 Red and Black 为例, 这是一道典型的DFS题目
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1312
题目大意:'@'代表起始位置, '.' 代表黑点(可以穿越),'#' 代表红点(障碍) 要求统计最多可以覆盖多少个黑点
题目分析:确定起始位置 -> 开始DFS -> 如果不是非法位置,计数一次 -> 递归的搜索四个方向(UP, DOWN, LEFT, RIGHT) ->结束
附上我的代码(仅供参考):
#include <cstdio>
#include <cstring>
const int maxn = + ;
char a[maxn][maxn];
int w, h, idx[maxn][maxn], cnt;
int dfs(int r, int c, int id)
{
if(r < || r >= h || c < || c >= w) return ;
if(idx[r][c] > || a[r][c] == '#') return ;
idx[r][c] = id;
cnt++;
for(int i = -; i <= ; i++)
for(int j = -; j <= ; j++)
if((i == && j != )||(i != && j == )) dfs(r+i, c+j, id);
} int main()
{
int r, c;
while(~scanf("%d%d", &w, &h)){
cnt = ;
if(w == && h == ) break;
for(int i = ; i < h; i++) scanf("%s", a[i]);
memset(idx, , sizeof(idx));
for(int i = ; i < h; i++)
for(int j = ; j < w; j++)
if(a[i][j] == '@'){
r = i;
c = j;
}
dfs(r, c, );
printf("%d\n", cnt);
}
return ;
}
参考一下?
DFS入门之一的更多相关文章
- 算法学习之BFS、DFS入门
算法学习之BFS.DFS入门 0x1 问题描述 迷宫的最短路径 给定一个大小为N*M的迷宫.迷宫由通道和墙壁组成,每一步可以向相邻的上下左右四格的通道移动.请求出从起点到终点所需的最小步数.如果不能到 ...
- Oil Deposits(poj 1526 DFS入门题)
http://poj.org/problem?id=1562 ...
- DFS入门之二---DFS求连通块
用DFS求连通块也是比较典型的问题, 求多维数组连通块的过程也称为--“种子填充”. 我们给每次遍历过的连通块加上编号, 这样就可以避免一个格子访问多次.比较典型的问题是”八连块问题“.即任意两格子所 ...
- poj1562 DFS入门
K - 搜索 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:10000KB 64bit I ...
- [HDU]1016 DFS入门题
题目的意思就是在1到n的所有序列之间,找出所有相邻的数相加是素数的序列.Ps:题目是环,所以头和尾也要算哦~ 典型的dfs,然后剪枝. 这题目有意思的就是用java跑回在tle的边缘,第一次提交就tl ...
- DFS入门__poj1979
Red and Black Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 26944 Accepted: 14637 D ...
- POJ 3984(DFS入门题 +stack储存路径)
POJ 3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, ...
- HDU 1241 连通块问题(DFS入门题)
Input The input file contains one or more grids. Each grid begins with a line containing m and n, th ...
- POJ 1416 Shredding Company【dfs入门】
题目传送门:http://poj.org/problem?id=1416 Shredding Company Time Limit: 1000MS Memory Limit: 10000K Tot ...
随机推荐
- c++笔试题两道,求解当中一道
1.Implement a functionthat prints the numbers from 1 to 100.But for multiples of three(3) print &quo ...
- C语言程序设计基础
C语言程序设计基础 目录 C语言 C语言基础 C语言编程注意 C语言 C语言基础 C语言编程注意 0<9<9和0<9&&9<9是不同的 数组的声明和定义 con ...
- 和iPhone有关的视图控制器:UIViewController、UITabBarController、UINavigationController及其混合用法
iPhone中的view视图是应用程序对于数据最直观.最直接的呈现方式,如下是我在学习了iPhone中的视图控制器以及由其衍生的特殊子类的总结,希望对那些初学者有所帮助: UIViewControll ...
- 下一个系列学习列表Spring.net+NHibernate+MVC
开源框架完美组合之Spring.NET + NHibernate + ASP.NET MVC + jQuery + easyUI 中英文双语言小型企业网站Demo 刘冬.NET 2011-08-19 ...
- NoSQL数据库的分布式算法&&memcache集群的实现
NoSQL数据库的分布式算法 http://blog.nosqlfan.com/html/4139.html 一致性hash算法在memcache集群中的应用 http://alunblog.d ...
- Common Linux log files name and usage--reference
reference:http://www.coolcoder.in/2013/12/common-linux-log-files-name-and-usage.html if you spend lo ...
- Inversions
There are N integers (1<=N<=65537) A1, A2,.. AN (0<=Ai<=10^9). You need to find amount o ...
- A+B Coming
Problem Description Many classmates said to me that A+B is must needs.If you can’t AC this problem, ...
- AliasRegistry接口
Spring - 4.2.3 // 将一个name注册为一个别名aliasvoid registerAlias(String name, String alias);// 移除一个别名aliasvoi ...
- 问题集-- SQL 约束名不能重复
今天看书整理笔记的时候,无意发现一个问题,记录下来: 前段时间做练习曾经创建过一个tbl_student的表, create table tbl_student( ...