hdoj1241Oil Deposits
来源:互联网 发布:手指算法图解 编辑:程序博客网 时间:2024/06/08 14:03
Oil Deposits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 30185 Accepted Submission(s): 17462
Problem DescriptionThe 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.
InputThe 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.
OutputFor 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 Input1 1*3 5*@*@***@***@*@*1 8@@****@*5 5 ****@*@@*@*@**@@@@*@@@**@0 0
Sample Output0122经典的深搜代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char s[101][101];
int n,m;
void dfs(int x,int y)
{
int tx,ty;
int next[8][2]={{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}};
s[x][y]='*';
for(int k=0;k<=7;k++)
{
tx=x+next[k][0];
ty=y+next[k][1];
if(tx<1||ty<1||tx>n||ty>m)
continue;
if(s[tx][ty]=='@')
{
dfs(tx,ty);
}
}
return ;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF&&(n!=0||m!=0))
{
memset(s,0,sizeof(s));
getchar();
int sum=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
scanf("%c",&s[i][j]);
getchar();
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(s[i][j]=='@')
{
dfs(i,j);
sum++;
}
}
}
printf("%d\n",sum);
}
return 0;
}
1 1*3 5*@*@***@***@*@*1 8@@****@*5 5 ****@*@@*@*@**@@@@*@@@**@0 0
0122经典的深搜代码:
0 0
- hdoj1241Oil Deposits
- hdoj1241Oil Deposits
- HDOJ1241Oil Deposits(DFS)
- poj1562&&hdoj1241Oil Deposits
- hdoj1241Oil Deposits bfs|dfs
- hdoj1241Oil Deposits(DFS)
- hdoj1241Oil Deposits(BFS)
- Oil Deposits
- Oil Deposits
- Oil Deposits
- Oil Deposits
- poj_1562Oil Deposits
- Oil Deposits
- hdu1241Oil Deposits
- Oil Deposits
- Oil Deposits
- Oil Deposits
- Oil Deposits
- ListView adapter getView 报错: addView(View, LayoutParams) is not supported in AdapterView
- 《现代操作系统(中文第三版)》课后习题——第三章 存储管理
- C++ 两个有趣的问题(静态动态绑定,和类指针指向空的实质)
- http协议基础知识总结
- 《Java虚拟机原理图解》5. JVM类加载器机制与类加载过程
- hdoj1241Oil Deposits
- ffmpeg源码简析(二)av_register_all(),avcodec_register_all()
- 深入理解mysqldump参数 --single-transaction --lock-all-tables
- 利用FRIDA攻击Android应用程序(一)
- git 设置别名
- MyBatis初识
- jmeter脚本录制
- C++学习——虚继承中半菱形继承
- AppCompatActivity和Activity的区别