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. 【框架学习与探究之定时器--Quartz.Net 】

    声明 本文欢迎转载,原文地址:http://www.cnblogs.com/DjlNet/p/7572174.html 前言 这里相信大部分玩家之前现在都应该有过使用定时器的时候或者需求,例如什么定时 ...

  2. 翻译连载 | 第 9 章:递归(下)-《JavaScript轻量级函数式编程》 |《你不知道的JS》姊妹篇

    原文地址:Functional-Light-JS 原文作者:Kyle Simpson-<You-Dont-Know-JS>作者 关于译者:这是一个流淌着沪江血液的纯粹工程:认真,是 HTM ...

  3. oracle数据中记录被另一个用户锁住

    原因:PL/SQL里面执行语句执行了很久都没有结果,于是中断执行,于是就直接在上面改字段,在点打钩(记入改变)的时候提示,记录被另一个用户锁住. 解决方法: 第一步:(只是用于查看哪些表被锁住,真正有 ...

  4. 第4章 同步控制 Synchronization ----信号量(Semaphore)

    许多文件中都会提到 semaphores(信号量),因为在电脑科学中它是最具历史的同步机制.它可以让你陷入理论的泥淖之中,教授们则喜欢问你一些有关于信号量的疑难杂 症.你可能不容易找到一些关于 sem ...

  5. P1035

    P1035 时间限制: 1 Sec  内存限制: 128 MB提交: 87  解决: 36[提交][状态][讨论版] 题目描述 给出一张n*n(n< =100)的国际象棋棋盘,其中被删除了一些点 ...

  6. 前端框架——AngularJS

      前  言 AngularJS是一款为了克服HTML在构建应用上的不足而设计的优秀的前端JS框架.AngularJS有着诸多特性,最为核心的是:MVC.模块化.自动化双向数据绑定.语义化标签.依赖注 ...

  7. C#之基础

    引子:C#是.NET平台所支持的多种语言中的一门编程语言,它是一门面向对象编程语言.面向对象语言的三大基本特性是:封装.继承.多态.学过C#的人肯定都知道,C#和Java极其相似.我已经学过C语言,现 ...

  8. 在CentOS 7 上安装docker

    Docker CE Install yum-utils, which provides the yum-config-manager utility: $ sudo yum install -y yu ...

  9. SpringMVC中redirect跳转后如何保存Model中的数据?

    @RequestMapping(value = "delete-user", method = RequestMethod.POST) public String deleteUs ...

  10. javascript(js)创建对象的模式与继承的几种方式

    1.js创建对象的几种方式 工厂模式 为什么会产生工厂模式,原因是使用同一个接口创建很多对象,会产生大量的重复代码,为了解决这个问题,产生了工厂模式. function createPerson(na ...