hdu 1241 Oil Deposits【广度优先搜索】
2014-11-03 21:46
120 查看
Oil Deposits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12655 Accepted Submission(s): 7359
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
1 1 * 3 5 *@*@* **@** *@*@* 1 8 @@****@* 5 5 ****@ *@@*@ *@**@ @@@*@ @@**@ 0 0
[align=left]Sample Output[/align]
0 1 2 2代码示例:#include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> #include<queue> #define N 100+10 using namespace std; typedef struct { int x,y; }node; int map ; int n,m; int dir[8][2]={{1,0},{0,1},{-1,0},{0,-1},{1,1},{-1,1},{1,-1},{-1,-1}}; bool judge(int x,int y) { if(x<1||x>m||y<1||y>n) return false; if(map[x][y]) return false; return true; } void bfs(int x,int y) { queue<node>Q; node fir,nex; fir.x=x; fir.y=y; map[x][y]=1; Q.push(fir); while(!Q.empty()) { fir=Q.front(); Q.pop(); for(int i=0;i<8;i++) { nex.x=fir.x+dir[i][0]; nex.y=fir.y+dir[i][1]; if(judge(nex.x,nex.y)) { map[nex.x][nex.y]=1; Q.push(nex); } } } } int main() { char c; int sum; while(~scanf("%d%d",&m,&n)) { if(m==0) return 0; for(int i=1;i<=m;i++) for(int j=1;j<=n;j++) { cin>>c; if(c=='*') map[i][j]=1; else map[i][j]=0; } sum=0; for(int i=1;i<=m;i++) for(int j=1;j<=n;j++) { if(!map[i][j]) { bfs(i,j); sum++; } } printf("%d\n",sum); } return 0; }
相关文章推荐
- hdu 1241 Oil Deposits
- Oil Deposits 石油储藏HDU - 1241 BFS
- hdu 1241 Oil Deposits (一次dfs搞定有某有)
- (HDU - 1241)Oil Deposits
- HDU 1241 Oil Deposits【简单DPS搜索】
- HDU 1241 Oil Deposits[dfs || bfs]
- hdu 题目1241 Oil Deposits(简单DFS)
- HDU 1241 Oil Deposits (DFS)
- hdu 1241 Oil Deposits
- hdu 1241 Oil Deposits
- hdu - 1241 - Oil Deposits
- HDU 1241 Oil Deposits
- HDU 1241 Oil Deposits【深搜练习】
- HDU 1241 Oil Deposits 深搜(DFS)
- hdu 杭电 1241 Oil Deposits
- hdu 1241 Oil Deposits
- HDU-1241-Oil Deposits【DFS】
- hdu 1241 Oil Deposits 简单dfs
- Hdu 1241 Oil Deposits
- hdu 1241 Oil Deposits