HDOJ 1312 (POJ 1979) Red and Black
Problem 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.
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
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
Sample Output
45
59
6
13
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char d[30][30];
bool vis[30][30];
int str[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
int n,m,s;
using namespace std;
void dfs(int x,int y){
for(int i=0;i<4;i++){
int xx=x+str[i][0];
int yy=y+str[i][1];
if(xx>=0&&yy>=0&&xx<m&&yy<n&&d[xx][yy]=='.'&&vis[xx][yy]==0){
s++;
// printf("%d\n",s);
vis[xx][yy]=1;
dfs(xx,yy);
}
if(i==3)
return ;
}
}
int main()
{
int a,b;
while(~scanf("%d%d",&n,&m)&&(n||m)){
for(int i=0;i<m;i++){
scanf("%s",&d[i]);
for(int j=0;j<n;j++){
if(d[i][j]=='@'){
a=i;
b=j;
}
}
}
memset(vis,0,sizeof(vis));
s=1;
d[a][b]='#';
dfs(a,b);
printf("%d\n",s);
}
return 0;
}
HDOJ 1312 (POJ 1979) Red and Black的更多相关文章
- POJ 1979 Red and Black (红与黑)
POJ 1979 Red and Black (红与黑) Time Limit: 1000MS Memory Limit: 30000K Description 题目描述 There is a ...
- 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 ...
- 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开发的协议(protocol)和代理(delegate)
协议和代理对于一个新手来说确实不讨好理解,也有很多的iOS开发的老手对此是懂非懂的.网上的很多博文只是讲了怎么使用,并没有说的很明白.下面我谈一下我的理解. 1.你要先搞明白,协议和代理为什么会出现, ...
- React学习笔记(一) 基础知识
现在最热门的前端框架有AngularJS.React.Bootstrap等.自从接触了ReactJS,ReactJs的虚拟DOM(Virtual DOM)和组件化的开发深深的吸引了我. React的基 ...
- Java 设计模式_复合模式(2016-08-31)
一.什么是复合模式? 在形式上,复合模式确实是多个模式的组合,但满足了这一条并不一定是复合模式,注意它的定义: 将多个模式结合起来形成一个“框架”,以解决一般性问题 一提到“框架”,可能最容易联想到的 ...
- EDM推送
一.需求描述: 日前,做了一个发送客户账单的功能,邮件模板采用自定义,生成vm文件,保存至redis, 采用jodd-mail发送邮件,查询用户账单数据,账单明细,缓存加载模板并渲 ...
- mysql主要应用场景 转载
前言 据说目前MySQL用户已经达千万级别了,其中不乏企业级用户.可以说是目前最为流行的开源数据库管理系统软件了.任何产品都不可能是万能的,也不可能适用于所有的应用场景.那么MySQL到底在什么场景下 ...
- python细节
1.assert 断言语句,可判断一句话真假,若为假会抛出AssertionError. eg. assert 1==1 assert 1==2则AssertionError 2.单引号双引号 ...
- PHP实现简易的模板引擎
PHP实现简易的模板引擎 1.MVC简介 MVC 是一种使用 MVC(Model View Controller 模型-视图-控制器)设计创建 Web 应用程序的模式(详情自己百度): 1. Mode ...
- JavaScript学习心得(八)
Cookie是Netscape发明的技术,是动态网站必不可少的部分,用于浏览器请求Web页面的超文本传输协议是一种无状态的协议. 两种方法维护状态:使用会话(session)(使用服务器技术实现,数据 ...
- [python]随机数
import random()testlist = [1,3,4,5]a,b = 1,5random().random()() 生成0至1之间的随机浮点数,结果大于等于0.0,小于1.0random. ...
- linux创建交换分区
一.SWAP 概述 当系统的物理内存不够用的时候,就需要将物理内存中的一部分空间释放出来,以供当前运行的程序使用.那些被释放的空间可能来自一些很长时间没有什么操作的程序,这些被释放的空间被临时保存到S ...