DFS入门__poj1979
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 26944 | Accepted: 14637 |
Description
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.
'.' - a black tile
'#' - a red tile
'@' - a man on a black tile(appears exactly once in a data set)
The end of the input is indicated by a line consisting of two zeros.
Output
Sample Input
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
Sample Output
45
59
6
13
/*题目大意:在一个矩形房间里,里面填满着红砖与黑砖,每次走动只能走黑砖位置,而不能走红砖,
* 可以上下左右四个方向走动,现在初始位置在黑砖位置上,试问从此黑砖位置开始能够到达的砖的数量是多少
*算法分析:从起始位置开始四个方向进行dfs,若遇到红砖位置则将此砖块换为黑砖。换砖的次数即为到达的黑砖数量 */ #include <iostream>
#include <cstdio>
using namespace std; char a[25][25];
int n, m;
int dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0}; //四个方向数组 void dfs(int x, int y, int &res) {
a[x][y] = '#';
res ++ ;
for (int i = 0; i<4; i++) {
int nx = x + dir[i][0];
int ny = y + dir[i][1];
if (nx>=0 && nx<n && ny>=0 && ny<m && a[nx][ny] == '.')
dfs(nx, ny, res);
}
} int main() { while (cin >> m >> n && (n+m)) { memset(a, 0, sizeof(a));
for (int i = 0; i<n; i++) {
for (int j = 0; j<m; j++) {
cin >> a[i][j];
}
}
int res = 0;
for (int i = 0; i<n; i++) {
for (int j = 0; j<m; j++) {
if (a[i][j] == '@')
dfs(i, j, res);
}
}
cout << res << endl;
}
return 0;
}
DFS入门__poj1979的更多相关文章
- 算法学习之BFS、DFS入门
算法学习之BFS.DFS入门 0x1 问题描述 迷宫的最短路径 给定一个大小为N*M的迷宫.迷宫由通道和墙壁组成,每一步可以向相邻的上下左右四格的通道移动.请求出从起点到终点所需的最小步数.如果不能到 ...
- Oil Deposits(poj 1526 DFS入门题)
http://poj.org/problem?id=1562 ...
- DFS入门之二---DFS求连通块
用DFS求连通块也是比较典型的问题, 求多维数组连通块的过程也称为--“种子填充”. 我们给每次遍历过的连通块加上编号, 这样就可以避免一个格子访问多次.比较典型的问题是”八连块问题“.即任意两格子所 ...
- DFS入门之一
深度优先搜索实现较为简单,需要控制两个因素: 1.已经访问过的元素不能再访问,在实际题目中还要加上不能访问的元素(障碍) 2.越界这种情况是不允许的 以杭电的1312 Red and Black 为例 ...
- 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 ...
- 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 ...
随机推荐
- Xftp连接阿里云Linux,向Linux上传文件,Windows和Linux文件传输
我之前是用SecureCRT连接阿里云Linux的,上传文件用的Alt+p快捷键,感觉不是很方便.后来朋友给我推荐了Xshell,感觉确实好用得很多. 传输文件用的是Xftp,今天在向我的个人网站发布 ...
- [C#]使用Quartz.NET来创建定时工作任务
本文为原创文章.源代码为原创代码,如转载/复制,请在网页/代码处明显位置标明原文名称.作者及网址,谢谢! 开发工具:VS2017 语言:C# DotNet版本:.Net FrameWork 4.0及以 ...
- ConcurrentHashMap 从Java7 到 Java8的改变
一.关于分段锁 集合框架很大程度减少了java程序员的重复劳动,然而,在Java多线程环境中,以线程安全的方式使用集合类是一个首先考虑的问题. 越来越多的程序员了解到了ConcurrentHashMa ...
- openldap 编译报错MozNSS not found
openldap 编译报错 1)报错 MozNSS not found - please specify the location to the NSPR and NSS header files i ...
- partition length exceeds the loop-partition-table-imposed maximum of 4294967295
问题: 当大于2T的磁盘,在用parted操作的时候,会出现这样的报错,原因是因为现在的分区表是mbr,需要修改为gpt.mbr最大支持2T的空间. 解决方法: 搞清楚问题,解决方法也很简单:mkla ...
- git强制push
Git 如何强制push? $ git push -u origin master –f 文章来源:刘俊涛的博客 地址:http://www.cnblogs.com/lovebing 欢迎关注,有 ...
- spring中Bean后置处理器实现总结
BeanPostProcessor接口 bean的后置处理器实现功能主要是 可以在bean初始化之前和之后做增强处理.自定义MyBeanProcessor实现BeanPostProcessor接口,重 ...
- iOS 关于退出键盘两种方法和避免遮挡
退出键盘: 方法1:不使用代理,直接使用: -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.textFi ...
- 【转】Python 爬虫的工具列表【预】
这个列表包含与网页抓取和数据处理的Python库 网络 通用 urllib -网络库(stdlib). requests -网络库. grab – 网络库(基于pycurl). pycurl – 网络 ...
- Elastic-Job-一个分布式调度解决方案
注:Elastic-Job是一个分布式调度解决方案,由两个相互独立的子项目Elastic-Job-Lite和Elastic-Job-Cloud组成.Elastic-Job-Lite定位为轻量级无中心化 ...