首页 > 代码库 > hdu 1204 深搜
hdu 1204 深搜
#include <iostream>
#include <string>
#define MAX 110
#define OIL true
#define BLANK false
using namespace std;
bool oil_map[MAX][MAX];
int dir_map[8][2]={ {1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1} };
void set_oil_map()
{
memset(oil_map,BLANK,sizeof(oil_map) );
}
void input_oil_map(int x,int y)
{
for(int i=1 ;i<=x ;i++)
{
string temp;
cin>>temp;
for(int j=1 ;j<=y ;j++)
{
if(temp[j-1]==‘@‘ )
oil_map[i][j]=OIL;
}
}
}
void search_oil_map(int x ,int y)
{
//if(oil_map[x][y]==OIL )
oil_map[x][y]=BLANK;
int search_x,search_y;
for(int i=0 ;i<8 ;i++)
{
search_x=x+dir_map[i][0];
search_y=y+dir_map[i][1];
if(oil_map[search_x][search_y]==OIL )
search_oil_map(search_x ,search_y );
}
}
int main()
{
int n,m;
while(cin>>m>>n &&(m+n)!=0 )
{
set_oil_map();
input_oil_map(m,n);
int count=0;
for(int i=1 ;i<=m ;i++)
for(int j=1 ;j<=n ;j++)
{
if(oil_map[i][j]==OIL )
{
search_oil_map(i,j);
count++;
}
}
cout<<count<<endl;
}
return 0;
}
/*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.
*/
来自为知笔记(Wiz)
附件列表
hdu 1204 深搜
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。