题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312

Red and Black

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15186    Accepted Submission(s): 9401

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<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
using namespace std;
#define N 25
char mp[N][N];
int go[][] = {{,},{-,},{,-},{,}}; int n,m;
bool ck(int x, int y){
if(x<n&&x>=&&y<m&&y>=) return true;
else return false;
}
bool vis[N][N];
void dfs(int x, int y, int &sum)
{
vis[x][y] = ;
bool fl = ;
for(int i = ; i < ; i++){
int xx = x + go[i][];
int yy = y + go[i][];
if(ck(xx,yy)&&!vis[xx][yy]&&mp[xx][yy]=='.'){
fl = ;
sum = sum+;
dfs(xx,yy,sum);
}
}
if(fl==) return;
}
int main()
{
while(~scanf("%d%d",&m,&n))
{
int x, y;
if(n==&&m==) break;
getchar();
for(int i = ; i < n; i++){
for(int j = ; j < m; j++){
scanf("%c",&mp[i][j]);
if(mp[i][j]=='@'){ x = i; y = j; }
}
getchar();
}
memset(vis,,sizeof(vis));
int sum = ;
dfs(x,y,sum);
printf("%d\n",sum+);
}
return ;
}

Red and Black(dfs水)的更多相关文章

  1. poj1564 Sum It Up dfs水题

    题目描述: Description Given a specified total t and a list of n integers, find all distinct sums using n ...

  2. poj 1979 Red and Black(dfs水题)

    Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...

  3. 【wikioi】1229 数字游戏(dfs+水题)

    http://wikioi.com/problem/1229/ 赤裸裸的水题啊. 一开始我认为不用用完全部的牌,以为爆搜会tle.. 可是我想多了. 将所有状态全部求出,排序后暴力判断即可. (水题有 ...

  4. HDU 1312 Red and Black DFS(深度优先搜索) 和 BFS(广度优先搜索)

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

  5. HDU1312——Red and Black(DFS)

    Red and Black Problem DescriptionThere is a rectangular room, covered with square tiles. Each tile i ...

  6. 数据结构——HDU1312:Red and Black(DFS)

    题目描述 There is a rectangular room, covered with square tiles. Each tile is colored either red or blac ...

  7. Red and Black(水)

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

  8. HDU 1312 Red and Black (DFS)

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

  9. 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 ...

随机推荐

  1. RabbitMQ阻塞读取时数据时,关闭channel引起的问题和解决方案

    项目场景: 最近在项目中使用了RabbitMq,其中有一个功能必须能随时切断RabbitMq的coumser.第一时间写出来的代码如下: 伪代码: while(flag){ QueueingConsu ...

  2. xamarin android viewpager的用法

    1.什么是ViewPager 通过手势滑动可以完成view的切换,一般是用来app的引导页或则实现图片轮播,类似网页上的banner轮播. Adnroid 3.0后引入的一个UI控件,在xamarin ...

  3. ArcGIS API for JavaScript 4.2学习笔记[28] 可视域分析【使用Geoprocessor类】

    想知道可视域分析是什么,就得知道可视域是什么 我们站在某个地方,原地不动转一圈能看到的所有事物就叫可视域.当然平地就没什么所谓的可视域. 如果在山区呢?可视范围就会被山体挡住了.这个分析对军事上有十分 ...

  4. css弹性盒子新旧兼容

    前言:本篇随笔是对弹性盒子有了解的人来写的这篇文章,具体属性产生的效果这里不做说明,基础的东西去查文档.这里只是总结. 时至今日,css3的flex弹性盒子在移动端基本上都是支持的,但不排除有些些低版 ...

  5. SecureCRT连接本地的Vmware虚拟机(CentOS)时提示连接超时“Connection timed out”

    测试了一下,直接在Vmware的VM里面可以ping通宿主机. 但是宿主机无法ping通VM. 后面发现是本地的网络设置里面的vmware的NAT的网卡设置了手工填写地址和DNS. 修改为自动获取.问 ...

  6. UNIX域协议(无名套接字)

    关于什么是UNIX域套接字可以参考:http://www.cnblogs.com/xcywt/p/8185597.html这里主要介绍非命名的UNIX域套接字的用法.1.socketpair函数先看m ...

  7. IDLE3.6.3 Mac版不支持中文输入解决办法

    最近安装了IDLE 3.6.3版本 但是在IDLE中要输入中文注释时发现虽然输入法切换到了中文,但输入的还是英文.然后我在IDLE外试了下,输入中文没问题,于是就确认应该是IDLE的问题. 网上查询到 ...

  8. amaze UI 笔记 - CSS

    导航添加依据 http://amazeui.org/css/  下面内容属学习笔记,如有理解偏差和错误请留言相告,感谢!* =(官网这块写的很详细) 一.基本样式 1.统一样式 说明了为什么使用Nor ...

  9. qt中建立图片资源文件

    qt中如果你要添加图片资源文件我们需要执行以下步骤: (1)先找好一张图片,这里就不多说了,网上资源很多. (2)把我们找好的文件统一放到一个文件夹,然后拉到工程文件所在的文件夹下 (3)在qt中新建 ...

  10. Python玩转硬件:TPYBoard-Micropython开发板大盘点

    学习python能做什么? 可以快速搭建一个漂亮的网站 可以写爬虫 实现微信机器人助手 可以实现很多自动化的任务 -- IEEE发布2017年编程语言排行榜:Python更是高居首位,那么Python ...