hdu 1241 Oil Desposits
来源:互联网 发布:时时彩服务器源码 编辑:程序博客网 时间:2024/06/05 16:31
Oil Deposits
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
---------------------------------------------------
#include<stdio.h>
#include<string.h>
int row,col;
char map[110][110];
int visited[110][110];
bool check(int x,int y)
{
if(x>=0 && x<row && y>=0 && y<col)
return true;
return false;
}
void dfs(int x,int y)
{
if(map[x][y]=='*'||visited[x][y]||check(x,y)==false)
return ;
visited[x][y] = 1;
dfs(x-1,y-1); dfs(x-1,y); dfs(x-1,y+1);
dfs(x,y-1); dfs(x,y+1);
dfs(x+1,y-1); dfs(x+1,y); dfs(x+1,y+1);
}
int main()
{
int i,j;
int cont;
while(scanf("%d%d%*c",&row,&col)&&row)
{
memset(map,0,sizeof(map));
for(i=0;i<row;i++)
{
scanf("%s",map[i]);
}
memset(visited,0,sizeof(visited));
cont=0;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(!visited[i][j] && map[i][j]=='@')
{
cont++;
dfs(i,j);
}
}
}
printf("%d\n",cont);
}
return 0;
}
#include<string.h>
int row,col;
char map[110][110];
int visited[110][110];
bool check(int x,int y)
{
if(x>=0 && x<row && y>=0 && y<col)
return true;
return false;
}
void dfs(int x,int y)
{
if(map[x][y]=='*'||visited[x][y]||check(x,y)==false)
return ;
visited[x][y] = 1;
dfs(x-1,y-1); dfs(x-1,y); dfs(x-1,y+1);
dfs(x,y-1); dfs(x,y+1);
dfs(x+1,y-1); dfs(x+1,y); dfs(x+1,y+1);
}
int main()
{
int i,j;
int cont;
while(scanf("%d%d%*c",&row,&col)&&row)
{
memset(map,0,sizeof(map));
for(i=0;i<row;i++)
{
scanf("%s",map[i]);
}
memset(visited,0,sizeof(visited));
cont=0;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(!visited[i][j] && map[i][j]=='@')
{
cont++;
dfs(i,j);
}
}
}
printf("%d\n",cont);
}
return 0;
}
0 0
- hdu 1241 Oil Desposits
- HDU 1241Oil Deposits
- HDU---1241Oil Deposits
- Hdu 1241 Oil Deposits
- Hdu 1241 - Oil Deposits
- HDU-1241:Oil Deposits
- hdu 1241 Oil Deposits
- hdu 1241 Oil Deposits
- hdu 1241 Oil Deposits
- hdu - 1241 - Oil Deposits
- HDU-1241Oil Deposits
- hdu 1241 Oil Deposits
- hdu 1241 Oil Deposits
- HDU 1241 Oil Deposits
- hdu 1241 Oil Deposits
- hdu 1241 Oil Deposits
- HDU 1241 Oil Deposits
- hdu-1241-Oil Deposits
- Linux 文本处理三剑客之sed命令详解
- PHP:产生不重复随机数的方法
- Minimal Window Search/PVS
- string_agg
- Transposition Table
- hdu 1241 Oil Desposits
- Android MediaStore裁剪图片3
- poj 1182 食物链 //带权并查集
- gdb相关
- C#把list转化为Datatable
- Specified VM install not found: type Standard VM, name jdk1.x
- Photoshop脚本 > 批量生成各尺寸的iOS图标
- smarty语法
- 编译器的工作过程