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. AngularJS 框架

    AngularJS 通过 指令 扩展了 HTML,且通过 表达式 绑定数据到 HTML. [Angular JS表达式]  1.Angular JS使用双{{}}绑定方式.用于将表达式的内容输出到页面 ...

  2. Day3 文件操作和函数

    一 文件操作 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 1.1打开文件读取内容 print(open("sounds","r", ...

  3. window、linux系统与linux服务器之间使用svn同步及自动部署代码的方法

    摘要: 在家用PC,在公司用办公电脑对一个项目的代码进行修改时,会遇到代码同步的问题.本文讲解了代码同步及自动部署的解决办法. 实现方法: 1.首先在linux服务器上和linux上安装svn(sud ...

  4. LInux ugo权限详解

    Linux 中的用户和组是用来控制使用者或者进程可以或者不可以使用哪些资源和硬件,是Linux权限控制最基本的方式. 用户和组可以看一下上一章的部分,先来看一下权限. 一.权限概览 在Linux下,使 ...

  5. 关于js浮点数计算精度不准确问题的解决办法

    今天在计算商品价格的时候再次遇到js浮点数计算出现误差的问题,以前就一直碰到这个问题,都是简单的使用tofixed方法进行处理一下,这对于一个程序员来说是及其不严谨的.因此在网上收集了一些处理浮点数精 ...

  6. es6函数的rest参数和拓展运算符(...)的解析

    es6的新特性对函数的功能新增加了rest参数和...的拓展运算符.这是两个什么东西呢? 先来看一个问题:如何获取一个函数除了定义的参数之外的其他参数?传统的做法是借助函数的arguments关键字来 ...

  7. Qt下 QString转char*

    Qt下面,字符串都用QString,确实给开发者提供了方便.Qt再使用第三方开源库时,由于库的类型基本上都是标准的类型,字符串遇的多的就是Char*类型 Qt再使用第三方开源库时,由于库的类型基本上都 ...

  8. java一些问题的思考

    1.思考 为什么java规定作为程序入口点的main() 方法静态的? 在java中,main()方法是java应用程序的入口方法,也就是说,程序在运行的时候,第一个执行的方法就是main()方法,这 ...

  9. 关于form表单或者Ajax向后台发送数据时,数据格式的探究

    最近在做一个资产管理系统项目,其中有一个部分是客户端向服务端发送采集到的数据的,服务端是Django写的,客户端需要用rrequests模块模拟发送请求 假设发送的数据是这样的: data = {'s ...

  10. 关于离线底图和离线shp文件的加载

    首先底图是我自己用百度地图18级别的瓦片图在armap中制作的TPK文件,shp图层是我用同样的百度地图18级别的瓦片图矢量化的,二者在arcmap中的空间参考是一致的,所以我以为在移动端加入的时候二 ...