算法:深搜

题意:就是让你找到一共可以移动多少次,每次只能移到黑色格子上,

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

代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <iomanip>
using namespace std;
char ch[25][25];
int k,n,m;
void dfs(int x,int y,int &k)
{
ch[x][y]='#';
if(x-1>=0&&x-1<m&&y>=0&&y<n&&ch[x-1][y]=='.')
{k++;dfs(x-1,y,k);}
if(x+1<m&&x+1>=0&&y>=0&&y<n&&ch[x+1][y]=='.')
{k++;dfs(x+1,y,k);}
if(y-1>=0&&y-1<n&&x>=0&&x<m&&ch[x][y-1]=='.')
{k++;dfs(x,y-1,k);}
if(y+1<n&&y+1>=0&&x>=0&&x<m&&ch[x][y+1]=='.')
{k++;dfs(x,y+1,k);}
else return ;
}
int main()
{
int i,j,q,p;
while(cin>>n>>m&&n&&m)
{ k=1;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>ch[i][j];
if(ch[i][j]=='@')
{
p=i;q=j;
}
} }
dfs(p,q,k);
cout<<k<<endl;
}
return 0;
}

hdu Red and Black的更多相关文章

  1. HDU 1312 Red and Black (dfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Oth ...

  2. HDU 1312 Red and Black --- 入门搜索 BFS解法

    HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...

  3. HDU 1312 Red and Black --- 入门搜索 DFS解法

    HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...

  4. HDU 1312:Red and Black(DFS搜索)

      HDU 1312:Red and Black Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & ...

  5. HDU 1312 Red and Black(DFS,板子题,详解,零基础教你代码实现DFS)

    Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  6. HDU 1312 Red and Black(最简单也是最经典的搜索)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Oth ...

  7. HDU 1312 Red and Black(bfs,dfs均可,个人倾向bfs)

    题目代号:HDU 1312 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/100 ...

  8. hdu 1312 Red and Black

    Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...

  9. hdu 1312:Red and Black(DFS搜索,入门题)

    Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

随机推荐

  1. 你好,C++(20).4.2.2 表达并列条件选择的switch语句:如果……如果……如果……

    4.2.2  表达并列条件选择的switch语句:如果……如果……如果…… 在现实世界中,还有这样一类特殊的条件选择: 如果明天是晴天,我就穿T恤: 如果明天是阴天,我就穿衬衣: 如果明天是雨天,我就 ...

  2. u盘禁用

    Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\services\USBSTOR]" ...

  3. 分享8款精美的jQuery图片播放插件

    本文将和大家一起分享8款精美的jQuery图片播放插件,每一款插件均有演示和源码下载,有兴趣的朋友可以下载使用和研究.废话不多说了,直接上这些插件. 1.3D轮播相册 这款3D相册插件利用了HTML5 ...

  4. JqueryUI插件网络连接

    operamasks_UI官网 http://ui.operamasks.org/website/homepage.html EasyUI官网 http://www.jeasyui.com/index ...

  5. 如何在symfony 控制器里面创建soap web service

    通过一些工具将一个控制器设置成一个soap服务将会非常简单.首先,你必须安装了php soap扩展.由于php soap扩展现在不能生成wsdl,你要么自己从头开始创建要模使用第三方生成器. php中 ...

  6. dedecms 织梦ping服务插件 最新破解可用版

    dedecms 织梦ping服务插件 最新破解可用版  ping_gbk.xml <module> <baseinfo> name=ping服务 team=井哥 time=20 ...

  7. Python一路走来 RabbitMQ

    一:介绍:(induction) Rabbitmq 是一个消息中间件.他的思想就是:接收和发送消息.你可以把它想成一个邮政局.当你把你的邮件发送到邮箱的,首先你需要确认的是:邮政员先生能把你的邮件发送 ...

  8. 基类方法的反隐藏 反private 秘籍

    class GoodStudent:private Mentor,private Student { public : using Mentor::GetInfo;   ///------------ ...

  9. JNI学习&使用过程中的错误

    Part 1 Ubuntu下JNI的简单使用: http://blog.csdn.net/fengqiaoyebo2008/article/details/6210499 Part 2 在eclips ...

  10. 关于popupwindow的两种实现方式

    http://104zz.iteye.com/blog/1685389 android PopupWindow实现从底部弹出或滑出选择菜单或窗口 本实例弹出窗口主要是继承PopupWindow类来实现 ...