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 ...
随机推荐
- IE无法获得cookie,ie不支持cookie的解决办法,火狐支持
发现用自己的电脑 IE7.0总是无法正常登录,别的电脑都可以. 每次登录后又被重定向回了登录页面. 可换成Firefox和google chrome 却一切OK,后来还把浏览器升级到IE8.0 问题依 ...
- iOS 多人共享开发证书
当多人开发时,如果已经申请了几个开发者证书和发布者证书,苹果就不允许再创建了,页面添加的地方被灰化了,所以不可能每个人都建一个开发证书,这时候需要共用一个证书了.(其实一般在我们的证书界面中应该只有一 ...
- JAVA HashMap 解析
1.简介(其实是HashMap注释的大致翻译) 本文基于JDK1.8,与JDK1.7中的HashMap有一些区别,看官注意区别. HashMap实现了Map接口,提供了高效的Key-Value访问.H ...
- JSP入门 导出文件
1.图片校验码 <img src="captcha.jpg" /> web.xml配置 <servlet> <servlet-name& ...
- pygame_第一个窗口程序
####可以使用python自带的IDLE交互式开发,也可以借助其他的编辑器,我这里采用的pycharm编辑器 1.导入我们所需要的模块 import pygame,sys --导入我们需要的模块 ...
- electron入门心得
前言 前端开发桌面程序这个概念已经出现有一段时间了,这项技术也已经走向成熟,Github上nw和光electron的star就差不多有10w颗星了,github也衍生出了很多开源的桌面项目俨然成了一个 ...
- poj3070矩阵快速幂
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7752 Accepted: 5501 Descrip ...
- 宝藏(树形DP)
这道题目是十分考验思维的,n^2应该还是比较好想的,主要是如何转移根的问题.转移根,在我看来应该是树形dp最难的一部分了, 一般学会如何转移根,也就差不多考验通吃树形dp了. 下面转一转大佬链接: ...
- HDU1698 线段树(区间更新区间查询)
In the game of DotA, Pudge's meat hook is actually the most horrible thing for most of the heroes. T ...
- ERROR! MySQL server PID file could not be found!的解决方法
启动MySQL服务 [root@test vhosts]# /etc/init.d/mysqld restart 提示错误: ERROR! MySQL server PID file could no ...