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 ...
随机推荐
- 【算法】论平衡二叉树(AVL)的正确种植方法
参考资料 <算法(java)> — — Robert Sedgewick, Kevin Wayne <数据结构> ...
- Add to List 349. Intersection of Two Arrays
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- Java 读者写者问题
实验存档.V 允许好几个人同时读,但是不允许在有人读的时候写,以及同一时间只能有一个人在写. 读者.java: package operating.entity.readerwriter; impor ...
- 关于vue 框架与后台框架的混合使用的尝试------转载
这几天我在研究前台框架和后台框架融合的问题,进行了一些尝试; 我前台选择的是 vue,当然也可以选择 react 等其他 mvvm 框架,不过 vue 对于我来说是最熟悉的; 后台话,我选择的是 ph ...
- css3 UI元素状态伪类选择器
选择器 说明 例子/备注 E:hover 当鼠标移到元素上元素所使用的样式 :hover{}或input:[type="text"]:hover{} E:active 当元素被激活 ...
- Docker入门书籍
https://yuedu.baidu.com/ebook/d817967416fc700abb68fca1 精细讲解,入门使用极佳.
- lesson - 7 课程笔记 vim
vim :修改文件 模式: 默认进来是一般模式.i 编辑模式.esc 退出编辑 .shift+: 底行模式 参数: w: write/q:quit/! force 编辑模式: /a:光标之后插入内容 ...
- Python之禅及释义
在python shell中敲 import this会触发一个彩蛋,神奇的打印下面一段话: The Zen of Python, 即python之禅, 1999年Tim Peters大牛总结的&qu ...
- Java中使用LocalDate根据日期来计算年龄
Java中和日期直接相关的类有很多,平时最常用到的就是java.util package下面的Date和Calendar,需要用到格式的时候还会用到java.text.SimpleDateFormat ...
- 初识CSS3
1.CSS规则由两部分构成,即选择器和声明器 声明必须放在{}中并且声明可以是一条或者多条 每条声明由一个属性和值构成,属性和值用冒号分开,每条语句用英文冒号分开 注意: css的最后一条声明 ...