博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU1312:Red and Black(DFS)
阅读量:4316 次
发布时间:2019-06-06

本文共 1943 字,大约阅读时间需要 6 分钟。

版权声明:本文为博主原创文章,未经博主同意不得转载。

https://blog.csdn.net/u012325397/article/details/26818317

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
DFS水题,带来了久违一次AC。太感动了~
#include
#include
char map[25][25];int vis[25][25],cnt,w,h;void dfs(int x,int y){ if(x<0||y<0||x>=w||y>=h) return ; if(map[y][x]=='#') return ; if(vis[y][x]) return; if(!vis[y][x]&&(map[y][x]=='.'||map[y][x]=='@')) { vis[y][x]=1; cnt++; } dfs(x+1,y); dfs(x-1,y); dfs(x,y+1); dfs(x,y-1);}int main(){ int x,y; while (scanf("%d%d",&w,&h),w&&h) { getchar(); for(int i=0;i

转载于:https://www.cnblogs.com/xfgnongmin/p/10799653.html

你可能感兴趣的文章
MyBatis总结八:缓存介绍(一级缓存,二级缓存)
查看>>
div+css教程网站建设门户网站和电子商务网站CSS样式表
查看>>
[LeetCode][JavaScript]Candy
查看>>
Mybatis分页插件
查看>>
sk_buff Structure
查看>>
oracle的级联更新、删除
查看>>
多浏览器开发需要注意的问题之一
查看>>
Maven配置
查看>>
HttpServletRequest /HttpServletResponse
查看>>
SAM4E单片机之旅——24、使用DSP库求向量数量积
查看>>
从远程库克隆库
查看>>
codeforces Unusual Product
查看>>
hdu4348 - To the moon 可持久化线段树 区间修改 离线处理
查看>>
springMVC中一个class中的多个方法
查看>>
Linux系统安装出错后出现grub rescue的修复方法
查看>>
线段树模板整理
查看>>
[教程][6月4日更新]VMware 8.02虚拟机安装MAC lion 10.7.3教程 附送原版提取镜像InstallESD.iso!...
查看>>
[iOS问题归总]iPhone上传项目遇到的问题
查看>>
Python天天美味(总) --转
查看>>
Spring Framework tutorial
查看>>