正文

Oil Deposits2008-08-13 14:59:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/1mi2/37565.html

分享到:

  Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 22   Accepted Submission(s) : 9 Font: Times New Roman | Verdana | Georgia Font Size: ← → 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 Source Mid-Central USA 1997     #include<stdio.h>#include<string.h>#include<iostream>using namespace std;struct {    int i,j;}queue[10005];char f[105][105];int fr,l,m,n;void dfs(int i,int j){                    f[i][j]='*';        if(i-1>0&&f[i-1][j]=='@')        {            f[i-1][j]='*';            queue[fr].i=i-1;            queue[fr++].j=j;        }        if(j-1>0&&f[i][j-1]=='@')        {            f[i][j-1]='*';            queue[fr].i=i;            queue[fr++].j=j-1;        }        if(i-1>0&&j-1>0&&f[i-1][j-1]=='@')        {            f[i-1][j-1]='*';            queue[fr].i=i-1;            queue[fr++].j=j-1;        }        if(i+1<=m&&f[i+1][j]=='@')        {            f[i+1][j]='*';            queue[fr].i=i+1;            queue[fr++].j=j;        }        if(j+1<=n&&f[i][j+1]=='@')        {            f[i][j+1]='*';            queue[fr].i=i;            queue[fr++].j=j+1;        }        if(i+1<=m&&j+1<=n&&f[i+1][j+1]=='@')        {            f[i+1][j+1]='*';            queue[fr].i=i+1;            queue[fr++].j=j+1;        }        if(i-1>0&&j+1<=n&&f[i-1][j+1]=='@')        {            f[i-1][j+1]='*';            queue[fr].i=i-1;            queue[fr++].j=j+1;        }        if(j-1>0&&i+1<=m&&f[i+1][j-1]=='@')        {            f[i+1][j-1]='*';            queue[fr].i=i+1;            queue[fr++].j=j-1;        }    } int main(){    int total,i,j;    while(cin>>m>>n)    {        if(m==0||n==0) break;        total=0;        for(i=1;i<=m;i++)            for(j=1;j<=n;j++)                cin>>f[i][j];                    total=0;        for(i=1;i<=m;i++)            for(j=1;j<=n;j++)            {                    l=0;fr=0;                if(f[i][j]=='@')                {                    total++;                    queue[fr].i=i;                    queue[fr++].j=j;                }                while(l<fr)                {                    dfs(queue[l].i,queue[l].j);                    l++;                }            }        printf("%d\n",total);            }    return 0;}  

阅读(2576) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册