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
题意:有一间房子,有红黑瓷砖,人只能在黑的上面活动,人在的位置就是@符号在的位置,求人能活动的瓷砖个数,人每次只能向前后左右移动;
解题思路:深搜,将走过的地板标记为1,具体过程看代码吧;
感悟:庆祝第一个独立写完的深搜代码!!!哦也!!!!
代码(G++
0ms)
#include

#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的更多相关文章

  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. Numpy中Meshgrid函数介绍及2种应用场景

    近期在好几个地方都看到meshgrid的使用,虽然之前也注意到meshgrid的用法.但总觉得印象不深刻,不是太了解meshgrid的应用场景.所以,本文将进一步介绍Numpy中meshgrid的用法 ...

  2. C++移动构造函数以及move语句简单介绍

    C++移动构造函数以及move语句简单介绍 首先看一个小例子: #include <iostream> #include <cstring> #include <cstd ...

  3. Python NLP入门教程

    本文简要介绍Python自然语言处理(NLP),使用Python的NLTK库.NLTK是Python的自然语言处理工具包,在NLP领域中,最常使用的一个Python库. 什么是NLP? 简单来说,自然 ...

  4. 【Linux笔记(001) 】-- centos7 系统目录结构与文件

    一.目录结构与用途: /boot:系统引导文件.内核 /bin:用户的基本命令 /dev:设备文件 /etc:配置文件 /home:用户目录 /root:root用户目录 /sbin:管理类的基本命令 ...

  5. 用ESP8266+android,制作自己的WIFI小车

    整体思路ESP8266作为TCP服务器,,手机作为TCP客户端,自己使用Lua直接做到了芯片里面,省了单片机,,节约成本,其实本来就是个单片机(感觉Lua开发8266真的很好,甩AT指令好几条街,,而 ...

  6. POJ 2359 Questions(约瑟夫环——数学解法)

    题目链接: http://poj.org/problem?id=2359 题意描述: 输入一个字符串 按照下面的规则,如果剩下的最后一个字符是'?',输出"Yes",如果剩下的最后 ...

  7. 关于AVALON总线动态地址对齐

    在NIOS的使用中,我们往往要用到自定义外设,然后通过AVALON交换架构和NIOSII进行通信. AVALON总线,其实是一种交换架构的协议,在自定义外设挂在AVALON总线上时,一定要注意地址对齐 ...

  8. 【转】DSCP 与IP 优先级IP优先级

    在IPv4的报文头中,TOS字段是1字节,如下图所示.根据RFC1122的定义,IP优先级(IPPrecedence)使用最高3比特(第0-2比特).+++++++++++++++++++++++++ ...

  9. 个人工作中ssd、audio python脚本总结

    1.os.system(cmd)或者os.popen(cmd)调用外部命令 cmd中需要注意特殊字符的转义功能,如: USBSTOR\DISK&VEN_GENERIC-&PROD_SD ...

  10. Python系列之反射、面向对象

    一.反射 说反射之前先介绍一下__import__方法,这个和import导入模块的另一种方式 1. import commons 2. __import__('commons') 如果是多层导入: ...