HDU 1241 Oil Deposits (基础DFS 或者 BFS)

来源:互联网 发布:淘宝恶意退款技巧 编辑:程序博客网 时间:2024/06/06 03:03

Oil Deposits

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

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
0122

Source
Mid-Central USA 1997

题解:给你一个油田的图,然后计算这个图的连通块。
图的遍历.有DFS和BFS.
由于DFS更容易编写,一般用DFS找连通块
:从每个"@"格子出发.递归遍历它周围的"@"格子.每次访问一个格子的时候就给它写上''连通分量编号",即标志一下就ok了,这样就可以在访问之前检查它是否有了编号,从而避免同一格子访问多次。

AC代码:DFS
#include<iostream>#include<stdio.h>#include<cstring>#include<algorithm>using namespace std;char map[110][110];int dir[8][2]={{0,1},{0,-1},{1,0},{-1,0},{1,1},{-1,-1},{-1,1},{1,-1}};int vis[110][110];int next_x,next_y;void dfs(int x,int y){map[x][y]='*'; //标志一下,避免重复计数 for(int k=0;k<8;k++){next_x=x+dir[k][0];next_y=y+dir[k][1];if(map[next_x][next_y]=='@'){dfs(next_x,next_y);}}}int main(){int n,m;while(cin>>n>>m,m&&n){int count=0;for(int i=0;i<n;i++){for(int j=0;j<m;j++){cin>>map[i][j];}}for(int i=0;i<n;i++){for(int j=0;j<m;j++){if(map[i][j]=='@'){count++;dfs(i,j);}}}printf("%d\n",count);}return 0;}

AC代码:BFS
#include<stdio.h>#include<cstring>#include<queue>using namespace std;int n,m;char a[100][100];int sum;int dir[8][2]={1,0,1,1,1,-1,0,1,0,-1,-1,0,-1,1,-1,-1};      //8个方向struct node{    int x;    int y;};int OK(int x,int y){    if(x>=0&&x<n&&y>=0&&y<m)        return true;    return false;}void bfs(int x,int y){    int i;    node now,next;    queue<node> q;    now.x=x;    now.y=y;    q.push(now);    while(!q.empty())    {        now=q.front();        q.pop();        for(i=0;i<8;i++)        {            next.x=now.x+dir[i][0];            next.y=now.y+dir[i][1];            if(OK(next.x,next.y)&&a[next.x][next.y]=='@')     //判断油田和越界            {                a[next.x][next.y]='*';     //油田群遍历,标记                q.push(next);            }        }    }}int main(){    int i,j;    while(scanf("%d%d",&n,&m)&&m)    {        sum=0;        for(i=0;i<n;i++)            scanf("%s",a[i]);        for(i=0;i<n;i++)        {            for(j=0;j<m;j++)            {                if(a[i][j]=='@')      //找到油田                {                    sum++;                    bfs(i,j);                }            }        }        printf("%d\n",sum);    }    return 0;}



3 0
原创粉丝点击