首页 > 代码库 > hdu1241
hdu1241
英语太菜,罚自己翻译题目:
GeoSurvComp地质调查公司负责探测地下石油储量。GeoSurvComp公司在一段时间内在一大块矩形区域内工作,创造出一个网格把土地分成很多方块。如果每一方块单独分析,使用感应设备去测定方块中是否含有石油。含有油的方块称为口袋。如果两个口袋是相邻的,那它们属于同一个油床。油床可以相当大,可以包含众多口袋。你的任务就是测定出有多少不同的油床。
输入:
输入文件包括一个或多个网格。每一个网格输入的第一行含有m和n,分别代表网格的横坐标和纵坐标,被一个空格分开。如果m=0则标志着输入结束;否则1<=m<=100并且1<=n<=100。接下来输入m行n列个网格特征(不记录换行符)。每一个特征值跟一个方块相对应,并且要么是*(代表没油),要么是@代表一个石油口袋。
输出:
对于每一个方块,输出不同的油田数。两个不同的口袋属于同一个油田,如果它们垂直水平或对角相邻。一个油田包含少于100个口袋。
这是我的第一道搜索题,看了别人的代码才写出来的,跟大神的很像
Oil Deposits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11533 Accepted Submission(s): 6713
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
0122
#include<iostream>#include<cstdlib>#include<cstdio>#include<memory.h>using namespace std;char map[101][101];bool visit[101][101];int sum;int n,m;int dir[8][2]={-1,-1,-1,0,-1,1,0,-1,0,1,1,-1,1,0,1,1};void dfs(int i,int j){ visit[i][j]=false; for(int k=0;k<8;k++) { int ii,jj; ii=i+dir[k][0]; jj=j+dir[k][1]; if(map[ii][jj]==‘@‘&&ii>=0&&ii<n&&jj>=0&&jj<m&&visit[ii][jj]) dfs(ii,jj); }}int main(){ int i,j; while(scanf("%d%d%*c",&n,&m)!=EOF) { if(m==0) break; for(i=0;i<n;i++) scanf("%s",map[i]); memset(visit,true,sizeof(visit)); sum=0; for(i=0;i<n;i++) for(j=0;j<m;j++) { if(map[i][j]==‘@‘&&visit[i][j]) { sum++; dfs(i,j); } } cout<<sum<<endl; } return 0;}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。