Red and Black
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.
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)
set, your program should output a line which contains the number of
tiles he can reach from the initial tile (including
itself).
0ms)
#include
#include
#include
#define maxn 100
using namespace std;
bool visit[maxn][maxn];
char mapn[maxn][maxn];
int direction[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
int w,h,s=1;
void dfs(int w,int h,int sx,int sy)
{
int
x,y;
for(int
i=0;i<4;i++)
{
//printf("i=%d\n",i);
x=sx+direction[i][0];
y=sy+direction[i][1];//记录坐标
if(x>=1&&x<=h&&y>=1&&y<=w&&visit[x][y]==false&&mapn[x][y]=='.')
{
visit[x][y]=true;
s++;
dfs(w,h,x,y);
}
}
}
int main()
{
//freopen("in.txt", "r", stdin);
int
x,y;
while(~scanf("%d%d\n",&w,&h)&&(w||h))
{
//printf("w=%d h=%d\n",w,h);
s=1;
memset(visit,false,sizeof(visit));
for(int i=1;i<=h;i++)
{
for(int j=1;j<=w;j++)
{
scanf("%c",&mapn[i][j]);
// printf("mapn[i][j]=%c\n",mapn[i][j]);
//printf("i=%d\n",i);
if(mapn[i][j]=='@')
{
x=i;
y=j;
visit[x][y]=true;
}//记录@符出现的位置
}
scanf("\n");
}
//for(int i=1;i<=h;i++)
//{
// for(int
j=1;j<=w;j++)
// {
//
printf("%c",mapn[i][j]);
// }
// printf("\n");
//}
//printf("mapn[x][y]=%c\n",mapn[x][y]);
dfs(w,h,x,y);//开始深搜
printf("%d\n",s);
}
return
0;
}
Red and Black的更多相关文章
- 使用Red Gate Sql Data Compare 数据库同步工具进行SQL Server的两个数据库的数据比较、同步
Sql Data Compare 是比较两个数据库的数据是否相同.生成同步sql的工具. 这一款工具由Red Gate公司出品,我们熟悉的.NET Reflector就是这个公司推出的,它的SQLTo ...
- 使用Red Gate Sql Compare 数据库同步工具进行SQL Server的两个数据库的结构比较、同步
将测试版的项目同步(部署)到正式版的时候,两个数据库的结构比较与同步时,如果修改数据库的时候没有记录好修改了那些表,很难将两个数据库进行同步 RedGate Sql Compare使用简介说明: 1. ...
- 新年抢红包效果(New Year Red Packet)
新年抢红包效果(New Year Red Packet) 晓娜的文章(微信公众号:migufe) 2016即将过去,我们将迎来新的一年2017,这里小编提前祝大家新年快乐!万事如意!那我们新年最开心的 ...
- KALI Linux problems & Study Red Hat | Ubuntu
Problem When you ask some website with https head.you may met the problem secure connection failed ...
- Red Gate(SQLToolbelt)SQL Server的安装与注册(破解)
Red Gate(SQLToolbelt)是SQL Server辅佐工具 1.SQL Compare 比较和同步SQL Server数据库结构 2.SQL Data Compare 比较和同步SQL ...
- Red Hat Enterprise Server 6.0 安装Sendmail相关包
由于需要在Linux服务器(Red Hat Enterprise Linux Server release 6.0)上配置邮件服务,需要安装Sendmail包,一般Sendmail的安装有两种方式:R ...
- Red Hat Enterprise Linux 6.6安装体验
Red Hat Enterprise Linux 6.6的安装首界面有五个选项,这跟以前的Red Hat Enterprise Linux 5.x的安装界面是有一些区别的. 安装或者升级现有系统( ...
- Red Hat Enterprise Linux 各个版本以及发布日期
Red Hat Enterprise Linux 7 Release/Update General Availability Date redhat-release Errata Date* Kern ...
- linux red hat 给普通用户开启root权限
环境:虚拟机:red hat 6.5:root角色用户:普通用户:宏基笔记本:win7: 操作过程: 1.登录普通用户,进入图形界面(可以设置为启动登录进入命令行界面): 2.按Crl+ALT+F2进 ...
- 【BZOJ-1419】Red is good 概率期望DP
1419: Red is good Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 660 Solved: 257[Submit][Status][Di ...
随机推荐
- css样式引入优先级?
css中的优先级讲的有 1.选择器的优先级. 2.样式引入的优先级. 今天要研究的是样式引入的优先级.网上又很多答案都是如下的,但是真的是这样的吗,让我们来探一探究竟是如何. 四种样式的优先级别是:行 ...
- Ensemble Learning: Bootstrap aggregating (Bagging) & Boosting & Stacked generalization (Stacking)
Booststrap aggregating (有些地方译作:引导聚集),也就是通常为大家所熟知的bagging.在维基上被定义为一种提升机器学习算法稳定性和准确性的元算法,常用于统计分类和回归中. ...
- 翻译:MariaDB字符集和排序规则
html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...
- C#的"?"修饰符和"??"运算符
一. ? 可空类型修饰符 “?”用来修饰为空的值类型,在加上“?”修饰符后,值类型也可以为空了,如: public int? CommandTimeout { get; }: var prop = ...
- ThinkPHP控制器输出防止乱码小技巧
在控制器中加一句:试试看 header('content-type:text/html;charset=utf-8');
- 第4章 同步控制 Synchronization ----Interlocked Variables
同步机制的最简单类型是使用 interlocked 函数,对着标准的 32 位变量进行操作.这些函数并没有提供"等待"机能,它们只是保证对某个特定变量的存取操作是"一个一 ...
- Robberies hdu 2955 01背包
Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- Problem 2144 Shooting Game fzu
Problem 2144 Shooting Game Accept: 99 Submit: 465Time Limit: 1000 mSec Memory Limit : 32768 KB ...
- css3弹性盒模型flex快速入门与上手1
一.什么是flex? flex是css3中引入的一种布局方式,可以非常灵活高效控制元素的排列与对齐方式,大多人称之为弹性布局. 二.怎么使用flex? 任何一个容器都可以指定为flex布局 #box ...
- Python系列之正则表达式详解
Python 正则表达式模块 (re) 简介 Python 的 re 模块(Regular Expression 正则表达式)提供各种正则表达式的匹配操作,和 Perl 脚本的正则表达式功能类似,使用 ...