HDU-1241-Oil Deposits
来源:互联网 发布:telnet连接到80端口 编辑:程序博客网 时间:2024/06/06 02:03
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241
Oil Deposits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 31803 Accepted Submission(s): 18467
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
分析:输入m行n列的地图,“@”为地图,“*”为没有油的地方,遇到@就搜索所有与之相连的@然后将其置成*,遍历整个图看看搜索了多少次,即连通块的数量。
DFS代码如下:
#include<iostream>using namespace std;int m,n;char mp[101][101];int dir[8][2]={{-1,0},{1,0},{0,-1},{0,1},{1,-1},{1,1},{-1,1},{-1,-1}};bool check(int x,int y){if(x>=1&&x<=m&&y>=1&&y<=n&&mp[x][y]=='@')return 1;elsereturn 0;}void dfs(int sx,int sy){for(int i=0;i<8;i++){int xx=sx+dir[i][0];int yy=sy+dir[i][1];if(check(xx,yy)){mp[xx][yy]='*';dfs(xx,yy);}}}int main(){while(cin>>m>>n){int sum=0;if(m==0&&n==0)break;for(int i=1;i<=m;i++) { for(int j=1;j<=n;j++) {cin>>mp[i][j]; } } for(int i=1;i<=m;i++) { for(int j=1;j<=n;j++) {if(mp[i][j]=='@'){mp[i][j]='*';dfs(i,j);sum++;} } } cout<<sum<<endl;}return 0;}
BFS代码如下:
#include<iostream>#include<queue>using namespace std;char mp[101][101];int dir[8][2]={{0,1},{0,-1},{1,-1},{-1,-1},{1,0},{-1,0},{-1,1},{1,1}};int m,n,sum;struct node{int x,y;}; bool check(node t){if(t.x>=1&&t.x<=m&&t.y>=1&&t.y<=n&&mp[t.x][t.y]=='@')return 1;elsereturn 0;}void bfs(int sx,int sy){queue<node>q;node now,next;now.x=sx;now.y=sy;q.push(now);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(check(next)){mp[next.x][next.y]='*';q.push(next);}}}}int main(){while(cin>>m>>n){if(m==0&&n==0)break;sum=0;for(int i=1;i<=m;i++){for(int j=1;j<=n;j++){cin>>mp[i][j];}}for(int i=1;i<=m;i++){for(int j=1;j<=n;j++){if(mp[i][j]=='@'){sum++;mp[i][j]='*';//消除同一连通块的所有标记 bfs(i,j);}}}cout<<sum<<endl;}return 0;}
阅读全文
0 0
- 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
- hdu 1241 Oil Deposits
- HDU 1232 畅通工程【并查集】
- SW板子 hdu3691(全局最小割 o(n^3)
- vue中的懒加载和按需加载
- OpenCV Python 图像的载入、显示和保存
- 复杂链表的构成与复制
- HDU-1241-Oil Deposits
- Java基础之自增(++)和自减(--)运算符
- PAT甲级 1039
- 机器学习实战【2】(决策树ID3算法)
- DSP的C/C++语言程序设计
- Android中字符串的拆分split方法
- Educational Codeforces Round 25
- 深入Java集合学习系列:HashMap的实现原理
- python 常用函数