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. 基于SSM框架的简易的分页功能——包含maven项目的搭建

    新人第一次发帖,有什么不对的地方请多多指教~~ 分页这个功能经常会被使用到,我之前学习的时候找了很多资源,可都看不懂(笨死算了),最后还是在朋友帮助下做出了这个分页.我现在把我所能想到的知识 做了一个 ...

  2. scrapy, 自带命令行调用工具.

    #-*- coding:utf-8 -*- from scrapy import cmdline cmdline.execute("scrapy crawl dmoz".split ...

  3. leetcode345——Reverse Vowels of a String(C++)

    Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...

  4. git: command not found

    在使用git时,出现“git: command not found”的情况,于是使用yum安装: [root@localhost ~]# yum install -y gitLoaded plugin ...

  5. LeetCode OJ学习

    一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...

  6. varchar(n),nvarchar(n) 长度、性能、及所占空间分析 nvarchar(64) nvarchar(128) nvarchar(256)(转)

    varchar(n),nvarchar(n) 中的n怎么解释: nvarchar(n)最多能存n个字符,不区分中英文. varchar(n)最多能存n个字节,一个中文是两个字节. 所占空间: nvar ...

  7. css动画怎么写:3个属性实现

    3个属性:transition,animation,transform 实现步骤: 1.css定位 2.rgba设置颜色透明度 3.转换+动画 transform+animation 4.动画平滑过渡 ...

  8. yii2源码学习笔记(十九)

    view剩余代码 /** * @return string|boolean the view file currently being rendered. False if no view file ...

  9. MySQL - “Timeout error occurred trying to start MySQL Daemon”解决方法

    前几天,网站出现Many connections的问题,进入mysql,show full processlist发现有154个进程,晕....直接service mysqld restart 但是不 ...

  10. python实现雅虎拍卖后台自动回复卖家消息

    前些时间,公司让做一个自动回复卖家信息的程序,现在总结下(用python实现的) 1.登陆雅虎拍卖后台手动获取cookie文件 #coding=utf-8 import sqlite3 import ...