【HDU】 1241 Oil Deposits
2016-01-19 09:32
316 查看
Oil Deposits
[b]Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 20535 Accepted Submission(s): 11816
[/b]
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
1 1 * 3 5 *@*@* **@** *@*@* 1 8 @@****@* 5 5 ****@ *@@*@ *@**@ @@@*@ @@**@ 0 0
[align=left]Sample Output[/align]
0 1 2 2
题解:Floodfill裸题。
#include <iostream> #include <cstring> #include <cstdio> #include <queue> using namespace std; struct node { int x,y; }; int way[8][2]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}}; int n,m,flag[105][105],ans; char s[105][105]; queue<node> q; int check(int x,int y) { return x>=0 && x<n && y>=0 && y<m && s[x][y]=='@' && !flag[x][y]; } void bfs(int x,int y) { node r,t; t.x=x; t.y=y; flag[x][y]=1; q.push(t); while (!q.empty()) { t=q.front(); q.pop(); for (int i=0;i<8;i++) { r.x=t.x+way[i][0]; r.y=t.y+way[i][1]; if (check(r.x,r.y)) { q.push(r); flag[r.x][r.y]=1; } } } } int main() { while (scanf("%d%d",&n,&m),n&&m) { memset(flag,0,sizeof(flag)); memset(s,0,sizeof(s)); ans=0; for (int i=0;i<n;i++) scanf("%s",s[i]); for (int i=0;i<n;i++) for (int j=0;j<m;j++) if (check(i,j)) { bfs(i,j); ans++; } printf("%d\n",ans); } }
相关文章推荐
- 嵌入式学习推荐大家读得书
- bzoj3504: [Cqoi2014]危桥
- 一个关于1到100之间和与积的数学题
- 86.Xcode配置.pch文件
- 集算器协助java处理多样性数据源之HDFS
- 01-语言入门-01-A+B Problem
- 使用WebView显示页面
- openstack云计算札记
- LeetCode OJ 1. Two Sum
- ajax多个数值传递
- bzoj1066: [SCOI2007]蜥蜴
- //普通字符串转JSON字符串转字典
- CocoaPods的安装使用和常见问题
- jstl标签
- RFID数据传输常用编码格式
- 笛卡尔积算法的Java实现
- php性能优化
- POJ-魔兽世界之一:备战
- ROM/RAM/DRAM/SRAM/FLASH区别联系总结
- MongoDB的本地化排序