DFS——The Mine
来源:互联网 发布:软件国产化 东方通 编辑:程序博客网 时间:2024/05/22 00:05
The Mine
The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil.
A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid.
Input
The input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise and . Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either ` *', representing the absence of oil, or ` @', representing an oil pocket.
Output
For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.
Sample Input
1 1*3 5*@*@***@***@*@*1 8@@****@*5 5****@*@@*@*@**@@@@*@@@**@0 0
Sample Output
0122
简单的DFS搜索,在搜索的过程中,注意修改遍历过的值以避免重复遍历
#include <stdio.h>char dui[100][100];char mapp[100][100];int n,m;int fx[] = {-1,-1,-1,0,0,1,1,1};int fy[] = {-1,0,1,-1,1,-1,0,1};int dfs(int i,int j){ int x; if(i >= 0 && i < m && j >= 0 && j < n) { if(mapp[i][j] == '@') { mapp[i][j] = '*'; for(x = 0;x < 8;x++) { dfs(i + fx[x],j + fy[x]); } } } return 0;}int main(){ int i,j,k; while(scanf("%d%*c%d%*c",&m,&n),m) { k = 0; for(i = 0; i<m; i++) { scanf("%s",&mapp[i]); } for(i = 0; i<m; i++) { for(j = 0; j<n; j++) { if(mapp[i][j] == '@') { k++; dfs(i,j); } } } printf("%d\n",k); } return 0;}
Miguel A. Revilla
1998-03-10
- DFS——The Mine
- 兄弟连——The Hotel California Of Mine
- hdu3316 Mine sweeping--DFS
- Down the Mine
- The Mine (bfs)
- The First Blog of Mine
- the first log of mine
- The first blog of mine...
- SDUT 2410 Mine Number DFS+回溯 (扫雷)
- DFS——The die is cast
- The number of ponds——DFS
- MINE
- MINE
- mine
- Mine
- The first Java programming of mine
- hdu 1010——Tempter of the Bone(DFS)
- hdu1010——Tempter of the Bone(DFS+剪枝)
- 如何成为一名黑客
- 黑马程序员__ ArrayList 与HashSet 去除重复函数的 区别 equals
- java中有几种方法可以实现一个线程
- 2014.4.1
- opencv 配置(vs2012)
- DFS——The Mine
- 最长单调递增子序列(网络24题,六)
- eclipse中加入tomcat6时不能添加
- ImageButton 的使用
- POJ 2499 Binary Tree(水题)
- select,poll,epoll学习
- linux系统下android真机测试,adb shell查询真机数据库
- 数字个数统计
- java算法之最大公约数及最小公倍数