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 题解 本题是<挑战程序设计>一书的例题 根据树中描述 所有切割的代价 可以形成一颗二叉树 而最后的代价总和是与子节点和深 ...
随机推荐
- Android五大布局详解——LinearLayout(线性布局)
Android五大布局 本篇开始介绍Android的五大布局的知识,一个丰富的界面显示总是要有众多的控件来组成的,那么怎样才能让这些控件能够按你的想法进行摆放,从而自定义你所想要的用户界面呢?这就牵涉 ...
- YourSQLDba的共享路径备份遭遇重启问题
如果YourSQLDba设置过共享路径备份(具体参考博客YourSQLDba设置共享路径备份),有时候服务器重启后,备份就会出错,具体错误信息类似如下所示: Date 2019/9/25 ...
- C# 执行 cmd 命令, 不显示任何窗口
代码如下: 调用的命令:reg export exportPath registryKey -y Process proc = new Process(); proc.StartInfo.FileNa ...
- 23.login1(SKCTF)
没有账号?注册一个试一试~ 题目提示用SQL约束攻击,那么什么是SQL约束攻击呢? 约束攻击的原理就是注册用户名为'admin '(有多个空格)的账号,密码'*******'(密码可以自定义,符 ...
- Linux中fuser命令用法详解
描述: fuser可以显示出当前哪个程序在使用磁盘上的某个文件.挂载点.甚至网络端口,并给出程序进程的详细信息. fuser显示使用指定文件或者文件系统的进程ID. 默认情况下每个文件名后面跟一个字母 ...
- PAT 1007 Maximum Subsequence Sum 最大连续子序列和
Given a sequence of K integers { N1, N2, …, NK }. A continuous subsequence is defined to be { Ni, Ni ...
- 一起学SpringMVC之注解
概述 SpringMVC不仅提供了Xml的配置方式,还提供了注解的方式来声明一个Controller,本文属于SpringMVC的入门级内容,仅供学习分享使用,如有不足之处,还请指正. SpringM ...
- MySql入门知识(一)
概述 MySQL是一个真正多用户,多线程结构化查询语言数据库服务器.MySQL是一个关系型数据库管理系统,由瑞典MySQL AB公司开发,目前属于Oracle公司.MySQL的SQL语言是用于访问数据 ...
- Sql sever DateDiff 函数
函数: DATEDIFF(datepart,startdate,enddate) 具体实例: --相差年数 结果0 SELECT DATEDIFF(yy,'2008-12-29','2008-12-1 ...
- Java操作数据库——使用JDBC连接数据库
Java操作数据库——使用JDBC连接数据库 摘要:本文主要学习了如何使用JDBC连接数据库. 背景 数据持久化 数据持久化就是把数据保存到可掉电式存储设备中以供之后使用.大多数情况下,特别是企业级应 ...