HDU 1241Oil Deposits(簡單搜尋題)
Oil Deposits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8471 Accepted Submission(s): 4949
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
題目大意:@表示油田,可以向周圍八個方向延伸,問你有多少塊油田。下面是DFS,BFS兩種解法。
題目地址:Oil Deposits
DFS很好解決,直接每次往八個方向擴充套件,dfs即可。
AC程式碼:
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
using namespace std;
char map1[105][105];
int visi[105][105];
void dfs(int i,int j)
{
if(map1[i][j]=='@'&&!visi[i][j])
{ //向周圍八個方向搜尋
visi[i][j]=1;
dfs(i-1,j-1);
dfs(i+1,j+1);
dfs(i-1,j);
dfs(i+1,j);
dfs(i-1,j+1);
dfs(i+1,j-1);
dfs(i,j-1);
dfs(i,j+1);
}
return;
}
int main()
{
int i,j,m,n,sum;
while(scanf("%d%d",&m,&n))
{
if(m==0&&n==0)
break;
memset(visi,0,sizeof(visi));
for(i=1;i<=m;i++) //邊界處理
{
map1[i][0]='*';
map1[i][n+1]='*';
}
for(i=1;i<=n;i++)
{
map1[0][i]='*';
map1[m+1][i]='*';
}
for(i=1;i<=m;i++)
scanf("%s",map1[i]+1);
/*for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
cout<<map1[i][j];
cout<<endl;
}*/
sum=0;
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
if(map1[i][j]=='@'&&!visi[i][j])
{
dfs(i,j);
sum++;
}
cout<<sum<<endl;
}
return 0;
}
//0MS 396K
BFS也比較好寫,座標需要轉換,當時腦子被驢踢了,行和列一直在糾結,最後WA了幾發!
AC程式碼:
#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;
char map1[105][105];
int visi[105][105];
int sum,m,n;
int dir[8][2]=
{
{-1,-1},{-1,0},{-1,1},{0,-1},
{0,1},{1,-1},{1,0},{1,1}
};
queue<int>q;
void bfs()
{
int i,j,k;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
if(map1[i][j]=='@'&&!visi[i][j])
{
int x,y,s,r;
q.push(i*n+j); //二維壓成一維的
while(!q.empty())
{
int head;
head=q.front();
q.pop(); //取出隊首元素
r=head/n; //橫座標
s=head%n; //縱座標
for(k=0;k<8;k++)
{
x=r+dir[k][0];//橫座標
y=s+dir[k][1];//縱座標
if(x>=0&&x<m&&y>=0&&y<n&&map1[x][y]=='@'&&visi[x][y]==0)
{
visi[x][y]=1;
q.push(x*n+y);
}
}
}
sum++;
}
}
}
int main()
{
int i;
while(scanf("%d%d",&m,&n))
{
if(m==0&&n==0) break;
memset(visi,0,sizeof(visi));
for(i=0;i<m;i++)
scanf("%s",map1[i]);
sum=0;
bfs();
cout<<sum<<endl;
}
return 0;
}
//0MS 340K
相關文章
- hdu 1241 Oil Deposits 深搜 Ac
- Elasticsearch 實現簡單搜尋Elasticsearch
- ElasticSearch 簡單的 搜尋 聚合 分析Elasticsearch
- HDU 4620 Fruit Ninja Extreme(搜尋)UIREM
- 【簡單搜尋】POJ 2251 Dugeon MasterAST
- DFS與BFS——理解簡單搜尋(中文虛擬碼+例題)
- HDU5348 MZL's endless loop (搜尋)OOP
- 自制簡單的詩歌搜尋系統
- 簡單檔案搜尋:Find Any File for MacMac
- SearchView的簡單使用和模擬搜尋View
- hdu5365 簡單幾何問題
- HDU4620 Fruit Ninja Extreme(搜尋+剪枝)UIREM
- EasyFind for Mac操作簡單的檔案搜尋工具Mac
- laravel 簡單限制搜尋引擎爬蟲頻率Laravel爬蟲
- BFS廣度優先搜尋(11)--hdu2102(基礎題)
- HDU 5469 Antonidas(樹上的字串匹配/搜尋)字串匹配
- Oil Deposits(DFS,基礎題)
- Meteor+MongoDB 實現簡單的即時搜尋MongoDB
- [LeetCode題解]79. 單詞搜尋LeetCode
- HDU 4422 The Little Girl who Picks Mushrooms(簡單題)OOM
- BFS廣度優先搜尋(4)--hdu2717(poj3278)(基礎題)
- 搜尋引擎面試題面試題
- 搜尋排序技術簡介排序
- DFS深度優先搜尋(3)--hdu2181(哈密頓繞行世界問題)(基礎題)
- 【問題】 檔案搜尋
- 簡單的揹包問題(入門)HDU2602 HDU2546 HDU1864
- 實戰:Nodejs+Mongodb+Elasticsearch 實現簡單的搜尋NodeJSMongoDBElasticsearch
- SimpleAISearch:C# + DuckDuckGo 實現簡單的AI搜尋AIC#Go
- ACM HDU 1279 驗證角谷猜想(簡單水題)ACM
- 20240713總結(搜尋專題,但是不想搜尋)
- 解決 PbootCMS 搜尋未搜尋到任何資料的問題boot
- 使用 JavaScript 實現簡單候選項推薦功能(模糊搜尋)JavaScript
- 【leetcode 簡單】 第六十八題 二叉搜尋樹的最近公共祖先LeetCode
- 洛谷題單指南-搜尋-P1433 吃乳酪
- hdu 3665Seaside(簡單floyd)IDE
- 文字獲取和搜尋引擎簡介
- Linux 搜尋檔案和資料夾的 4 種簡單方法Linux
- 海量資料搜尋---搜尋引擎