首页 > 其他分享 >kuangbin专题一 简单搜索 石油储备(HDU-1241)

kuangbin专题一 简单搜索 石油储备(HDU-1241)

时间:2023-04-15 20:44:51浏览次数:46  
标签:HDU oil int typedef 1241 vis kuangbin include nj

Oil Deposits

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

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


题目大意

'@'表示有石油储备,求出所给矩阵中石油连通块的个数,每个方格都与其上、下、左、右、左上、右上、左下、右下八个方格视为相邻,相邻的石油方格同属于一个连通块。



解题思路

很单纯的模板题,DFS或者BFS都行,这里给一个DFS的代码。

/*   一切都是命运石之门的选择  El Psy Kongroo  */
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<cmath>
#include<functional>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<double, double> pdd;
typedef pair<string, pii> psi;
typedef __int128 int128;
#define PI acos(-1.0)
#define x first
#define y second
const int inf = 0x3f3f3f3f, mod = 1e9 + 7;

const int N = 110;
char g[N][N];
int vis[N][N];
int n, m;
int dx[8] = {1, -1, 0, 0, 1, -1, 1, -1};
int dy[8] = {0, 0, 1, -1, 1, -1, -1, 1};


void dfs(int i, int j){
    vis[i][j] = 1;
    for(int k = 0; k < 8; k++){
        int ni = i + dx[k], nj = j + dy[k];
        if(ni < 0 || ni >= n || nj < 0 || nj >= m || vis[ni][nj]) continue;
        if(g[ni][nj] != '@') continue;
        dfs(ni, nj);
    }
}


int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    while(cin >> n >> m && (n && m)){
        memset(vis, 0, sizeof(vis));
        for(int i = 0; i < n; i ++ )  cin >> g[i];

        int ans = 0;
        for(int i = 0; i < n; i ++ )
            for(int j = 0; j < m; j ++ )
                if(!vis[i][j] && g[i][j] == '@')
                    ans ++ , dfs(i, j);
        
        cout << ans << endl;
    }
    
    return 0;
}

标签:HDU,oil,int,typedef,1241,vis,kuangbin,include,nj
From: https://www.cnblogs.com/MAKISE004/p/17321810.html

相关文章

  • kuangbin专题一 简单搜索 起火迷宫(UVA-11624)
    Fire!DescriptionJoeworksinamaze.Unfortunately,portionsofthemazehavecaughtonfire,andtheownerofthemazeneglectedtocreateafireescapeplan.HelpJoeescapethemaze.GivenJoe’slocationinthemazeandwhichsquaresofthemazeareo......
  • kuangbin专题一 简单搜索 点火游戏(FZU-2150)
    FireGameDescriptionFatbrotherandMazeareplayingakindofspecial(hentai)gameonanN*Mboard(Nrows,Mcolumns).Atthebeginning,eachgridofthisboardisconsistingofgrassorjustemptyandthentheystarttofireallthegrass.Firstlyt......
  • kuangbin专题一 简单搜索 罐子(POJ-3414)
    PotsTimeLimit:1000MS MemoryLimit:65536KDescriptionYouaregiventwopots,havingthevolumeofAandBlitersrespectively.Thefollowingoperationscanbeperformed:FILL(i)fillthepoti(1≤i≤2)fromthetap;DROP(i)emptythep......
  • kuangbin专题一 简单搜索 找倍数(POJ-1426)
    FindTheMultipleTimeLimit:1000MS MemoryLimit:10000KDescriptionGivenapositiveintegern,writeaprogramtofindoutanonzeromultiplemofnwhosedecimalrepresentationcontainsonlythedigits0and1.Youmayassumethatnisnotgreaterth......
  • kuangbin专题一 简单搜索 翻转(POJ-3279)
    FliptileTimeLimit:2000MS MemoryLimit:65536KDescriptionFarmerJohnknowsthatanintellectuallysatisfiedcowisahappycowwhowillgivemoremilk.HehasarrangedabrainyactivityforcowsinwhichtheymanipulateanM×Ngrid(1≤M≤15;1≤......
  • kuangbin专题一 简单搜索 抓住那头牛(POJ-3278)
    CatchThatCowTimeLimit:2000MS MemoryLimit:65536KTotalSubmissions:210291 Accepted:63838DescriptionFarmerJohnhasbeeninformedofthelocationofafugitivecowandwantstocatchherimmediately.HestartsatapointN(0≤N≤100,000)on......
  • HDU 1042 N! (大整数阶乘)
    这道题开始并不会,是看了别人的代码,自己又改造了一下,代码如下:(PS:这个时候自带大整数运算的java就有优势了)#include<bits/stdc++.h>usingnamespacestd;constintN=20000+10;intans[N];voidfact(intn){ans[0]=ans[1]=1;inttot=1;for(inti=......
  • HDU 1253 胜利大逃亡
    题目有个坑是可能没有到达门口的路,结果WA好几次#include<iostream>#include<cstdio>#include<queue>#include<algorithm>usingnamespacestd;constintINF=10000000;inta,b,c,d;ints[60][60][60],cost[60][60][60];intdx[]={0,0,1,-1,0,0}......
  • hdu 1272 小希的迷宫
    这是我第一次用并查集解决问题,其实刷这道题就是为了学习并查集,这是学长在hdu上找的模板题#include<iostream>#include<cstdio>usingnamespacestd;constintN=110000;intpar[N],mark[N];intfind1(intx){intr=x;while(par[r]!=r)r=par[r];......
  • HDU 5443 The Water Problem RMQ
    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5443题意:给定一个数组,查询区间最大值思路:RMQ模板题#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<queue>usingnamespacestd;constint......