HDU1241(DFS求连通块)
来源:互联网 发布:上海it helpdesk招聘 编辑:程序博客网 时间:2024/04/30 14:02
Oil Deposits
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
8次DFS(可优化)
#include<iostream>#include<string>using namespace std;int n,m,ans;char a[105][105];bool area(int x,int y){ if(x>0&&x<n+1&&y>0&&y<m+1) return true; return false; }void dfs(int x,int y){ if(a[x][y]=='@'){ a[x][y]='*'; if(area(x+1,y)) dfs(x+1,y); if(area(x-1,y)) dfs(x-1,y); if(area(x,y+1)) dfs(x,y+1); if(area(x,y-1)) dfs(x,y-1); if(area(x+1,y+1)) dfs(x+1,y+1); if(area(x+1,y-1)) dfs(x+1,y-1); if(area(x-1,y+1)) dfs(x-1,y+1); if(area(x-1,y-1)) dfs(x-1,y-1); }}int main(){ while(cin>>n>>m&&n&&m){ ans=0; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ cin>>a[i][j]; } } for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(a[i][j]=='@') { ans++; dfs(i,j); } } } cout<<ans<<endl; } return 0;}
- HDU1241(DFS求连通块)
- HDU1241 DFS/BFS 求连通块
- hdu1241(dfs连通块)
- HDU1241 Oil Deposits —— DFS求连通块
- dfs求连通块(围棋 poj)
- 用DFS求连通块
- 用DFS求连通块
- hud1241-DFS求连通块
- C++ DFS求连通块
- UVA572 POJ1562 oil deposits(DFS求连通块问题)
- poj 1111 dfs(求连通块周长)
- POJ 2386(DFS 求连通块数量)
- AOJ 0118 Property Distribution(dfs 求连通块)
- UVA572油田问题(DFS求连通块)
- nyoj 27 水池数目(dfs求连通块)
- hdoj2952 数小羊(dfs求连通块)
- DFS:POJ1562-Oil Deposits(求连通块个数)
- Uva 572 Oil Deposits 油田 (DFS求连通块)
- Xcode调试之View Memory(查看内存)
- 白盒测试
- OSError: [Errno 1] Operation not permitted
- python爬虫基本数据类型
- 关于Spring中MongoTemplate.aggregate的一个奇异bug
- HDU1241(DFS求连通块)
- C开发经验3:如何避免重复包含头文件多次
- mybatis (十二) 逆向工程
- Sql_Server编程 简明教程
- Android的四层结构
- ,有一款RESTFUL接口的文档在线自动生成+功能测试功能软件——Swagger UI,具体配置过程可移步《Spring Boot 利用 Swagger 实现restful测试》
- JSON.NET VS BinaryFormatter 性能
- 蓝桥杯 2012 3 拼音字母
- spring boot 入门示例代码