poj 1979 Red and Black 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=1979
Description There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles. Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than . 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 For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
Sample Input ....#.
.....#
......
......
......
......
......
#@...#
.#..#. .#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
........... ..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#.. ..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#.. Sample Output
大概意思就是 @是一个人的起点 可以在.的地板砖上移动 但是不能移动到#的地板上。求能够达到的最多地板
算是基础的dfs题目 没有剪枝 就是遍历
代码
#include <iostream> using namespace std; int n,m; const int N = ; char graph[N][N]; char visit[N][N];
int ret = ; int addx[] = { ,-,, };
int addy[] = { ,,,- }; void Dfs(int x, int y) {
if (x < || x >= n || y < || y >= m) return; if (visit[x][y] == || graph[x][y] == '#') return; visit[x][y] = ;
if (graph[x][y] == '.') {
//cout << "x = " << x << ". y = " << y << endl;
ret++;
} for (int i = ; i < ; i++) {
int newx = x + addx[i];
int newy = y + addy[i];
Dfs(newx, newy);
} return;
} int main()
{
while (cin >> m >> n) {
if (m == || n == ) break;
int x, y;
ret = ;
memset(visit, , N*N); for (int i = ; i < n; i++) {
for (int j = ; j < m; j++) {
cin >> graph[i][j];
if (graph[i][j] == '@') {
x = i; y = j;
ret++;
}
}
} Dfs(x, y); cout << ret << endl;
} //system("pause"); return ;
}
poj 1979 Red and Black 题解《挑战程序设计竞赛》的更多相关文章
- 【网络流#9】POJ 2135 Farm Tour 最小费用流 - 《挑战程序设计竞赛》例题
[题意]给出一张无向图,从1开始到n,求两条没有公共边的最短路,使得路程总和最小 每条边的权值设为费用,最大流量设为1,然后就是从源点到汇点流量为2的最小费用流. 因为是规定了流量,新建一个源点和一个 ...
- 【网络流#7】POJ 3281 Dining 最大流 - 《挑战程序设计竞赛》例题
不使用二分图匹配,使用最大流即可,设源点S与汇点T,S->食物->牛->牛->饮料->T,每条边流量为1,因为流过牛的最大流量是1,所以将牛拆成两个点. 前向星,Dini ...
- 【网络流#6】POJ 3041 Asteroids 二分图最大匹配 - 《挑战程序设计竞赛》例题
学习网络流中ing...作为初学者练习是不可少的~~~构图方法因为书上很详细了,所以就简单说一说 把光束作为图的顶点,小行星当做连接顶点的边,建图,由于 最小顶点覆盖 等于 二分图最大匹配 ,因此求二 ...
- POJ 2386 Lake Counting 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=2386 <挑战程序设计竞赛>习题 题目描述Description Due to recent rains, water has ...
- POJ 1979 Red and Black (红与黑)
POJ 1979 Red and Black (红与黑) Time Limit: 1000MS Memory Limit: 30000K Description 题目描述 There is a ...
- 《挑战程序设计竞赛》2.3 动态规划-优化递推 POJ1742 3046 3181
POJ1742 http://poj.org/problem?id=1742 题意 有n种面额的硬币,面额个数分别为Ai.Ci,求最多能搭配出几种不超过m的金额? 思路 据说这是传说中的男人8题呢,对 ...
- Aizu 2249Road Construction 单源最短路变形《挑战程序设计竞赛》模板题
King Mercer is the king of ACM kingdom. There are one capital and some cities in his kingdom. Amazin ...
- 挑战程序设计竞赛》P345 观看计划
<挑战程序设计竞赛>P345 观看计划 题意:一周一共有M个单位的时间.一共有N部动画在每周si时 ...
- poj 3253 Fence Repair 贪心 最小堆 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=3253 题解 本题是<挑战程序设计>一书的例题 根据树中描述 所有切割的代价 可以形成一颗二叉树 而最后的代价总和是与子节点和深 ...
随机推荐
- C# 中的基本数值类型
在之前的文章中(地址:https://www.vinanysoft.com/c-sharp-basics/introducing/),以 HelloWorld 程序为基础,介绍 C# 语言.它的结构. ...
- typeORM 多对多关系不同情况的处理
本文以RBAC权限管理中的用户和角色举例,两个实体存在多对多的关系,一个用户拥有多个角色,一个角色属于多个用户.typeorm的中文文档没有对自定义中间表的说明,发现英文有相关说明,但示例代码貌似有问 ...
- laravel开发大型电商网站之异常设计思路分析
令人讨厌的异常 提起异常,大家都很反感,当信心满满的写完一段代码,刷新页面发现上面写着大大的 Exception 是最心烦的时候了.模块给领导演示的时候,如果报了异常,也是最让人崩溃的时候了. 在一般 ...
- 松软科技web课堂:JavaScript 布尔(逻辑)
JavaScript 布尔(逻辑)代表两个值之一:true 或 false. 布尔值 通常,在编程中,您会需要只能有两个值之一的数据类型,比如 YES / NO ON / OFF TRUE / FAL ...
- vue--CRUD
1. Create this.$http.post("http://localhost:3000/users",newCustomer).then(function (respon ...
- 深入理解JavaScript程序设计
今天没事情回顾了一下我在去年4-6月份学习JavaScript程序设计的笔记.发现书到用时方恨少,感觉自己学的还不够深,准备抽时间啃一下<JavaScript高级程序设计>,同时深入了解一 ...
- IDEA项目更改项目名
点击File,如图:
- 023.[转] 尚硅谷_Maven笔记
- Django django-cors-headers实现防跨域
安装 pip install django-cors-headers 注册应用 INSTALLED_APPS = ( ... 'corsheaders', ... ) 中间层设置 MIDDLEWARE ...
- Linux下MySQL数据库的my.cnf配置文件,解决中文乱码问题
系统 CentOS 7.7 MySQL - 5.7.28文件放置目录:/etc/文件权限:644解决问题:存储中文数据乱码 # For advice on how to change settings ...