您的位置:首页 > 其它

Oil Deposits (HDU 1241)

2014-04-28 16:47 393 查看
说实话,当我真正看懂这题后,感觉虽然有想法,但是却很难实现,我郁闷地看着电脑屏幕。。

最可恨的是我同学已经写出来了,感觉很不爽额。。

然后我就不管那么多了,慢慢写吧,能写多少写多少,我就不信写不出来,哼。。

接着写着写着,就发现似乎不难,思路很清晰了,最后,,哈哈,一次性AC了^-^

Oil Deposits

[b]Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 10870 Accepted Submission(s): 6319

[/b]

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


代码如下:

#include<stdio.h>
char x[100][100];
void dfs(int i,int j,int m,int n)
{
    if(i-1>=0)
    {
        if(j-1>=0&&x[i-1][j-1]=='@')
            x[i-1][j-1]='*',dfs(i-1,j-1,m,n);
        if(x[i-1][j]=='@')
            x[i-1][j]='*',dfs(i-1,j,m,n);
        if(j+1<n&&x[i-1][j+1]=='@')
            x[i-1][j+1]='*',dfs(i-1,j+1,m,n);
    }
    if(j-1>=0&&x[i][j-1]=='@')
        x[i][j-1]='*',dfs(i,j-1,m,n);
    if(j+1<n&&x[i][j+1]=='@')
        x[i][j+1]='*',dfs(i,j+1,m,n);
    if(i+1<m)
    {
        if(j-1>=0&&x[i+1][j-1]=='@')
            x[i+1][j-1]='*',dfs(i+1,j-1,m,n);
        if(x[i+1][j]=='@')
            x[i+1][j]='*',dfs(i+1,j,m,n);
        if(j+1<n&&x[i+1][j+1]=='@')
            x[i+1][j+1]='*',dfs(i+1,j+1,m,n);
    }
}

int main()
{

    int m,n,i,j;

    while(~scanf("%d%d",&m,&n),m)
    {
        int sum=0;
        for(i=0; i<m; i++)
            scanf("%s",x[i]);
        for(i=0; i<m; i++)
            for(j=0; j<n; j++)
                if(x[i][j]=='@')
                {
                    sum++;
                    x[i][j]='*';
                    dfs(i,j,m,n);
                }
        printf("%d\n",sum);
    }
    return 0;
}

后来我才知道这是深度优先搜索题。。

由于当时写的这么挫的代码别人已经看不下去了,连被评论两发,于是只好贴一个学过dfs后写的吧。。。

#include<stdio.h>
#include<string.h>
const int maxn = 110;
int mat[maxn][maxn];

void dfs(int x,int y)
{
    if(!mat[x][y]) return;
    mat[x][y] = 0;
    dfs(x-1,y-1); dfs(x-1,y); dfs(x-1,y+1);
    dfs(x,y-1);               dfs(x,y+1);
    dfs(x+1,y-1); dfs(x+1,y); dfs(x+1,y+1);//这里用for也可以咯
}

int main()
{
    int m,n,i,j;
    char x[maxn];

    while(~scanf("%d%d",&m,&n),m)
    {
        memset(mat, 0, sizeof(mat));
        for(i=0; i<m; i++)
        {
            scanf("%s",x);
            for(j=0; j<n; j++)
                if(x[j] == '@') mat[i+1][j+1] = 1;
        }
        int sum = 0;
        for(i=1; i<=m; i++)
            for(j=1; j<=n; j++)
                if(mat[i][j])
                   sum++, dfs(i,j);
        printf("%d\n",sum);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: