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. jenkins~集群分发功能和职责处理

    jenkins的多节点集群 在进行自动化部署时,你可以按着它们的项目类型去进行分别部署,这样即可以达到负载均衡,又可以达到一种职责的明确,比如像java的项目你可以使用linux服务来进行部署(拉代码 ...

  2. javascript插入before(),after()新DOM方法

    随着web的技术突飞猛进的发展.HTML5 ES6等新技术的发展,与此同时DOM等标准也在悄悄的进步,各大浏览器也在悄悄的发展适配新的属性和方法,今天我们来看看Javascript新的DOM的方法 二 ...

  3. Tree(uva 536)

    先声明,我还在学习中,这个题大部分代码借鉴的大佬的,其实这算是比较经典二叉树题了,关键在于递归建树. 代码附上: #include <iostream> #include <cstr ...

  4. #pragma编译指令

    #pragma alignment#pragma anon_struct#pragma argsused#pragma checkoption#pragma codeseg#pragma commen ...

  5. bzoj2118(加法原理)(墨墨的等式)

    题目大意:给定n个物品,每个物品有一个非负价值,问[L,R]区间内有多少价值可以被凑出来. 题意网上一大片,具体求解过程是利用了加法原理,将各个模数拥有的个数之和相加. 就是说随机取一个数a[k],那 ...

  6. 【机器学习实战】第11章 使用 Apriori 算法进行关联分析

    第 11 章 使用 Apriori 算法进行关联分析 关联分析 关联分析是一种在大规模数据集中寻找有趣关系的任务. 这些关系可以有两种形式: 频繁项集(frequent item sets): 经常出 ...

  7. php获取音悦台视频

    <?php $url=isset($_GET['url'])?trim($_GET['url']):''; $url = "http://v.yinyuetai.com/video/6 ...

  8. Python实战之正则表达式RE/re学习笔记及简单练习

    # .,\w,\s,\d,,^,$# *,+,?,{n},{n,},{n,m}# re模块用于对python的正则表达式的操作.## 字符:## . 匹配除换行符以外的任意字符# \w 匹配字母或数字 ...

  9. #云栖大会# 移动安全专场——APP加固新方向(演讲速记)

    主持人导语: 近些年来,移动APP数量呈现爆炸式的增长,黑产也从原来的PC端转移到了移动端,伴随而来的逆向攻击手段也越来越高明.在解决加固产品容易被脱壳的方案中,代码混淆技术是对抗逆向攻击最有效的方式 ...

  10. 一步一个坑 - WinDbg调试.NET程序

    引言 第一次用WinDbg来排查问题,花了很多时间踩坑,记录一下希望对后面的同学有些帮助. 客户现场软件出现偶发性的界面卡死现象一直找不出原因,就想着让客户用任务管理器生成了一个dump文件发给我,我 ...