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 4620 Fruit Ninja Extreme(搜尋)UIREM
- HDU1560,迭代加深搜尋
- hdu 1175 連連看 搜尋
- Elasticsearch 實現簡單搜尋Elasticsearch
- 單詞搜尋問題
- 百度簡單搜尋PC版玩法攻略 簡單搜尋有電腦版嗎?
- HDU 6415(dp/記憶化搜尋)
- hdu 1728 逃離迷宮 搜尋
- 【簡單搜尋】POJ 2251 Dugeon MasterAST
- ElasticSearch 簡單的 搜尋 聚合 分析Elasticsearch
- POJ - 2236 Wireless Network (kuangbin - 簡單搜尋)
- DFS與BFS——理解簡單搜尋(中文虛擬碼+例題)
- 簡單的揹包問題(入門)HDU2602 HDU2546 HDU1864
- 簡單檔案搜尋:Find Any File for MacMac
- 自制簡單的詩歌搜尋系統
- [LeetCode題解]79. 單詞搜尋LeetCode
- 單詞搜尋
- laravel 簡單限制搜尋引擎爬蟲頻率Laravel爬蟲
- EasyFind for Mac操作簡單的檔案搜尋工具Mac
- SimpleAISearch:C# + DuckDuckGo 實現簡單的AI搜尋AIC#Go
- 20240713總結(搜尋專題,但是不想搜尋)
- 79. 單詞搜尋
- 【leetcode 簡單】 第六十八題 二叉搜尋樹的最近公共祖先LeetCode
- 洛谷題單指南-搜尋-P1433 吃乳酪
- 搜尋排序技術簡介排序
- LeetCode-079-單詞搜尋LeetCode
- PTA搜尋專題部分題解
- HDU100題簡要題解(2080~2089)
- Linux 搜尋檔案和資料夾的 4 種簡單方法Linux
- 【Leetcode 346/700】79. 單詞搜尋 【中等】【回溯深度搜尋JavaScript版】LeetCodeJavaScript
- solr搜尋之搜尋精度問題我已經盡力了!!!Solr
- 解決 PbootCMS 搜尋未搜尋到任何資料的問題boot
- 洛谷題單指南-搜尋-P1162 填塗顏色
- 文字獲取和搜尋引擎簡介
- HDU 2612 Find a way (廣搜)
- 最佳路徑搜尋(二):啟發式搜尋(代價一致搜尋(Dijkstra search),貪心搜尋,A*搜尋)
- 洛谷題單指南-搜尋-P2036 [COCI 2008/2009 #2] PERKET
- 記憶搜尋解救滑雪問題
- [ZJOI2019] Minimax搜尋 題解