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# 网络编程之简易聊天示例
还记得刚刚开始接触编程开发时,傻傻的将网站开发和网络编程混为一谈,常常因分不清楚而引为笑柄.后来勉强分清楚,又因为各种各样的协议端口之类的名词而倍感神秘,所以为了揭开网络编程的神秘面纱,本文尝试以一个 ...
- ionic项目打包+部署
环境: 1.ionic 2.angular-cli 开发 1.CTRL C + CTRL V 2.图片路径的问题 使用‘assets/xxxxx.jpg’,而不使用‘../../assets/xxx ...
- 「SAP技术」如何看Z移动类型是复制哪个标准移动类型而创建的?
[SAP技术]SAP MM 如何看一个自定义移动类型是复制哪个标准移动类型而创建的? 比如项目上有一个自定义移动类型Z59,是复制551移动类型而定义的. OMJJ配置界面里,是有一个Ref字段.如下 ...
- 小白到大神,你需要了解的 sqlite 最佳实践
本文微信公众号「AndroidTraveler」首发. 背景 本文是对一篇英文文档的翻译,原文请见文末链接. 并发数据库访问 假设你实现了自己的 SQLiteOpenHelper. public cl ...
- HIFIMAN TWS600A听感小记——测评出街Man
HIFIMAN TWS600A听感小记——测评出街Man 几年前蓝牙耳塞在大哥苹果的带领下有着掀翻小池塘的气势.蓝牙耳塞完全替代了我在通勤路上用的有线塞子,当时随便买了一副两百多元的塞子,低音轰头就算 ...
- LeetCode刷题191125
博主渣渣一枚,刷刷leetcode给自己瞅瞅,大神们由更好方法还望不吝赐教.题目及解法来自于力扣(LeetCode),传送门. 今天状态不好,划一下水. 算法: 给定一个整数数组 nums 和一个目标 ...
- 一条简单的更新语句,MySQL是如何加锁的?
看如下一条sql语句: # table T (id )) delete : MySQL在执行的过程中,是如何加锁呢? 在看下面这条语句: : 那这条语句呢?其实这其中包含太多知识点了.要回答这两个问题 ...
- 使用navicat连接只开放内网ip连接的数据库
无法通过Navicat来连接MySQL,比较常见的两种问题? 服务器上自己安装的MySQL数据库,且未开通外网登录账号 直接购买服务商的MySQL数据库不创建公网访问,只有内网访问 背景: 公司数 ...
- java 后台上传文件
java 后台上传文件 public static String uploadFile(File file, String RequestURL) throws IOException { Strin ...
- 剑指Offer-39.把数组排成最小的数(C++/Java)
题目: 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323. 分析: 将数组 ...