OpenJudge/Poj 1979 Red and Black / OpenJudge 2816 红与黑

时间:2022-09-26 11:35:01

1.链接地址:

http://bailian.openjudge.cn/practice/1979

http://poj.org/problem?id=1979

2.题目:

总时间限制:
1000ms
内存限制:
65536kB
描述
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.

输入
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 20.

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.

输出
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).
样例输入
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
样例输出
45
59
6
13
来源
Japan 2004 Domestic

3.思路:

4.代码:

 #include <iostream>
#include <cstdio> using namespace std; int f(char **arr,const int i,const int j,int w,int h)
{
int res = ;
if(arr[i][j] == '.')
{
res += ;
arr[i][j] = '#';
if(i > ) res += f(arr,i - ,j,w,h);
if(i < h - ) res += f(arr,i + ,j,w,h);
if(j > ) res += f(arr,i,j - ,w,h);
if(j < w - ) res += f(arr,i,j + ,w,h);
}
return res;
} int main()
{
//freopen("C:\\Users\\wuzhihui\\Desktop\\input.txt","r",stdin); int i,j; int w,h;
//char ch;
while(cin>>w>>h)
{
if(w == && h == ) break;
//cin>>ch; char **arr = new char*[h];
for(i = ; i < h; ++i) arr[i] = new char[w]; for(i = ; i < h; ++i)
{
for(j = ; j < w; ++j)
{
cin>>arr[i][j];
}
//cin>>ch;
} for(i = ; i < h; ++i)
{
for(j = ; j < w; ++j)
{
if(arr[i][j] == '@')
{
arr[i][j] = '.';
cout << f(arr,i,j,w,h) << endl;
break;
}
}
if(j < w) break;
} for(i = ; i < h; ++i) delete [] arr[i];
delete [] arr;
} return ;
}

OpenJudge/Poj 1979 Red and Black / OpenJudge 2816 红与黑的更多相关文章

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

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

  2. poj 1979 Red and Black 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=1979 Description There is a rectangular room, covered with square tiles ...

  3. POJ 1979 Red and Black dfs 难度&colon;0

    http://poj.org/problem?id=1979 #include <cstdio> #include <cstring> using namespace std; ...

  4. poj 1979 Red and Black&lpar;dfs&rpar;

    题目链接:http://poj.org/problem?id=1979 思路分析:使用DFS解决,与迷宫问题相似:迷宫由于搜索方向只往左或右一个方向,往上或下一个方向,不会出现重复搜索: 在该问题中往 ...

  5. POJ 1979 Red and Black &lpar;zoj 2165&rpar; DFS

    传送门: poj:http://poj.org/problem?id=1979 zoj:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...

  6. HDOJ 1312 &lpar;POJ 1979&rpar; Red and Black

    Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...

  7. poj 1979 Red and Black(dfs水题)

    Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...

  8. POJ 1979 Red and Black (DFS)

    Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...

  9. POJ 1979 Red and Black 四方向棋盘搜索

    Red and Black Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 50913   Accepted: 27001 D ...

随机推荐

  1. java&period;lang&period;IllegalStateException&colon; Recursive entry to executePendingTransactions

    [解决办法]: 将getFragmentManager改为getChildFragmentManager即可

  2. 通过profile 用maven命令打不同配置的变量包

    profiles定义如下 <profiles>         <profile>             <id>local</id>         ...

  3. 1213 How Many Tables(简单并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 简单并查集,统计单独成树的数量. 代码: #include <stdio.h> #i ...

  4. Codeforces Round &num;363 &lpar;Div&period; 2&rpar; B&period; One Bomb (水题)

    B. One Bomb time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...

  5. Ant自动构建

    Ant+jenkins+tomcat <project name="buildWar" default="clean"> <property ...

  6. android SlidingmMenu的入门介绍

    最近了解了SlidingMenu控件的使用,之前手机qq等软件都采用了这种控件,所以还是很有必要学些这个控件的使用方法. 这个控件是基于github的一个开源项目. 地址是: https://gith ...

  7. Markdown语法指南

    1.背景 个人比较喜欢用Markdonw写东西,比如写博客随笔,写有道云笔记等,但有的时候会突然忘记某个具体语法怎么写了,如插入图片.插入链接.表格等,那干脆把这个语法简单地总结一下,也方便日后快速查 ...

  8. Stencil 基础

    Stencil 一个轻量化,渐进式编译器,注意,不是框架. 使用 TypeScript 进行所有操作,这是一个门槛,有一定技术门槛要求. PS:个人强烈推荐所有的前端同学都学习,或至少了解这个超集语言 ...

  9. Day6 Python常用的模块

    一.logging模块 一.日志级别 critical=50 error=40 waring=30 info=20 debug=10 notset=0 二.默认的日志级别是waring(30),默认的 ...

  10. Intel daal数据预处理

    https://software.intel.com/en-us/daal-programming-guide-datasource-featureextraction-py # file: data ...