UVa572 - Oil Deposits

解题思路:好久没写搜索了,练练手,陶冶情操。不多说,直接贴代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 const int maxn = 105;
 6 int dir[8][2] = {-1, -1, -1, 0, -1, 1, 0, -1,
 7                  0, 1, 1, -1, 1, 0, 1, 1};
 8 char gg[maxn][maxn];
 9 int m, n;
10
11 void DFS(int x, int y, int id)
12 {
13     if(x < 1 || x > m || y < 1 || y > n || gg[x][y] == ‘*‘) return ;
14     gg[x][y] = ‘*‘;
15     for(int i = 0; i < 8; i++)
16     {
17         int xx = x + dir[i][0];
18         int yy = y + dir[i][1];
19
20         if(xx < 1 || xx > m || yy < 1 || yy > n || gg[xx][yy] == ‘*‘) continue;
21         DFS(xx, yy, id);
22     }
23     return ;
24 }
25 int main()
26 {
27     int cnt;
28     while(~scanf("%d %d", &m, &n) && m)
29     {
30         memset(gg, ‘#‘, sizeof(gg));
31         for(int i = 1; i <= m; i++)
32         for(int j = 1; j <= n; j++) scanf(" %c", &gg[i][j]);
33
34         cnt = 0;
35         for(int i = 1; i <= m; i++)
36         for(int j = 1; j <= n; j++)
37         if(gg[i][j] == ‘@‘) DFS(i, j, ++cnt);
38         printf("%d\n", cnt);
39     }
40     return 0;
41 }

时间: 2024-10-12 21:33:13

UVa572 - Oil Deposits的相关文章

UVA572 Oil Deposits【DFS】

Oil Deposits 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

UVa572 Oil Deposits (DFS求连通块)

链接:http://acm.hust.edu.cn/vjudge/problem/19435分析:DFS求图的连通块.每次从图中找出一个未访问过的‘@’开始递归遍历它周围的‘@’格子,将访问到的‘@’标上id存在idx数组中,dfs的次数就是连通块的个数.DFS遍历用递归实现,BFS用队列实现.DFS和BFS实现种子填充:https://zh.wikipedia.org/wiki/Flood_fill 1 #include <cstdio> 2 #include <cstring>

油田(Oil Deposits)-用DFS求连通块

[本博文非博主原创,均摘自:刘汝佳<算法竞赛入门经典>(第2版) 6.4 图] [程序代码根据书中思路,非独立实现] 例题6-12 油田(Oil Deposits,UVa572) 输入一个m行n列的字符矩阵,统计字符"@"组成多少个八连块.如果两个字符"@"所在的格子相邻(横.纵或者对角线方向),就说它们属于一个八连块.例如,下图中有两个八连块. 一.分析 1.1 整体思路 图也有DFS和BFS遍历.由于DFS更容易编写,一般用DFS查找连通块:从每个&

UVa 572 - Oil Deposits【图DFS】

Oil Deposits 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

Oil Deposits hdu-1241 DFS

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 analyze

HDU 1241 Oil Deposits(石油储藏)

p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-size: 10.5000pt } h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: center; font-family: 宋体; color: rgb(26,92,200); font-weight: bold; fo

HDU_1241 Oil Deposits(DFS深搜)

Problem Description 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 pl

暑假集训(1)第七弹 -----Oil Deposits(Poj1562)

Description 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

HDU 搜索练习 Oil Deposits

Oil Deposits Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 61 Accepted Submission(s) : 25 Problem Description The GeoSurvComp geologic survey company is responsible for detecting underground oil