hdu 1241Oil Deposits(BFS)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241

Oil Deposits

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13137    Accepted Submission(s):
7611

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 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 1 <= m <= 100 and 1 <= n <= 100. 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

0

1

2

2

题目大意:

这个题目就是要查找油田的个数,比如说第二组数据

3 5

*@*@*

**@**             注明:"@"这个与他的几个方向都是@都是可以相连的。所以称为一块油田。输出1。

*@*@*

这种就很容易想到搜索。这里的一个技巧就是起点就从@开始,其次就是找到@就使其变成*;不过找过的还是要标记为1,否则就会重复,这样不连接的还可以继续搜~

东西还是要反复的咀嚼,不然就会很生,忘得差不多0.0

详见代码。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <queue>
 4 #include <cstring>
 5
 6 using namespace std;
 7
 8 int dir[8][2]= {0,1,0,-1,1,0,-1,0,1,1,1,-1,-1,1,-1,-1};
 9 char map[110][110];
10 int a,b,vis[110][110],k;
11
12 struct node
13 {
14     int x,y;
15     int t;
16 } s,ss;
17
18 queue<node>q,qq;
19 int bfs()
20 {
21     while (!q.empty())
22     {
23         s=q.front();
24         q.pop();
25         //vis[s.x][s.y]=1;
26         for (int i=0; i<8; i++)
27         {
28             int x=s.x+dir[i][0];
29             int y=s.y+dir[i][1];
30             //int t=s.t+1;
31             if (x>=0&&x<a&&y>=0&&y<b)
32             {
33                 if (!vis[x][y]&&map[x][y]==‘@‘)
34                 {
35                     ss.x=x;
36                     ss.y=y;
37                     map[ss.x][ss.y]=‘*‘;
38                     //vis[x][y]=1;
39                     q.push(ss);
40                 }
41
42             }
43         }
44     }
45 }
46
47 int main ()
48 {
49     while (scanf("%d%d",&a,&b)!=EOF)
50     {
51         memset(vis,0,sizeof(vis));
52         if (a==0&&b==0)
53             break;
54         for (int i=0; i<a; i++)
55         {
56             getchar();
57             for (int j=0; j<b; j++)
58             {
59                 scanf("%c",&map[i][j]);
60             }
61         }
62         //int k=0;
63         for (int i=k=0; i<a; i++)
64         {
65             for (int j=0; j<b; j++)
66             {
67                 if (map[i][j]==‘@‘)
68                 {
69                     k++;
70                     s.x=i;
71                     s.y=j;
72                     map[s.x][s.y]=‘*‘;
73                     vis[s.x][s.y]=1;
74                     q.push(s);
75                     bfs();
76                 }
77             }
78         }
79         printf ("%d\n",k);
80     }
81     return 0;
82 }
时间: 2024-08-25 23:51:17

hdu 1241Oil Deposits(BFS)的相关文章

HDU 1241Oil 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

hdu 1241Oil Deposits(dfs模板)

题目链接—— http://acm.hdu.edu.cn/showproblem.php?pid=1241 首先给出一个n*m的字符矩阵,‘*’表示空地,‘@’表示油井.问在这个矩阵中有多少组油井区? 每个点周围的8个点都可以与之相连. 从左上角的点开始向后枚举然后dfs搜索就可以了.记得记忆化. 废话说完,上代码—— 1 #include <cstdio> 2 #include <cmath> 3 #include <cstring> 4 #include <a

[ACM] hdu 1242 Rescue (BFS+优先队列)

Rescue Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is:

hdu 4856 Tunnels(bfs+状态压缩)

题目链接:hdu 4856 Tunnels 题目大意:给定一张图,图上有M个管道,管道给定入口和出口,单向,现在有人想要体验下这M个管道,问最短需要移动的距离,起点未定. 解题思路:首先用bfs处理出两两管道之间移动的距离,然后后用状态压缩求出最短代价,dp[i][j],i表示的已经走过的管道,j是当前所在的管道. #include <cstdio> #include <cstring> #include <queue> #include <algorithm&g

hdu 1104 数论+bfs

题意:给n,m,k;输出n经过+-*%后(n%k+k)%k==((n+1)%k)%k  输出最小路径与运算副 zsd:% 只是求余数 有正负 mod 是求模 无正负. yhd:对m*k求余对 对k求余不对 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5

HDU 1495——非常可乐( BFS )

非常可乐 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4663    Accepted Submission(s): 1868 Problem Description 大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为.因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要

HDU 1241 Oil Deposits bfs 难度:0

http://acm.hdu.edu.cn/showproblem.php?pid=1241 对每个还未访问的点bfs,到达的点都标为一块,最后统计有多少块即可 #include <cstdio> #include <cstring> #include <queue> using namespace std; const int maxn=101; const int inf=0x3fffffff; char maz[maxn][maxn]; int id[maxn][

hdu 5094 Maze bfs+状态压缩

Maze Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others) Total Submission(s): 642    Accepted Submission(s): 229 Problem Description This story happened on the background of Star Trek. Spock, the deputy captain of St

hdu 1242 Rescue(bfs+优先队列)

Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is: approach Angel. We assume