HDU1241 简单的搜索 个人当作为简单模板
2016-11-12 21:11
411 查看
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
题解:给你一个油田的图,然后计算这个图的连通块。
从每个"@"格子出发.遍历它周围的"@"格子.每次访问一个格子的时候就给它标志一下就ok了,这样就可以在访问之前检查它是否有了编号。
下面附上自己随手画的最简单搜索题的流程图
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
题解:给你一个油田的图,然后计算这个图的连通块。
从每个"@"格子出发.遍历它周围的"@"格子.每次访问一个格子的时候就给它标志一下就ok了,这样就可以在访问之前检查它是否有了编号。
下面附上自己随手画的最简单搜索题的流程图
#include "stdio.h" #include "queue" #include "string.h" using namespace std; int map[105][105]; int n,m; struct node { int x; int y; }; int dir[8][2]={1,0,-1,0,0,1,0,-1,1,1,-1,-1,-1,1,1,-1}; void bfs(int x,int y) { queue<node>q; node now ,next; now.x=x; now.y=y; q.push(now);// 初始条件入队 map[now.x][now.y]=0; while (!q.empty())//队是否空 { now=q.front();//出队 q.pop(); // 此处判断是否满足最终条件 // 但是这一题并不需要找到最终对象 所以省略 for(int i=0;i<8;i++)//循环方向 { next.x=now.x+dir[i][0]; next.y=now.y+dir[i][1]; if(next.x>=0&&next.x<n&&next.y<m&&next.y>=0) { if(map[next.x][next.y])//满足条件入队 { map[next.x][next.y]=0; q.push(next); } } } } } int main() { while (scanf("%d%d",&n,&m)==2&&n) { char t; getchar(); int time =0; memset(map,0,sizeof(map)); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { scanf("%c",&t); if(t=='@') map[i][j]=1; } getchar (); } for(int i=0;i<n;i++)// 判断连通块 { for(int j=0;j<m;j++) { if(map[i][j]==1) { bfs(i,j); time++; } } } printf("%d\n",time ); } }
相关文章推荐
- hdu1241较简单的搜索
- jsp单页面数据库查询模板:支持分页、排序、简单搜索、设置每页显示页数功能
- 简单搜索模板
- 不要递给我花里胡俏的简历,给我一点简洁(不是简单)而能突出你自己的表达——面对成千上万的求职者,你真的以为HR会去看你那封100个人有99个差不多的求职信、和装订得像书本一样的简历模板内容吗?
- 采用mustache语言作为elasticsearch搜索请求的预处理模板
- jsp单页面数据库查询模板:支持分页、排序、简单搜索、设置每页显示页数功能
- 字符串作为freemarker模板的简单实现例子
- 字符串作为freemarker模板的简单实现例子
- 字符串作为freemarker模板的简单实现例子
- 专题 简单搜索(bfs+dfs) 个人题解
- 类模板作为函数参数以及作为类模板参数的一个简单示例
- hdu1241(最简单的搜索)
- java 字符串作为freemarker模板的简单实现例子
- DW表格的简单应用 之(个人简历模板)
- 简单ioc容器实现,可作为插件框架的核心逻辑,个人文档记录
- 搜索练习6/poj.org/problem3278 /Catch That Cow/简单的bfs模板
- 字符串作为freemarker模板的简单实现例子
- hdu1241 简单搜索dfs
- 个人模板 简单离散化
- 采用字符串作为模板内容的 Velocity简单实例。