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;
}
时间: 2024-09-11 04:37:50

HDU1241-Oil Deposits的相关文章

杭电bfs 水题1241 Oil Deposits - 油田 详解 + 分析

Oil Deposits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13887    Accepted Submission(s): 7985 Problem Description The GeoSurvComp geologic survey company is responsible for detecting under

hdu 1241 Oil Deposits

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241 Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 18297 Accepted Submission(s): 10548 Problem Description The GeoSurvComp geologic su

UVa 572:Oil Deposits 搜索专题

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105&page=show_problem&problem=513 题目类型: 搜索 样例输入: 1 1 * 3 5 *@*@* **@** *@*@* 1 8 @@****@* 5 5 ****@ *@@*@ *@**@ @@@*@ @@**@ 0 0 样例输出: 0 1 2 2 分析: 这一题可以说是搜索

hdu 1241 Oil Deposits (一次dfs搞定有某有)

#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; char map[105][105]; int dir[8][2]={0, 1, 1, 0, -1, 0, 0, -1, 1, 1, 1, -1, -1, 1, -1, -1}; int n, m; int ans; bool judge(int x, int y){ i

uva 572 - Oil Deposits

点击打开链接 题目意思:给你一块区域里面分布着油田,只要有连着就属于同一个油田,求出该区域有几个油田 解题思路:简单的Bfs 或 dfs 可以搞定 ,注意对走过的进行标记用mark数组. 代码: //DFS代码(用到递归) #include <iostream> #include <queue> #include <cstdio> #include <cstring> using namespace std; const int MAXN = 110; in

POJ题目分类

初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      (4)递推.      (5)构造法.(poj3295)      (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996) 二.图算法:      (1)图的深度优先遍历和广度优先遍历.      (2)最短路径算法(dijkstra,bellman-ford

UVa 657:The die is cast 搜索专题

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105&page=show_problem&problem=598 题目类型: 搜索 样例输入: 30 15 .............................. .............................. ...............*.............. ...****

poj分类

初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      (4)递推.      (5)构造法.(poj3295)      (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996) 二.图算法:      (1)图的深度优先遍历和广度优先遍历.      (2)最短路径算法(dijkstra,bellman-ford

ACM练级

一般要做到50行以内的程序不用调试.100行以内的二分钟内调试成功.acm主要是考算法的 ,主要时间是花在思考算法上,不是花在写程序与debug上.  下面给个计划你练练: 第一阶段: 练经典常用算法,下面的每个算法给我打上十到二十遍,同时自己精简代码, 因为太常用,所以要练到写时不用想,10-15分钟内打完,甚至关掉显示器都可以把程序打 出来.  1.最短路(Floyd.Dijstra,BellmanFord)  2.最小生成树(先写个prim,kruscal要用并查集,不好写)  3.大数(