DFS基础题
hdu 1241 油田 裸DFS
题意:@代表油田 8个方向上还有@就相连 相当于求图中连通子图的个数
Sample Input
1 1 // n m
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
Sample Output
0
1
2
2
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std; int d[][] = {{,},{,-},{,},{,-},{,},{-,},{-,-},{-,}};
char map[][] ; int n , m ; void dfs(int x , int y)
{
int fx , fy ;
int i ;
map[x][y] = '*' ;
for (i = ; i < ; i++)
{
fx = x + d[i][] ;
fy = y + d[i][] ;
if (fx>= && fx<n && fy>= && fy<m && map[fx][fy] == '@')
{
dfs(fx,fy) ;
}
}
} int main()
{
//freopen("in.txt","r",stdin) ; while (scanf("%d %d" , &n , &m) !=EOF)
{
if (n == && m == )
break ;
int i , j ; for (i = ; i < n ; i++)
{
for (j = ; j < m ; j++)
{
cin>>map[i][j]; //用scanf一直WA=.=
} }
int sum = ;
for (i = ; i < n ; i++)
for (j = ; j < m ; j++)
{
if (map[i][j] == '@')
{
sum++ ; dfs(i,j) ;
}
}
printf("%d\n" , sum) ; } return ;
}
hdu 2952
上下左右 4个方向相连 求连通子图的个数
Sample Input
2
4 4
#.#. //#能走
.#.#
#.##
.#.#
3 5
###.#
..#..
#.###
Sample Output
6
3
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std; int d[][] = {{,},{,-},{,},{-,}};
char map[][] ; int n , m ; void dfs(int x , int y)
{
int fx , fy ;
int i ;
map[x][y] = '.' ;
for (i = ; i < ; i++)
{
fx = x + d[i][] ;
fy = y + d[i][] ;
if (fx>= && fx<n && fy>= && fy<m && map[fx][fy] == '#')
{
dfs(fx,fy) ;
}
}
} int main()
{
// freopen("in.txt","r",stdin) ;
int T ;
scanf("%d" , &T) ; while (T--)
{
scanf("%d %d" , &n , &m) ;
if (n == && m == )
break ;
int i , j ; for (i = ; i < n ; i++)
{
for (j = ; j < m ; j++)
{
cin>>map[i][j];
} }
int sum = ;
for (i = ; i < n ; i++)
for (j = ; j < m ; j++)
{
if (map[i][j] == '#')
{
sum++ ;
dfs(i,j) ;
}
}
printf("%d\n" , sum) ; } return ;
}
hdu 1312 能走多少格 裸DFS
Sample Input
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#......... //.能走 #不能走 @起点
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
Sample Output
45
59
6
13
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std; int d[][] = {{,},{,-},{,},{-,} } ;
char map[][] ; int n , m ;
int sum = ; void dfs(int x , int y)
{
int fx , fy ;
int i ;
sum++ ;
map[x][y] = '#';
for (i = ; i < ; i++)
{
fx = x + d[i][] ;
fy = y + d[i][] ;
if (fx>= && fx<n && fy>= && fy<m && map[fx][fy] == '.')
{
dfs(fx,fy) ;
}
}
} int main()
{
// freopen("in.txt","r",stdin) ; while (scanf("%d %d" , &m , &n) !=EOF)
{
if (n == && m == )
break ;
int i , j ;
int bx , by ;
sum = ; for (i = ; i < n ; i++)
{
for (j = ; j < m ; j++)
{
cin>>map[i][j];
} }
for (i = ; i < n ; i++)
{
for (j = ; j < m ; j++)
{
if (map[i][j] == '@')
{
bx = i ;
by = j ;
}
} } dfs(bx,by) ; printf("%d\n" , sum) ; } return ;
}
DFS基础题的更多相关文章
- 2-sat基础题 uvalive 3211
蓝书325页的基础题 二分+2-sat //看看会不会爆int!数组会不会少了一维! //取物问题一定要小心先手胜利的条件 #include <bits/stdc++.h> using n ...
- Android测试基础题(三)
今天接着给大家带来的是Android测试基础题(三). 需求:定义一个排序的方法,根据用户传入的double类型数组进行排序,并返回排序后的数组 俗话说的好:温故而知新,可以为师矣 packag ...
- 小试牛刀3之JavaScript基础题
JavaScript基础题 1.让用户输入两个数字,然后输出相加的结果. *prompt() 方法用于显示可提示用户进行输入的对话框. 语法: prompt(text,defaultText) 说明: ...
- 小试牛刀2:JavaScript基础题
JavaScript基础题 1.网页中有个字符串“我有一个梦想”,使用JavaScript获取该字符串的长度,同时输出字符串最后两个字. 答案: <!DOCTYPE html PUBLIC &q ...
- HDU 1301 Jungle Roads (最小生成树,基础题,模版解释)——同 poj 1251 Jungle Roads
双向边,基础题,最小生成树 题目 同题目 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<stri ...
- nyist oj 79 拦截导弹 (动态规划基础题)
拦截导弹 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描写叙述 某国为了防御敌国的导弹突击.发展中一种导弹拦截系统.可是这样的导弹拦截系统有一个缺陷:尽管它的第一发炮弹可以 ...
- POJ 1321 棋盘问题(DFS板子题,简单搜索练习)
棋盘问题 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44012 Accepted: 21375 Descriptio ...
- poj1564 Sum It Up dfs水题
题目描述: Description Given a specified total t and a list of n integers, find all distinct sums using n ...
- linux面试题-基础题1
第1章 基础题1 1.1 在装系统创建Linux分区时,一般至少需要创建两个分区( ) A.FAT.NTFS B. /usr.swap C. /boot.swap D.swap./ 1.2 ...
随机推荐
- Gitlab部署
前言 因部门业务整顿,需将原有的多部门共用的Gitlab环境遗弃,新建部门独立的Gitlab环境! 安装 CE版本安装操作:https://about.gitlab.com/install/#cent ...
- 逆向安全基础之IDA使用简介
转载:http://m.blog.csdn.net/ilnature2008/article/details/54912854 IDA简介 IDA是业界一个功能十分强大的反汇编工具,是安全渗透人员进行 ...
- 网站程序CMS识别
CMS cms一般有dedecms(织梦),dzcms,phpweb,phpwind,phpcms,ecshop,dvbbs,siteweaver,aspcms,帝国,zblog,wordpress等 ...
- apache - http
apahce 添加模块编译 httpd # so模块用来提供DSO支持的apache核心模块 # 如果编译中包含任何DSO模块,则mod_so会被自动包含进核心. # 如果希望核心能够装载DSO, ...
- 【摘】SVN提交与版本冲突
一般性解决办法 1.要提交的内容备份到项目之外[为还原版本做准备] 2.还原[回到之前版本] 3.更新[更新版本号和版本] 4.填充内容[即 将自己之前备份的内容填充项目对应处] 5.提交 6.OK ...
- 字符加密 Valentino 函数 (伪分治)
题面 \(solution:\) 这一题重点不在字符串加密,而是我们最后的求值:\(K^{s}\mod M\)(\(s\leq36^{100000}\)) 而我们发现它的指数十分巨大,但众所周知的指数 ...
- 利用.frm、.ibd恢复数据
我们知道启用innodb_file_per_table选项后,单个表(InnoDB引擎)的数据和索引放入单独的文件中(.ibd),建表语句保存在.frm文件中本文假设192.168.85.132,33 ...
- vue axios全攻略
不再继续维护vue-resource,并推荐大家使用 axios 开始,axios 被越来越多的人所了解.本来想在网上找找详细攻略,突然发现,axios 的官方文档本身就非常详细!!有这个还要什么自行 ...
- maven配置jdk1.8环境
<!-- 局部jdk配置,pom.xml中 --> <build> <plugins> <plugin> <groupId>org.apac ...
- Java泛型方法与泛型类的使用------------(五)
泛型的本质就是将数据类型也参数化, 普通方法的输入参数的值是可以变的,但是类型(比如: String)是不能变的,它使得了在面对不同类型的输入参数的时候我们要重载方法才行. 泛型就是将这个数据类型也搞 ...