poj 1562 Oil Deposits

Oil Deposits

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 13505   Accepted: 7345

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

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

Source

Mid-Central USA 1997

分析:

只有map[i][j]==0,该位置是新的oil deposit。然后广搜。

 1 #include<iostream>
 2 #include<queue>
 3 #include<cstdio>
 4 #include<cstring>
 5 using namespace std;
 6 int m,n;
 7 short map[105][105];
 8 short dir[8][2]={{-1,-1},{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1}};
 9 bool check(int x,int y){
10     if(x<0||x>=m||y<0||y>=n||map[x][y])
11     return false;
12     return true;
13 }
14 int main(){
15     while(scanf("%d%d",&m,&n)&&m!=0&&n!=0){
16         memset(map,0,sizeof(map));
17         int i,j;
18         for(i=0;i<m;i++){
19             for(j=0;j<n;j++){
20                 char c;
21                 cin>>c;
22                 if(c==‘*‘){
23                     map[i][j]=-1;
24                 }
25                 else{
26                     map[i][j]=0;
27                 }
28             }
29         }
30         //cout<<1<<endl;
31         /*for(i=0;i<m;i++){
32             for(j=0;j<n;j++){
33                 cout<<map[i][j]<<endl;
34             }
35         }*/
36         int count=0;
37         for(i=0;i<m;i++){
38             for(j=0;j<n;j++){
39                 if(map[i][j]==0){
40                 queue<int> q;
41                 q.push(i*100+j);
42                 map[i][j]=++count;
43                 while(!q.empty()){
44                     short x=q.front()/100;
45                     short y=q.front()%100;
46                     q.pop();
47                     int k;
48                     for(k=0;k<8;k++){
49                         short xx=x+dir[k][0];
50                         short yy=y+dir[k][1];
51                         if(check(xx,yy)){
52                             q.push(xx*100+yy);
53                             map[xx][yy]=map[x][y];
54                         }
55                     }
56                 }
57                 }
58             }
59         }
60         /*for(i=0;i<m;i++){
61             for(j=0;j<n;j++){
62                 cout<<map[i][j]<<endl;
63             }
64         }*/
65         cout<<count<<endl;
66     }
67     return 0;
68 }
时间: 2024-12-22 13:17:10

poj 1562 Oil Deposits的相关文章

(简单) POJ 1562 Oil Deposits,BFS。

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

[POJ] 1562 Oil Deposits (DFS)

Oil Deposits Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16655   Accepted: 8917 Description The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular r

HDU 1562 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 then ana

Oil Deposits(poj 1526 DFS入门题)

http://poj.org/problem?id=1562 Oil Deposits Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12595   Accepted: 6868 Description The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp wor

PKU 1562/HDU 1241 Oil Deposits(原油有多少块区域---BFS,DFS)

Oil Deposits Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region o

Oil Deposits (poj 1241) 搜索 (深度优先搜素)

Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8274 Accepted Submission(s): 4860 Problem Description The GeoSurvComp geologic survey company is responsible for detecting underground

poj1562 Oil Deposits(深搜dfs)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=1562 ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢

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