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 ...
随机推荐
- jdk版本切换
安装1.6/1.7/1.8版本的jdk 保存jdk的安装目录下的文件 卸载所有jdk 将jdk各个版本拷贝到一个文件夹下 配置环境变量 因为安装之后系统会有注册表之类的文件,单纯的修改环境是不会修改成 ...
- Piggy Back_KEY
Piggy Back (piggyback.pas/c/cpp) [问题描述] Bessie 和她的姐姐 Elsie 在不同的田块吃草,晚上她们都返回牛棚休息.作为聪明的奶牛,她们想设计一个方案使得步 ...
- Linux 安装Anaconda 4.4.0
安装步骤参考了官网的说明:https://docs.anaconda.com/anaconda/install/linux.html 具体步骤如下: 1.在官网下载地址 https://www.an ...
- ThinkPHP控制器输出防止乱码小技巧
在控制器中加一句:试试看 header('content-type:text/html;charset=utf-8');
- Python文件复制(txt文件)
功能:这个py脚本是把一个txt文件(源文件)复制到另一个txt文件(目的文件)里面 算法思路: 程序首先判断源文件(用exists函数判断)和目的文件是否存在,如果不存在则输出文件路径不存在,如果存 ...
- Spring详解(七)------事务管理
PS:本篇博客源码下载链接:http://pan.baidu.com/s/1mi3NhX2 密码:3io2 1.事务介绍 事务(Transaction),一般是指要做的或所做的事情.在计算机术语中是指 ...
- SpringMVC中redirect跳转后如何保存Model中的数据?
@RequestMapping(value = "delete-user", method = RequestMethod.POST) public String deleteUs ...
- Ghost文件封装说明
一.先列举目前Windows系统安装方式: 1.光盘安装 1.1 使用可刻录光驱将系统ISO文件刻录至DVD光盘,刻录工具比较多,QA目前使用Ultra ISO. 1.2 安装电脑从DVD光盘启动,无 ...
- Python系列之正则表达式详解
Python 正则表达式模块 (re) 简介 Python 的 re 模块(Regular Expression 正则表达式)提供各种正则表达式的匹配操作,和 Perl 脚本的正则表达式功能类似,使用 ...
- this到底指向哪里
this指向调用它的对象 首先要明确,this指向调用方,谁调用,this指向谁. 直接调用 举个栗子: var test = 'window' ; function testThis () { va ...