1. 程式人生 > >hud-1241-Oil Deposits (dfs)

hud-1241-Oil Deposits (dfs)

##Oil Deposits
翻譯
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 41406 Accepted Submission(s): 23977

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

####Source
Mid-Central USA 1997

####Recommend
Eddy | We have carefully selected several similar problems for you: 1016 1010 1312 1242 1240

dfs模板題吧,八個方向搜尋;(像i,j這樣的計數器還是寫在區域性比較好,我盡然被定義域的問題搞了一晚上醉了醉了。。。)

#include<bits/stdc++.h>
using namespace std;

int n,m,s;
char maze[107][107];
int vx[8]={-1,1,0,0,-1,-1,1,1};
int vy[8]={0,0,-1,1,-1,1,1,-1};

void dfs(int x,int y){
    maze[x][y]='*';
    for(int i=0;i<8;i++){
        int tx=x+vx[i];
        int ty=y+vy[i];
        if(tx>=0&&tx<m&&ty>=0&&ty<n&&maze[tx][ty]=='@')
            dfs(tx,ty);
    }
}

int main(){
    int i,j;
    while(cin>>m>>n&&m){
        s=0;
        for(i=0;i<m;i++)
            cin>>maze[i];
        for(i=0;i<m;i++){//相當於不連通的情況
            for(j=0;j<n;j++){
                if(maze[i][j]=='@'){
                    dfs(i,j);
                    s++;
                }
            }
        }
        cout<<s<<endl;
    }
	return 0;
}

相關推薦

hud-1241-Oil Deposits (dfs)

##Oil Deposits 翻譯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 41406

HDU 1241 Oil Deposits (DFS or BFS)

ble 多少 har pri 2017年 -m tchar creat int 鏈接 : Here! 思路 : 搜索判斷連通塊個數, 所以 $DFS$ 或則 $BFS$ 都行嘍...., 首先記錄一下整個地圖中所有$Oil$的個數, 然後遍歷整個地圖, 從油田開始搜索

HDU - 1241 Oil Deposits dfs

Oil Deposits  HDU - 1241 The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with

HDU 1241 Oil Deposits DFS 水題 基礎練習

Find a wayTime Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4360    Accepted Submiss

HDU 1241 Oil Deposits DFS無回溯 求油田的個數

Oil Deposits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 12195    Accepted S

HDU 1241 Oil DepositsDFS

這道題是一個比較水的搜尋題,思路就是先找到一塊油田,然後找這塊油田周圍的油田,這樣組成了一大塊油田,看一共有多少大塊油田,輸出。 附程式碼如下: #include<iostream> #include<cstring> #include<cstdio> u

HDU-1241-Oil DepositsDFS深度搜索模版題】

Oil Deposits Problem Description The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvC

hdu 1241 Oil Deposits

ott 由於 class parent mission desc 數字 tint 字符 題意:用廣度優先搜索 //c++寫輸入時有問題 1)這個是深搜 #include <stdio.h> #include &l

Oil Deposits DFS FloodFill漫水填充法求連通塊問題

with this ron reg row led one 個數 ret Oil Deposits The GeoSurvComp geologic survey company is responsible for detecting underground

HDU 1241: Oil Deposits

mono scanf oid new dir ref string string.h text Oil Deposits #include<stdio.h>#include<string.h>int m,n,dir[8][2]={{-1,-1},{-

UVA 572 Oil Deposits(DFS求連通塊)

dep 案例 nbsp color deposits 所在 統計字符 for i++ 題意:多組案例,每組案例輸入一個m行n列的字符矩陣,統計字符‘@’組成多少個連通塊。如果兩個字符‘@’所在的格子相鄰(橫、豎或對角線),則說明它們屬於同一連通塊。 //將判斷函數直接歸在

HDU 1241 - Oil Deposits - [BFS]

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=1241 題意: 求某塊平面上,連通塊的數量。一個油田格子若周圍八個方向也有一個油田格子,則認為兩者相連通。 AC程式碼: #include<bits/stdc++.h> using nam

HDU 1241 Oil Deposits 題解

Oil Deposits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 45017  

UVa Oil Deposits(dfs)

和poj 2386一模一樣 簡單的深搜 #include<stdio.h> char map[105][105]; int m, n; void dfs(int x, int y) { map[x][y] = '*'; for (int i = -1;

hdu 1241 Oil Deposits (簡單搜尋)

題目:   The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large recta

HDU-1241 Oil Deposits BFS+剪枝

以前做過的求聯通塊問題。這裡主要有兩點 1.漸漸出來一個板子,即 ①判出 ②迴圈方向 ①剪枝(越界,或者已經遍歷過) ②判斷是否可走 進行下一步dfs或者bfs 2.執行時間31ms,和網上的15ms有區別。我覺得主要是init函式花費較多時間。但是這樣的模組化讓程式碼可讀性更高了。 #

POJ1562,Oil Deposits,dfs+bfs,這幾天有做東西,沒寫部落格= =

Oil Deposits Description The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp wor

hdoj - 1241 Oil Deposits

string all code style ret contains vertica creates and Problem Description The GeoSurvComp geologic survey company is responsible for de

Oil Deposits hdu-1241 DFS

lib part str std math.h row nal sent print The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits.

Oil Deposits (hdu 1241) (基礎dfs)

Oil Deposits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 42649    Accepted S