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 ...
随机推荐
- iOS开发-布局基础
今天我学习了有关布局的知识,以下是我整理出来的需要注意的,也是我个人认为比较重要的细节. 一.自动约束 在我们设置好自动约束布局之后,有时会出现拖动滑动条,左边的Label会跟着放大缩小的问题. 这时 ...
- java--静态的应用(工具类)
ArrayTool.java文件 /** 静态的应用. 每一个应用程序中都有公行动的功能,可以将这些功能进行抽取,独立封装以便复用 由于ArrayTool中并没有封装特有数据,而且数组的每一个方法也没 ...
- Java连接MYSQL【转载】
这篇文章主要以MySQL为例讲下Java如何连接到数据库的. 当然,首先要安装有JDK(一般是JDK1.5.X).然后安装MySQL,这些都比较简单,具体过程就不说了.配置好这两个环境后,下载JDBC ...
- Windows下记事本编辑的Shell脚本放到Linux下执行出错,格式问题(/bin/bash^M: bad interpreter: 没有那个文件或目录)
错误: /bin/bash^M: bad interpreter: 没有那个文件或目录 解决方案: 运行脚本时出现了这样一个错误,打开之后并没有找到所谓的^M,查了之后才知道原来是文件格式的问题,也就 ...
- Struts1中ActionForward的技巧介绍
ActionForward是做什么的?他是用来封装转发和重定向路径的. 在struts- config.xml中<forward name="error" path=&quo ...
- dialog统一标准调用方法(内部记录)
更新base-config.js 对话框统一为三种形式(如后期需要再添加其他方式) //对话框--确定取消 //dialogOkFun:确定函数 dialogCancelFun:取消函数 functi ...
- careercup-排序和查找 11.7
11.7 有个马戏团正在设计叠罗汉的表演节目,一个人要站在另一人的肩膀上.处于实际和美观的考虑,在上面的人要比下面的人矮一点.轻一点.已知马戏团每个人的高度和重量,请编写代码计算叠罗汉最多能叠几个人. ...
- C#_delegate - 值参数和引用参数
值参数不能加,引用参数可以. 引用参数是共享的 using System; using System.Collections.Generic; using System.Linq; using Sys ...
- ERROR:_OBJC_CLASS_$_ADBannerView
http://stackoverflow.com/questions/4127489/iads-integration-with-cocos2d You are getting that error ...
- Distributed locks with Redis--官方
原文:http://redis.io/topics/distlock Distributed locks with Redis Distributed locks are a very useful ...