【HDU - 1241】Oil Deposits(dfs+染色)

Oil Deposits

Descriptions:html

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

Inputide

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 Inputui

1 1 this

* spa

3 5.net

*@*@*code

**@**htm

*@*@*

1 8

@@****@*

5 5

****@

*@@*@

*@**@

@@@*@

@@**@

0 0

Sample Output

0

1

2

2

題目大意:

GeoSurvComp地質調查公司負責探測地下石油儲藏。 GeoSurvComp如今在一塊矩形區域探測石油,並把這個大區域分紅了不少小塊。他們經過專業設備,來分析每一個小塊中是否蘊藏石油。若是這些蘊藏石油的小方格相鄰,那麼他們被認爲是同一油藏的一部分。在這塊矩形區域,可能有不少油藏。你的任務是肯定有多少不一樣的油藏。

Input

輸入可能有多個矩形區域(便可能有多組測試)。每一個矩形區域的起始行包含m和n,表示行和列的數量,1<=n,m<=100,若是m =0表示輸入的結束,接下來是n行,每行m個字符。每一個字符對應一個小方格,而且要麼是'*',表明沒有油,要麼是'@',表示有油。

Output

對於每個矩形區域,輸出油藏的數量。兩個小方格是相鄰的,當且僅當他們水平或者垂直或者對角線相鄰(即8個方向)。

題目連接:

https://vjudge.net/problem/HDU-1241

 

典型的dfs深搜題,加個染色就行,即每一個大油田染上一種顏色,求出多少種顏色便可

AC代碼

#include <iostream> #include <cstdio> #include <fstream> #include <algorithm> #include <cmath> #include <deque> #include <vector> #include <queue> #include <string> #include <cstring> #include <map> #include <stack> #include <set>
#define mod 1000000007
#define ll long long
#define INF 0x3f3f3f3f
using namespace std; int n,m,num; int vis[105][105];//是否能染色
char mp[105][105];//記錄地圖
int dt[8][2]={{0,-1},{0,1},{1,0},{-1,0},{1,-1},{1,1},{-1,-1},{-1,1}};//八個方向
void dfs(int x,int y) { if(mp[x][y]=='#'||vis[x][y]!=0||x<0||y<0||x>=n||y>=m)//不知足條件就返回
        return; vis[x][y]=num;//染色
    for(int i=0; i<8; i++)//判斷往哪走
 { int tx=x+dt[i][0]; int ty=y+dt[i][1]; if(mp[tx][ty]=='@'&&vis[tx][ty]==0&&tx>=0&&tx<n&&ty>=0&&ty<m) { dfs(tx,ty); } } } int main() { while(cin >> n>>m,n+m) { num=0; memset(vis,0,sizeof(vis)); for(int i=0; i<n; i++) cin >> mp[i]; for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { if(mp[i][j]=='@'&&vis[i][j]==0)//搜索知足條件
 { num++;//這一塊染上這個數字的顏色
                    dfs(i,j);//開始深搜
 } } } cout<< num << endl;//有幾種顏色
 } }

 

原文出處:https://www.cnblogs.com/sky-stars/p/10964563.html

相關文章
相關標籤/搜索