题目描述

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.

输入

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)
The end of the input is indicated by a line consisting of two zeros.

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

解题思路:深搜的方法解决,题目意思就是从@开始找.并与@连通,碰到#等于碰到了墙,题目很简单,@可以向四个方向上、下、左、右走,所以 用四个坐标标记出来,然后,再一一遍历,递归调用寻找,用一个30*30的数组标识此点有没有走过,避免走重复

程序代码:
#include <cstdio>
#include <cstring>
using namespace std;
int n,m,cot;
char map[30][30];
int to[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};void dfs(int i,int j)
{cot++;map[i][j] = '#';for(int k = 0; k<4; k++){int x = i+to[k][0];int y = j+to[k][1];if(x<n && y<m && x>=0 && y>=0 && map[x][y] == '.')dfs(x,y);}return;
}int main()
{int i,j,fi,fj;while(~scanf("%d%d%*c",&m,&n)&&m&&n){for(i = 0; i<n; i++){for(j = 0; j<m; j++){scanf("%c",&map[i][j]);if(map[i][j] == '@'){fi = i;fj = j;}}getchar();}cot= 0;dfs(fi,fj);printf("%d\n",cot);}return 0;
}

转载于:https://www.cnblogs.com/www-cnxcy-com/p/4678523.html

数据结构——HDU1312:Red and Black(DFS)相关推荐

  1. HDU1312 Red and Black(dfs+连通性问题)

    这有一间铺满方形瓷砖的长方形客房. 每块瓷砖的颜色是红色或者黑色. 一个人站在一块黑色瓷砖上, 他可以从这块瓷砖移动到相邻(即,上下左右)的四块瓷砖中的一块. 但是他只能移动到黑色瓷砖上,而不能移动到 ...

  2. 数据结构与算法—图论之dfs、bfs(深度优先搜索、宽度优先搜索)

    文章目录 前言 邻接矩阵和邻接表 深度优先搜索(dfs) 宽度(广度)优先搜索(bfs) 总结与比较 前言 在有向图和无向图中,如果节点之间无权值或者权值相等,那么dfs和bfs时常出现在日常算法中. ...

  3. 基本数据结构(图: 基本结构,DFS,prim算法, kruskal算法)

    #include <iostream> using namespace std; //约定: //1. 图是由很多节点(VERTEX)构成的, 因此图结构是由一个VERTEX的链表构成的, ...

  4. 数据结构 旅游规划(Dijkstra+Dfs)

    题目: 有了一张自驾旅游路线图,你会知道城市间的高速公路长度.以及该公路要收取的过路费.现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路径.如果有若干条路径都是最短的,那么需要 ...

  5. POJ 1979 Red and Black DFS

    简单DFS 注意边界就行了. // #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstd ...

  6. 【python3数据结构】图Graph及DFS(深度优先搜索)BFS(广度优先搜索)

    Graph 和 Vertex 构建基于邻接列表: # -*- coding: utf-8 -*- # @Date : 2019/12/1 # @File : AdjListGraph.pyimport ...

  7. 数据结构-图的深度优先遍历(DFS)和广度优先遍历(BFS)算法分析

    https://www.cnblogs.com/qzhc/p/10291430.html 最后一个广度优先有错误,H不指向E,只有G指向E,所以顺序应该是ABCFDHGE

  8. ICPC程序设计题解书籍系列之五:吴永辉:《数据结构编程实验》(第2版)

    第1章 简单计算 UVALive2362 POJ1004 HDU1064 ZOJ1048 Financial Management[数学+水题] - 海岛Blog - CSDN博客 POJ1552 H ...

  9. 数据结构实验之图论四:迷宫探索_迷宫搜索类的双向bfs问题(例题详解)

    前言 文章若有疏忽还请指正! 更多精彩还请关注公众号:bigsai 头条号:一直码农一直爽 在搜索问题中,以迷宫问题最具有代表性,无论是八皇后的回溯问题,还是dfs找出口,bfs找最短次数等等题目的问 ...

最新文章

  1. css实现垂直居中定位
  2. 手机百度首页的localStorage的使用实例
  3. SpringMVC搭建+实例
  4. java 范围搜寻要怎么弄_搜索范围
  5. TypeError: 'NoneType' object is not subscriptable
  6. 利用python从网页查找数据_利用Python模拟淘宝的搜索过程并对数据进行可视化分析...
  7. poj 3694 Network (无向图的 割边 lca )
  8. 嗖嗖移动业务大厅(源码下载+注释全 值得收藏)
  9. 解决Linux服务器时差问题
  10. 服务器如何与智能家居通讯协议,智能家居通信协议优缺点比较
  11. maven仓库的优先级,profile的优先级
  12. android 语音自动分句,进行音频断句的自动拆分方法及系统与流程
  13. android 高仿ios时间选择器,仿ios时间选择
  14. Java中的空指针异常
  15. 高通骁龙855发布,5G大幕拉开,新一轮手机大战在即
  16. win10定时关机怎么设置
  17. 经纬度与UTM(Universal Transverse Mercator Projector:通用横轴卡墨托投影)的坐标变换代码
  18. 解决Sublime出现中文乱码的情况
  19. 前端测试框架—jest基本使用
  20. uniapp中h5网页微信公众号授权

热门文章

  1. spark 写tidb_优秀的数据工程师,怎么用Spark在TiDB上做OLAP分析
  2. python中itertools groupby函数是干嘛的_Python-如何使用itertools.groupby()?
  3. JZOJ 3739. 【TJOI2014】匹配
  4. java鼠标经过时变色_鼠标经过时单元格变色
  5. python调试神器_介绍一款调试Python的神器
  6. 一元流量参数为null_为什么牛逼的程序员都不用 “ ! = null quot; 做判空?
  7. LOJ #6669 Nauuo and Binary Tree (交互题、树链剖分)
  8. 四位共阳极数码管显示函数_【项目8-任务10-小组13】利用四位数码管实现动态扫描显示...
  9. Git复习(十三)之git revert用法及与git reset区别
  10. [杂记]对RSA算法的数学原理的一点思考