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 ...
随机推荐
- Swif基本语法以及与OC比较三
(未 经 博 主 同 意,不 得 转 载 !) ------------------------华丽分割线----------------------- // // main.swift ...
- SAP ABAP 处理字符串串串串串串串串(详细)
关于ABAP中处理字符串的方法,非常详细,学习过程中总结一下分享给大家,,, ABAP/4 提供多个处理类型 C 即字符串 的数据对象的关键字. 处理字符串 的方法有: 1.拆分字符串split 2. ...
- 获利能力分析COPA的BAPI:BAPI_COPAACTUALS_POSTCOSTDATA 通过增强返回凭证号
用这个BAPI:BAPI_COPAACTUALS_POSTCOSTDATA 记录销售成本的时候,发现不能return生产的co凭证号,利用内存应该是最好的方法. SCN上的帖子:https://blo ...
- CentOS下系统时间同步和时区的修改和设置(用的这个)
一.修正时区 rm -rf /etc/localtime #删除当前默认时区www.kwx.gd ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localt ...
- C++ 外部调用private方法
1.思考,对于C++,能不能在外部调用私有方法? 2.在Java中,子类继承不能缩小父类成员的访问权限.因为在Java中,继承只是表示Is-A关系,因此,父类提供的接口,子类必须承诺仍然提供,不能缩小 ...
- 【Android】Activity遮罩效果的实现
Activity文件 package com.app.test02; import android.app.Activity; import android.graphics.Color; impor ...
- [Javascript + lodash] sortBy and sortedIndex
sortBy: var collection = ['John', 'Petteri', 'Antti', 'Joonas', 'Zhentian']; var sorted = _.sortBy(c ...
- sphinx中过滤的简单介绍
1.过滤字符串为空的一些记录的话,可以在sql_query中直接加上where来限制,如: sql_query = \ SELECT a.location_id as id,a.location_ ...
- ibdata文件增大的原因
http://blog.itpub.net/22664653/viewspace-1994016/
- 第十一章 Function类型
在ECMAScript中,Function(函数)类型实际上是对象.每个函数也是Function类型的实例,而且都与其它引用类型一样具有属性和方法.由于是函数对象,因此函数名实际上也是一个指向函数对象 ...