ACM查找油田块

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

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 <iostream>
using namespace std;
#define N 111
char str[N][N];
int a,b;
int next[8][2]={{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}};//八个方向
void fun(int x,int y)
{
    int m,n,k;
    for(k=0;k<8;k++)
    {
        n=x+next[k][0];
        m=y+next[k][1];
        if(n<0||n>a-1||m<0||m>b-1||str[n][m]==‘*‘)
            continue;
        str[n][m]=‘*‘;//标记
        fun(n,m);//递归
    }
}
int main()
{
    int flag;
    while(cin>>a>>b&&(a||b))
    {
        flag=0;
        for(int i=0;i<a;i++)
            for(int j=0;j<b;j++)
                cin>>str[i][j];
        for(int x=0;x<a;x++)
        {
            for(int y=0;y<b;y++)
            {
                if(str[x][y]==‘@‘)
                {
                    str[x][y]=‘*‘;//标记
                    fun(x,y);
                    flag++;//累加连通的块数
                }
            }
        }
        cout<<flag<<endl;
    }
    return 0;
}

				
时间: 2024-11-07 04:35:28

ACM查找油田块的相关文章

很在适强度政往然米

大片的骷髅便涌了上来这些骷髅全部是级的强化怪物虽然不是很强大但是蚁多咬死象而且还不是一般的多多如潮水要是想把他们杀光的话没有几天时间是不可能的地狱骷髅笨拙的铁剑凌空扬起直接劈在了我的肩甲之上只觉微微一痛我便损失了点气血 得知我干掉了骷髅王之后龙魂等人均是喜形于色但是当我提起要分赃的时候龙魂立刻摇头道做人不能太贪心鬼炙已经从你们那里得到了消亡套装而且骷髅王是书生你杀死的说句不好听的我们也没帮上多大的忙就算是骷髅王爆出神器我们也绝不会染指半分的 措不及防下傲世派出的七名刺客已经有四个被我干掉而剩下的

[ACM] POJ 1035 Spell checker (单词查找,删除替换增加任何一个字母)

Spell checker Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18693   Accepted: 6844 Description You, as a member of a development team for a new spell checking program, are to write a module that will check the correctness of given word

[ACM] POJ 1936 All in All (查找,一个串是否在另一个串中)

All in All Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 27521   Accepted: 11266 Description You have devised a new encryption technique which encodes a message by inserting between its characters randomly generated strings in a clever

2015 ACM/ICPC Asia Regional Changchun Online HDU 5444 Elven Postman【二叉排序树的建树和遍历查找】

Elven Postman Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 591    Accepted Submission(s): 329 Problem Description Elves are very peculiar creatures. As we all know, they can live for a very

ACM:二分查找,以及利用二分法来找上下界

(一)二分的模版: int binary_search(int *array, int length, int key) { int start = 0, end = length - 1; while(end >= start) { int middle = start + (end - start) / 2; int tmp = array[middle]; if(tmp < key) start = middle + 1; else if (tmp > key) end = mid

[ACM] poj 2456 Aggressive cows (二分查找)

Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5436   Accepted: 2720 Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,...

ACM大一练习赛-第三场------H - 玻色-爱因斯坦凝聚态《二分查找》

H - 玻色-爱因斯坦凝聚态 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description Now, here is a fuction: F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100) Can you find the minimum value when x is between 0

[ACM] POJ 3349 Snowflake Snow Snowflakes(哈希查找,链式解决冲突)

Snowflake Snow Snowflakes Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 30512   Accepted: 8024 Description You may have heard that no two snowflakes are alike. Your task is to write a program to determine whether this is really true. Y

[ACM] POJ 1035 Spell checker (单词查找,删除替换添加不论什么一个字母)

Spell checker Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18693   Accepted: 6844 Description You, as a member of a development team for a new spell checking program, are to write a module that will check the correctness of given word