OpenJudge/Poj 1979 Red and Black / OpenJudge 2816 红与黑
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 红与黑的更多相关文章
- POJ 1979 Red and Black (红与黑)
POJ 1979 Red and Black (红与黑) Time Limit: 1000MS Memory Limit: 30000K Description 题目描述 There is a ...
- poj 1979 Red and Black 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=1979 Description There is a rectangular room, covered with square tiles ...
- POJ 1979 Red and Black dfs 难度:0
http://poj.org/problem?id=1979 #include <cstdio> #include <cstring> using namespace std; ...
- poj 1979 Red and Black(dfs)
题目链接:http://poj.org/problem?id=1979 思路分析:使用DFS解决,与迷宫问题相似:迷宫由于搜索方向只往左或右一个方向,往上或下一个方向,不会出现重复搜索: 在该问题中往 ...
- POJ 1979 Red and Black (zoj 2165) DFS
传送门: poj:http://poj.org/problem?id=1979 zoj:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...
- HDOJ 1312 (POJ 1979) Red and Black
Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...
- poj 1979 Red and Black(dfs水题)
Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...
- POJ 1979 Red and Black (DFS)
Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...
- POJ 1979 Red and Black 四方向棋盘搜索
Red and Black Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 50913 Accepted: 27001 D ...
随机推荐
- 关于 JavaScript 数据类型判断
在 JavaScript 中,有 undefined.null.number.string.boolean 五种基本数据类型,另外,有一种复杂数据类型 object ,类似于 C# 中值类型.引用类型 ...
- Ubuntu下配置 keepalived+nginx+tomcat 负载均衡
本文力图阐述在 Ubuntu Server 环境下使用 Keepalived + Nginx + Tomcat 搭建高可用负载均衡环境的操作步骤和简约配置,这里不涉及性能调优.先说一下他们各自扮演的角 ...
- 使用air进行移动app开发常见功能和问题(二)
1. Air如何判断android.ios 平台网络连接状态? Android,使用as3原生api: if(NetworkInfo.isSupported)//只有android支持 Networ ...
- 信号之abort函数
abort函数的功能是使异常程序终止. #include <stdlib.h> void abort(void); 此函数不返回 此函数将SIGABRT信号发送给调用进程(进程不应忽略此信 ...
- jquery实现无缝滚动
//点击上一页 $('.pointLeft').click(function() { if (prevAllow) { prevAllow = false; scrollUlLeft = scroll ...
- (源)V8 Engine 编译
v8 engine编译 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !imp ...
- Tomcat启动报错org.apache.coyote.AbstractProtocol.init Failed to initialize end point associated with ProtocolHandler ["http-apr-8080"]”
1.使用netstat查看端口8080的使用情况: netstat -ano | findstr 8080 结果为: 最后一列表示使用8080端口的进程PID,如果返回结果为空则说明没有被使用. 2. ...
- 三个案例带你看懂LayoutInflater中inflate方法两个参数和三个参数的区别
关于inflate参数问题,我想很多人多多少少都了解一点,网上也有很多关于这方面介绍的文章,但是枯燥的理论或者翻译让很多小伙伴看完之后还是一脸懵逼,so,我今天想通过三个案例来让小伙伴彻底的搞清楚这个 ...
- WebKit笔记
加载网页时执行javascript代码 let mWebView = WKWebView.init(frame: self.view.bounds) self.view.addSubview(mWeb ...
- 开启nginx缓存
proxy_cache_path和proxy_cache可以开启基础缓存 proxy_cache_path:缓存路径 proxy_cache:用来启用缓存