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

题目大意:
给定两个数m和n,表示m行,n列,然后给出一个字符矩阵,找有几个@组成的部分,一共有几个。。。。
解题思路:dfs搜索

#include <iostream>
#include <cstdio>
using namespace std;
const int maxn=200;
char map[maxn][maxn];
int dir[8][2]={{1,0},{-1,0},{0,1},{0,-1},{1,1},{-1,1},{1,-1},{-1,-1}};
int m,n,ans;
void dfs(int x, int y)
{
    for(int i=0; i<8; i++)
    {
        int nx=x+dir[i][0];
        int ny=y+dir[i][1];
        if(nx>=0 && nx<n && ny>=0 && ny<m && map[nx][ny]=='@')
        {
            map[nx][ny]='*';
            dfs(nx, ny);
        }
    }
}

int main()
{
    while(~scanf("%d%d",&n,&m),m,n)
    {
        ans=0;
        /*getchar();
        for(int i=1; i<=n; i++)
          for(int j=1; j<=m ;j++)
            scanf("%c",&map[i][j]);
            */
        for(int i=0; i<n; i++)
           scanf("%s",map[i]);
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                if(map[i][j] == '@')
                {
                    map[i][j]='*';
                    ans++;
                    // 1cout<<ans<<endl;
                    dfs(i,j);
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

/*
Sample Input
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0 

Sample Output
0
1
2
2
*/
时间: 2024-02-19 05:57:49

hdu 1241 Oil Deposits的相关文章

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

杭电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

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 分析: 这一题可以说是搜索

uva 572 - Oil Deposits

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

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

POJ题目分类

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

hduoj题目分类

基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029.1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092.1093.1094.1095.1096.1097.1098.1106.1108.1157.1163.1164.1170.1194.1196.1197.1201.1202.1205.1219.1234.1235.1236.1248.1

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