Red and Black

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6865    Accepted Submission(s): 4359

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) 

 
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
 
思路:确实是简单dfs,初学者一定要多想
 
#include<stdio.h>
#include<string.h>
char str[][];
int
temp[][];
int
cnt,h,w;
void
dfs(int x,int y)
{

cnt ++;
temp[x][y] =;
if
(!temp[x+][y]&& x+ < w)
dfs(x+,y);
if
(!temp[x-][y] && x- > -)
dfs(x-,y);
if
(!temp[x][y+] && y+ < h)
dfs(x,y+);
if
(!temp[x][y-] && y- > -)
dfs(x,y-);
}
int main()
{

int
i,j,a,b;
while
(~scanf("%d%d",&h,&w)&&h+w)
{

cnt =;
for
(i =;i < w;i ++)
{

scanf("%s",str[i]);
for
(j =;j < h;j ++)
{

if
(str[i][j] == '#')
temp[i][j] =;
if
(str[i][j] == '.')
temp[i][j] =;
if
(str[i][j] == '@')
{

a = i;
b = j;
}
}
}

dfs(a,b);
printf("%d\n",cnt);
}

return
;
}

Red and Black的更多相关文章

  1. 使用Red Gate Sql Data Compare 数据库同步工具进行SQL Server的两个数据库的数据比较、同步

    Sql Data Compare 是比较两个数据库的数据是否相同.生成同步sql的工具. 这一款工具由Red Gate公司出品,我们熟悉的.NET Reflector就是这个公司推出的,它的SQLTo ...

  2. 使用Red Gate Sql Compare 数据库同步工具进行SQL Server的两个数据库的结构比较、同步

    将测试版的项目同步(部署)到正式版的时候,两个数据库的结构比较与同步时,如果修改数据库的时候没有记录好修改了那些表,很难将两个数据库进行同步 RedGate Sql Compare使用简介说明: 1. ...

  3. 新年抢红包效果(New Year Red Packet)

    新年抢红包效果(New Year Red Packet) 晓娜的文章(微信公众号:migufe) 2016即将过去,我们将迎来新的一年2017,这里小编提前祝大家新年快乐!万事如意!那我们新年最开心的 ...

  4. KALI Linux problems & Study Red Hat | Ubuntu

    Problem When you ask some website with https head.you may met the problem  secure connection failed ...

  5. Red Gate(SQLToolbelt)SQL Server的安装与注册(破解)

    Red Gate(SQLToolbelt)是SQL Server辅佐工具 1.SQL Compare 比较和同步SQL Server数据库结构 2.SQL Data Compare 比较和同步SQL ...

  6. Red Hat Enterprise Server 6.0 安装Sendmail相关包

    由于需要在Linux服务器(Red Hat Enterprise Linux Server release 6.0)上配置邮件服务,需要安装Sendmail包,一般Sendmail的安装有两种方式:R ...

  7. Red Hat Enterprise Linux 6.6安装体验

    Red Hat Enterprise Linux 6.6的安装首界面有五个选项,这跟以前的Red Hat Enterprise Linux 5.x的安装界面是有一些区别的.   安装或者升级现有系统( ...

  8. Red Hat Enterprise Linux 各个版本以及发布日期

    Red Hat Enterprise Linux 7 Release/Update General Availability Date redhat-release Errata Date* Kern ...

  9. linux red hat 给普通用户开启root权限

    环境:虚拟机:red hat 6.5:root角色用户:普通用户:宏基笔记本:win7: 操作过程: 1.登录普通用户,进入图形界面(可以设置为启动登录进入命令行界面): 2.按Crl+ALT+F2进 ...

  10. 【BZOJ-1419】Red is good 概率期望DP

    1419: Red is good Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 660  Solved: 257[Submit][Status][Di ...

随机推荐

  1. ZIP压缩文件夹中上个月的文件,并将备份文件拷贝到服务器

    遍历文件夹的子文件夹下的所有文件,将上个月的文件集中到一起,然互压缩,并copy到服务器的映射磁盘. static void Main(string[] args) { //原始文件存放的位置 Dir ...

  2. redis基本数据类型【3】-List类型

    列表类型采用双向链表实现的,借助列表类型redis还可以作为队列使用,一个列表类型key最多能容纳2^32-1个元素 1.向列表两端增加元素 #向左边增加元素,返回增加后列表的长度 lpush key ...

  3. 04_XML_03_XMLDTD约束与校验

    [什么是XML约束] 在XML技术里,可以编写一个文档来约束一个XML文档的书写规范,称之为XML约束. 常见的XML约束技术有:XML DTD和XML Schema DTD(Document Typ ...

  4. Linux常用(持续更新)

    1. scp ./bcec_computernode_check.sh  root@10.254.3.1:/tmp 2. # uname -a # cat /proc/version # cat /e ...

  5. 网卡添加VLAN TAG

    #modprobe 8021q 用命令 lsmod | grep 8021q 来检查 以配置网卡eth0为例,添加vlan号:1002 ================================ ...

  6. Hive - 建表和加载数据指令小结 以及使用Load data指令的注意事项

    类似Mysql的数据库概念: hive> CREATE DATABASE cui; hive> USE cui; 创建表: CREATE TABLE test( first STRING, ...

  7. H-UI的前端处理验证,判断是否已经存在,比较健全的模板,可以自己添加一些校验

    <input type="text" class="input-text" value="${detail.supportingname }&q ...

  8. angularjs制作的iframe后台管理页切换页面

    <code> <!DOCTYPE html><html lang="zh" ng-app><head> <meta chars ...

  9. Echart..js插件渲染报错 data.length<1?

    问题 getJSON提交 返回数据正常,在传入参数进行序列化,渲染报表时报错 option.data.length < 1. 分析  1.可能情况一: . 可自己明明是getJSON()把渲染放 ...

  10. NodeJS加MongoDB应用入门

    OS:Windows 7 1.下载安装MongoDB:http://www.mongodb.org/downloads 2.下载安装NodeJS:http://nodejs.org/ 3.运行Mong ...