dfs学习总结
来源:互联网 发布:python读音 编辑:程序博客网 时间:2024/05/22 07:47
今天做到了dfs的训练,感觉和bfs有相似之处,接下来用一道题来总结一下方法,可类比bfs。
上题:
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.
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)
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
4559613
#include<stdio.h>#include<string.h>char point[25][25];bool flag[25][25]; //flag对应着我们需要研究的点point,用来标记是否曾经到过int dx[4]={1,-1,0,0};int dy[4]={0,0,-1,1};int count;void dfs(int x0,int y0,int r,int c){ for(int i=0;i<4;i++) //for循环用来探索所有邻接点 { int tempx=x0+dx[i],tempy=y0+dy[i]; if(tempx<c&&tempx>=0&&tempy<r&&tempy>=0&&flag[tempy][tempx]==false&&point[tempy][tempx]=='.') { count++; flag[tempy][tempx]=true; //满足条件且未标记的标记上 dfs(tempx,tempy,r,c); //通过递归来实现顺藤摸瓜的效果 } } return ;}void make_set(){ for(int i=0;i<25;i++) for(int j=0;j<25;j++) flag[i][j]=false; return ;}int main(){ int c,r,x0,y0; while(1) { count=1; scanf("%d%d",&c,&r); getchar(); if(c==0&&r==0) break; for(int i=0;i<r;i++) { for(int j=0;j<c;j++) { point[i][j]=getchar(); if(point[i][j]=='@') { x0=j; y0=i; } } getchar(); } make_set(); dfs(x0,y0,r,c); printf("%d\n",count); } return 0;}
与bfs的区别:bfs是通过队列来进行逐层探索,而dfs则是沿着一个路径探索下去一直到底,到底后再返回沿其他路径探索,就和顺藤摸瓜差不多,大体上两者达到的效果基本一致(特殊情况效果有区别),两者均有缺点,bfs在编写代码时较麻烦,用到队列,一般要使用结构体,而dfs则是由于其使用递归调用,大数据易超时。
==================================================================================================================================
赶紧跑回来加一句:最短路径用bfs!!
0 0
- dfs学习总结
- DFS学习归纳总结
- DFS和BFS学习总结
- dfs总结
- DFS 总结
- dfs总结
- dfs总结
- DFS总结
- DFS学习
- 7.29 DFS总结
- DFS,BFS算法总结
- BFS与DFS总结
- 总结之bfs,dfs
- dfs简单总结
- Dijkstra+DFS模板总结
- DFS序总结
- DFS BFS 搜索总结
- DFS-lintcode解法总结
- 9.5 触摸和手势:事件的传递的机制--演示代码
- UVA10250 The Other Two Trees
- uva 10250(数学)
- linux下文件打包和压缩
- [vim 小技巧] 在vim里保存只读文件
- dfs学习总结
- 链表(三)——链表删除冗余结点&插入结点到有序链表
- 堆和栈内存扩展方向问题
- hdu 2846 Repository
- Android 开发学习记录(3)---- httpclient使用(二)
- 派生类的构造函数
- couchbase & sync-gateway
- hdu 1393 Weird Clock
- 黑马程序员 关于一个ARC引发的问题