地址 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 题解《挑战程序设计竞赛》的更多相关文章

  1. 【网络流#9】POJ 2135 Farm Tour 最小费用流 - 《挑战程序设计竞赛》例题

    [题意]给出一张无向图,从1开始到n,求两条没有公共边的最短路,使得路程总和最小 每条边的权值设为费用,最大流量设为1,然后就是从源点到汇点流量为2的最小费用流. 因为是规定了流量,新建一个源点和一个 ...

  2. 【网络流#7】POJ 3281 Dining 最大流 - 《挑战程序设计竞赛》例题

    不使用二分图匹配,使用最大流即可,设源点S与汇点T,S->食物->牛->牛->饮料->T,每条边流量为1,因为流过牛的最大流量是1,所以将牛拆成两个点. 前向星,Dini ...

  3. 【网络流#6】POJ 3041 Asteroids 二分图最大匹配 - 《挑战程序设计竞赛》例题

    学习网络流中ing...作为初学者练习是不可少的~~~构图方法因为书上很详细了,所以就简单说一说 把光束作为图的顶点,小行星当做连接顶点的边,建图,由于 最小顶点覆盖 等于 二分图最大匹配 ,因此求二 ...

  4. POJ 2386 Lake Counting 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=2386 <挑战程序设计竞赛>习题 题目描述Description Due to recent rains, water has ...

  5. POJ 1979 Red and Black (红与黑)

    POJ 1979 Red and Black (红与黑) Time Limit: 1000MS    Memory Limit: 30000K Description 题目描述 There is a ...

  6. 《挑战程序设计竞赛》2.3 动态规划-优化递推 POJ1742 3046 3181

    POJ1742 http://poj.org/problem?id=1742 题意 有n种面额的硬币,面额个数分别为Ai.Ci,求最多能搭配出几种不超过m的金额? 思路 据说这是传说中的男人8题呢,对 ...

  7. Aizu 2249Road Construction 单源最短路变形《挑战程序设计竞赛》模板题

    King Mercer is the king of ACM kingdom. There are one capital and some cities in his kingdom. Amazin ...

  8. 挑战程序设计竞赛》P345 观看计划

                                                 <挑战程序设计竞赛>P345 观看计划 题意:一周一共有M个单位的时间.一共有N部动画在每周si时 ...

  9. poj 3253 Fence Repair 贪心 最小堆 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=3253 题解 本题是<挑战程序设计>一书的例题 根据树中描述 所有切割的代价 可以形成一颗二叉树 而最后的代价总和是与子节点和深 ...

随机推荐

  1. Python高级特性——切片(Slice)

    摘录廖雪峰网站 定义一个list: L = ['haha','xixi','hehe','heihei','gaga'] 取其前三个元素: >>> L[0],L[1],L[2] (' ...

  2. git常用情景和基础命令

    git常用情景和基础命令 将项目克隆到本地 --xxx是git的地址 git clone xxxx 或者初始化git(github提供滴) --新建一个readme.md文件 echo "# ...

  3. PHP 日期之间所有日期

    /** * 获取起止日期之间所有日期 * @param $sdate * @param $edate * @return array */ function get_dates($sdate, $ed ...

  4. 轻松定位CPU飙高问题

    以下四步轻松定位CPU飙高问题: ①top pid 查看cpu耗CPU进程 ②top -Hp pid 查看该进程所有线程的运行情况,找到占用 CPU 过高的线程 pid ③ printf %x pid ...

  5. application context not configured for this file于spring框架使用中的原因

    spring配置文件中时常会出现这个提示,翻译过来大概意思就是没有配置该文件到项目中 于是进入到Project Structure中查看 可以很明显的看到下面有个感叹号,大概意思是下面的文件没有匹配 ...

  6. 基于django的个人博客网站建立(五)

    基于django的个人博客网站建立(五) 前言 网站效果可点击这里访问 之前鸽了两天,今天继续再写点 主要内容 今天加了个展示照片的功能,就叫他生活记录吧 先建表 class Record(model ...

  7. TestNG入门教程-12-Java代码执行testng.xml和失败后重跑

    前面我们都在IDEA上右键testng.xml文件来运行testng用例,这个在编写测试用例过程是 可以这么做,但是,如果测试用例写完了,也是这么做吗?有没有什么方法,例如自动化去实现.测试脚本维护后 ...

  8. vue介绍以及相关概念理解大全

    1.什么是vue 以官网的解释来说,vue是渐进式javascript框架.Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架.与其它大型框架不同的是,Vue 被设计 ...

  9. github用户注册和仓库创建

    访问github官网:https://github.com/,点击注册进入注册页面 输入用户名,电子邮箱和密码后点击下一步 邮箱验证,收到github的验证邮箱,打开后点击验证 选择个人计划 创建仓库 ...

  10. Python三级菜单作业实现

    数据结构: menu = { '北京':{ '海淀':{ '五道口':{ 'soho':{}, '网易':{}, 'google':{} }, '中关村':{ '爱奇艺':{}, '汽车之家':{}, ...