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 ...
随机推荐
- SQL语句(一)SQL和数据库数据表的创建
SQL的组成 (1) 数据定义语言DDL(Data Definition Language) 用于数据库和数据表的创建.修改和删除等操作 CREATE (create) 创建数据库.数据表 ALTER ...
- 从池子里的beta看秋香, 个性迥异
从池子里的beta看秋香, 个性迥异 前文里那十只个股为例, 统计了它们的beta值. 回顾如下: Num name code Beta 0 深圳燃气 601139 0.710 公用事业 1 分众传媒 ...
- Java 集合和映射表
集合 可以使用集合的三个具体类HashSet.LinkedHashSet.TreeSet来创建集合 HashSet类 负载系数 当元素个数超过了容量与负载系数的乘积,容量就会自动翻倍 HashSet类 ...
- JS结合a标签的使用
a标签可以当作按钮使用,也可以当作连接. <a href=javascript:test(5)>弹出5</a> 会直接调用JS函数(注意中间没引号) <a href ...
- kali linux 下搭建git服务器
参考:http://www.cnblogs.com/dee0912/p/5815267.html https://www.liaoxuefeng.com/wiki/001373951630592960 ...
- 【Python】多线程-线程池使用
1.学习目标 线程池使用 2.编程思路 2.1 代码原理 线程池是预先创建线程的一种技术.线程池在还没有任务到来之前,创建一定数量的线程,放入空闲队列中.这些线程都是处于睡眠状态,即均为启动,不消耗 ...
- 【转】Python之文件读写
[转]Python之文件读写 本节内容: I/O操作概述 文件读写实现原理与操作步骤 文件打开模式 Python文件操作步骤示例 Python文件读取相关方法 文件读写与字符编码 一.I/O操作概述 ...
- Python中的exec、eval使用实例
Python中的exec.eval使用实例 这篇文章主要介绍了Python中的exec.eval使用实例,本文以简洁的方式总结了Python中的exec.eval作用,并给出实例,需要的朋友可以参考下 ...
- ansible报错Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this
安装和配置好ansible,执行命令时报错如下 [root@test01 ansible-install]# ansible test -m shell -a 'w' >> Using a ...
- shell脚本中冒号
格式:: your comment here 格式:# your comment here 写代码注释(单行注释). 例如: 格式:: 'comment line1 comment line2 mor ...