HDU1241-Oil Deposits

简介:

Oil Deposits
Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 9   Accepted Submission(s) : 6
Problem DescriptionThe 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.
 


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


OutputFor 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 Input1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
 


Sample Output0
1
2
2

 

题意:所给你一个地图,有两种元素分别为‘.’与@,然后要求你找出不相连的@的个数,@的邻接与@的对角都算是相连的。

注意:第三组测试数据的"5 5“后有个空格,复制粘贴会出错,建议手动输入
//DFS

#include<stdio.h>
#include<string.h>
struct node
{
   char V;
   int flag; 
}a[200][200];
int n,m;
void Fun(int x,int y)
{
     if(x+1<n&&y-1>=0&&a[x+1][y-1].flag==0&&a[x+1][y-1].V=='@')
     {
        a[x+1][y-1].flag=1;
        Fun(x+1,y-1);
     }
     
     if(x+1<n&&y+1<m&&a[x+1][y+1].flag==0&&a[x+1][y+1].V=='@')
     {
        a[x+1][y+1].flag=1;
        Fun(x+1,y+1);
     }
     
     if(x-1>=0&&y-1>=0&&a[x-1][y-1].flag==0&&a[x-1][y-1].V=='@')
     {
        a[x-1][y-1].flag=1;
        Fun(x-1,y-1);
     }
     
     if(x-1>=0&&y+1<m&&a[x-1][y+1].flag==0&&a[x-1][y+1].V=='@')
     {
        a[x-1][y+1].flag=1;
        Fun(x-1,y+1);
     }
     
     if(x+1<n&&a[x+1][y].V=='@'&&a[x+1][y].flag==0)
     {
        a[x+1][y].flag=1;
        Fun(x+1,y);
     }
     
     if(x-1>=0&&a[x-1][y].V=='@'&&a[x-1][y].flag==0)
     {
        a[x-1][y].flag=1;
        Fun(x-1,y);
     }
     
     if(y+1<m&&a[x][y+1].V=='@'&&a[x][y+1].flag==0)
     {
        a[x][y+1].flag=1;
        Fun(x,y+1);
     }
     
     if(y-1>=0&&a[x][y-1].V=='@'&&a[x][y-1].flag==0)
     {
        a[x][y-1].flag=1;
        Fun(x,y-1);
     }
     
     return;
}
int main()
{
    int i,j,sum;
    while(scanf("%d %d",&n,&m),n!=0&&m!=0)
    {
       memset(a,0,sizeof(a));
       for(i=0;i<n;i++)
       { 
          getchar();
          for(j=0;j<m;j++)
          scanf("%c",&a[i][j].V);
       }
        sum=0;
        for(i=0;i<n;i++)
        {
           for(j=0;j<m;j++)
           {
               if(a[i][j].V=='@'&&a[i][j].flag==0)
               {
                  a[i][j].flag=1;
                  Fun(i,j);
                  sum++;
               }
           }
        }
        printf("%d\n",sum);
    }
    return 0;
}
相关文章
|
8月前
Strange fuction(HDU--2899)
Strange fuction(HDU--2899)
ZOJ - Summer 2018 - Contest 2 by Astolfo - Problems - 1002: Hazard and The Triangle
ZOJ - Summer 2018 - Contest 2 by Astolfo - Problems - 1002: Hazard and The Triangle
115 0
ZOJ - Summer 2018 - Contest 2 by Astolfo - Problems - 1002: Hazard and The Triangle
HDU-1017,A Mathematical Curiosity
HDU-1017,A Mathematical Curiosity
HDOJ/HDU 1241 Oil Deposits(经典DFS)
HDOJ/HDU 1241 Oil Deposits(经典DFS)
94 0
|
Go
HDOJ/HDU 1087 Super Jumping! Jumping! Jumping!(经典DP~)
HDOJ/HDU 1087 Super Jumping! Jumping! Jumping!(经典DP~)
91 0

热门文章

最新文章