POJ 1979 Red and Black (红与黑)
POJ 1979 Red and Black (红与黑)
Time Limit: 1000MS Memory Limit: 30000K
|
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 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. |
多组测试用例。每组数组开头有两个正整数W和H;W与H分别表示 x- 与 y- 方向上瓷砖的数量。W和W均不超过20。 还有H行数据,每行包含W个字符。每个字符表示各色瓷砖如下。 ‘.’- 一块黑砖 ‘#’- 一块红砖 ‘@’- 一个黑砖上的人(一组数据一个人) 输入以一行两个零为结束。 |
|
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 - 输出样例 |
6 9 |
45 |
【题解】
数据不大,DFS可解。
【代码 C++】
#include <cstdio>
#include <cstring>
char data[][];
int sum;
void DFS(int y, int x){
if (data[y][x] == '#') return;
++sum; data[y][x] = '#';
DFS(y + , x); DFS(y - , x);
DFS(y, x + ); DFS(y, x - );
}
int main(){
int w, h, i, j, stY, stX;
while (~scanf("%d%d ", &w, &h)){
if (w + h == ) break;
memset(data, '#', sizeof(data));
for (i = ; i <= h; ++i){
gets(&data[i][]);
for (j = ; j <= w; ++j) if (data[i][j] == '@') stY = i, stX = j;
data[i][j] = '#';
}
sum = ; DFS(stY, stX);
printf("%d\n", sum);
}
return ;
}
POJ 1979 Red and Black (红与黑)的更多相关文章
- OpenJudge/Poj 1979 Red and Black / OpenJudge 2816 红与黑
1.链接地址: http://bailian.openjudge.cn/practice/1979 http://poj.org/problem?id=1979 2.题目: 总时间限制: 1000ms ...
- 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 ...
随机推荐
- python核心编程学习记录之基础知识
虽然对python的基础知识有所了解,但是为了更深入的学习,要对python的各种经典书籍进行学习 第一章介绍python的优缺点,略过 第二章介绍python起步,第三章介绍python基础,仅记录 ...
- Linux内存管理原理【转】
转自:http://www.cnblogs.com/zhaoyl/p/3695517.html 本文以32位机器为准,串讲一些内存管理的知识点. 1. 虚拟地址.物理地址.逻辑地址.线性地址 虚拟地址 ...
- 5.1JavaScript精华
9.使用Promises Promises,是Javascript表现item的一种方式.它执行异步工作,在未来的某个时间点完成.遇到最多的promises,是使用Ajax请求.浏览器在后台发起HTT ...
- Maven之Nexus构建企业级Maven仓库
什么是Nexus? Nexus是Maven仓库管理器,用来搭建一个本地仓库服务器,这样做的好处是便于管理,节省网络资源,速度快,还有一个非常有用的功能就是可以通过项目的SNAPSHOT版本管理,来进行 ...
- NSDictionary to jsonString || 对象转json格式
-(NSString*)DataTOjsonString:(id)object { NSString *jsonString = nil; NSError *error; NSData *jsonDa ...
- iOS身份证的正则验证
在ios项目的开发中可能很多地方都需要用到身份证校验,一般在开发的时候很多人都是直接百度去网上荡相关的正则表达式和校验代码,但是网上疯狂粘贴复制的校验代码本身也可能并不准确,可能会有风险,比如2013 ...
- HDU 2236:无题II(二分搜索+二分匹配)
http://acm.hdu.edu.cn/showproblem.php?pid=2236 题意:中文题意. 思路:先找出最大和最小值,然后二分差值,对于每一个差值从下界开始枚举判断能不能二分匹配. ...
- 【转】介绍设置Session失效的几种方法
转载地址:http://developer.51cto.com/art/201106/269493.htm Session对象是HttpSessionState的一个实例.该类为当前用户会话提供信息, ...
- Linux下启动Oracle
切换到oracle用户 su - oracle 启动监听 lsnrctl start 确认是不是想要启动的oracle实例 echo $ORACLE_SID 如果不是,切换SID ora ...
- 文本去重之MinHash算法
1.概述 跟SimHash一样,MinHash也是LSH的一种,可以用来快速估算两个集合的相似度.MinHash由Andrei Broder提出,最初用于在搜索引擎中检测重复网页.它也可以应用 ...