Oil Deposits(DFS,基礎題)
Oil Deposits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5776 Accepted Submission(s): 3360
原題連結:點選開啟連結
Total Submission(s): 5776 Accepted Submission(s): 3360
原題連結:點選開啟連結
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
Recommend
Eddy
開始時想用並查集。。。後來還是深搜算了,程式很快寫出來,可是測試例子的時候卻發現有誤,debug半天才發現原來是複製網站上的例子的時候換行符不知道為何沒複製過去。。。
AC CODE
//Memory: 344 KB Time: 0 MS
//Language: C++ Result: Accepted
#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
#define LL long long
#define MAXI 2147483647
#define MAXL 9223372036854775807
#define dg(i) cout << "*" << i << endl;
using namespace std;
typedef struct Node
{
int x, y;
Node(int i, int j)
{
x = j; y = i;
}
}P;
queue<Node> Q;
int n, m;
char grid[101][101];
int move[8][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {-1, 1}, {1, -1}, {-1, -1}};
void DFS(int i, int j)
{
int ii, jj, k;
for(k = 0; k < 8; k++)
{
ii = i + move[k][0];
jj = j + move[k][1];
if(ii > -1 && jj > -1 && ii < m && jj < n && grid[ii][jj] == '@')
{
grid[ii][jj] = '*';
DFS(ii, jj);
}
}
}
int main()
{
int i, j, cnt;
char c;
while(scanf("%d %d", &m, &n) && m)
{
cnt = 0;
for(i = 0; i < m; i++)
{
scanf("%c", &c); //吃掉換行符
for(j = 0; j < n; j++)
{
scanf("%c", &grid[i][j]);
if(grid[i][j] == '@')
{
P t(i, j);
Q.push(t);
}
}
}
while(!Q.empty())
{
P t = Q.front();
Q.pop();
if(grid[t.y][t.x] == '@')
{
cnt++;
DFS(t.y, t.x);
}
}
printf("%d\n", cnt);
}
return 0;
}
相關文章
- POJ 1562 Oil Deposits
- HDU 1241Oil Deposits(簡單搜尋題)
- hdu 1241 Oil Deposits 深搜 Ac
- Clone Graph leetcode java(DFS and BFS 基礎)LeetCodeJava
- DFS深度優先搜尋(3)--hdu2181(哈密頓繞行世界問題)(基礎題)
- 基礎題
- DFS序例題+感受
- 結構與OIL
- Java基礎題Java
- 基礎題一
- 基礎題二
- 【題目整理】dfs入門
- BFS廣度優先搜尋(5)(亦可以用DFS)--hdu1241(poj1562)(基礎題)
- java基礎題(部分)Java
- python基礎題Python
- iOS基礎面試題iOS面試題
- Oracle基礎面試題Oracle面試題
- java基礎面試題Java面試題
- Red and Black(DFS入門題)
- dfs的return時機問題
- JS基礎知識(覆蓋JS基礎面試題)JS面試題
- 【IOS開發基礎系列】Cocoa基礎專題iOS
- python 基礎習題1--基礎語法Python
- JAVA 基礎練習題Java
- js面試題(基礎)梳理JS面試題
- go面試題-基礎類Go面試題
- Golang 基礎面試題 01Golang面試題
- MQTT-主題基礎MQQT
- Android基礎面試題Android面試題
- 基礎學習問題
- java基礎筆試題Java筆試
- [zt] Oracle基礎面試題Oracle面試題
- SQL基礎教程問題SQL
- java面試題--基礎上Java面試題
- Java基礎面試題下Java面試題
- 【圖論】Python [ numpy, pandas] 實現 基礎能力以及基礎演算法 [ dfs bfs spfa ] 經過較為嚴格測試圖論Python演算法
- Python基礎面試題30問!Python基礎教程Python面試題
- DFS