DFS——hdu1241Oil Deposits

一、题目回顾

题目链接:Oil Deposits

二、解题思路

  • DFS经典
  • 注意的就是要向八个方向搜索,搜索完一处,将该处变成石头。

三、代码

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 using namespace std;
 6 const int maxn = 1e4+10;
 7 #define INF 0x3f3f3f3f
 8 char a[105][105];
 9 int m,n,k,ans;
10
11 void dfs(int x,int y)        //当前坐标
12 {
13     if(x<1 || x>m || y<1 || y>n || a[x][y]!=‘@‘){
14         return;
15     }
16     a[x][y] = ‘*‘;        //若(x,y)为石油,则标记已访问
17
18     //向当前坐标点的八个方向搜索
19     dfs(x-1,y-1);
20     dfs(x,y-1);
21     dfs(x+1,y-1);
22     dfs(x-1,y);
23     dfs(x+1,y);
24     dfs(x-1,y+1);
25     dfs(x,y+1);
26     dfs(x+1,y+1);
27 }
28 int main()
29 {
30     while(cin>>m>>n && !(m==0 && n==0)){
31         for(int i=1;i<=m;i++){
32             getchar();
33             for(int j=1;j<=n;j++){
34                 scanf("%c",&a[i][j]);
35             }
36         }
37         getchar();
38         ans = 0;
39         for(int i=1;i<=m;i++){
40             for(int j=1;j<=n;j++){
41                 if(a[i][j] == ‘@‘){
42                     dfs(i,j);
43                     ans++;
44                 }
45             }
46         }
47         printf("%d\n",ans);
48     }
49     return 0;
50 }
时间: 2024-11-09 09:57:31

DFS——hdu1241Oil Deposits的相关文章

HDU-1241Oil Deposits

Description GeoSurvComp地质调查公司负责探测地下石油储藏. GeoSurvComp现在在一块矩形区域探测石油,并把这个大区域分成了很多小块.他们通过专业设备,来分析每个小块中是否蕴藏石油.如果这些蕴藏石油 的小方格相邻,那么他们被认为是同一油藏的一部分.在这块矩形区域,可能有很多油藏.你的任务是确定有多少不同的油藏. Input 输入可能有多个矩形区域(即可能有多组测试).每个矩形区域的起始行包含m和n,表示行和列的数量,1<=n,m<=100,如果m =0表示输入的结束

DFS PKU 1562

简单的DFS Oil Deposits Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12801   Accepted: 6998 Description The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectan

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(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

Oil Deposits(dfs)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 15291    Accepted Submission(s): 8787 Problem Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep

UVA - 572 - Oil Deposits (图的DFS!)

UVA - 572 Oil Deposits Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description  Oil Deposits  The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works

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.

HDU 1241 Oil Deposits --- 入门DFS

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1241 /* HDU 1241 Oil Deposits --- 入门DFS */ #include <cstdio> int m, n; //n行m列 char mapp[105][105]; /* 将和i,j同处于一个连通块的字符标记出来 */ void dfs(int i, int j){ if (i < 0 || j < 0 || i >= n || j >= m