连通分支
来源:互联网 发布:王夫之的知行 编辑:程序博客网 时间:2024/06/05 06:06
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
dfs:
#include<iostream>#include<cstring># define Maxn 110using namespace std;char mp[Maxn][Maxn];int vis[Maxn][Maxn];int cou,m,n;int dx[]={0,0,1,-1,-1,-1,1,1};int dy[]={-1,1,0,0,-1,1,-1,1};int judge(int x,int y){ return x>=0&&y>=0&&x<m&&y<n;}void dfs(int x,int y){ if(!vis[x][y]) vis[x][y]=1; int flag=0; for(int i=0;i<8;i++){ int tx=x+dx[i],ty=y+dy[i]; if(judge(tx,ty)&&!vis[tx][ty]) if(mp[tx][ty]=='*') vis[tx][ty]=1;//dfs停止,标注该点已搜索 else {flag--;dfs(tx,ty);} flag++; } if(flag==8&&mp[x][y]=='@') cou++;}int main(){ while(cin>>m>>n,m){ for(int i=0;i<m;i++) for(int j=0;j<n;j++) cin>>mp[i][j]; cou=0; memset(vis,0,sizeof vis); for(int i=0;i<m;i++) for(int j=0;j<n;j++) if(!vis[i][j]) dfs(i,j); cout<<cou<<endl; } return 0;}
bfs:
#include<iostream>#include<cstring>#include <algorithm>#include<queue>#include<vector># define Maxn 110#define MP(a, b) make_pair(a, b)using namespace std;typedef pair<int, int> pii;int m,n,cou;char mp[Maxn][Maxn];int vis[Maxn][Maxn];int dx[]={0,0,1,-1,-1,-1,1,1};int dy[]={-1,1,0,0,-1,1,-1,1};int judge(int x,int y){ return x>=0&&y>=0&&x<m&&y<n;}void bfs(int x,int y){ vis[x][y]=1; queue<pii> p; p.push(MP(x,y)); while(!p.empty()){ pii nv=p.front();p.pop(); for(int i=0;i<8;i++){ int tx=nv.first+dx[i],ty=nv.second+dy[i]; if(judge(tx,ty)&&mp[tx][ty]=='@'&&vis[tx][ty]==0) {vis[tx][ty]=1;p.push(MP(tx,ty));} } }}int main(){ char to; while(cin>>m>>n,m){ for(int i=0;i<m;i++) for(int j=0;j<n;j++) cin>>mp[i][j]; cou=0; memset(vis,0,sizeof vis); for(int i=0;i<m;i++) for(int j=0;j<n;j++) if(mp[i][j]=='@'&&vis[i][j]==0) {cou++;bfs(i,j);} cout<<cou<<endl; } return 0;}注:非常简单的搜索题。
0 0
- 连通分支
- 4.3 连通分支
- 强连通分支
- POJ_1236 强连通分支
- 强连通分支算法
- 图论--强连通分支
- 强连通分支
- 强连通分支
- 连通图的强连通分支
- 算法之强连通分支
- 图论 极大强连通分支
- hdoj1269(强连通分支Tarjan)
- 无向图连通分支
- 图算法之强连通分支
- 寻找强连通分支C语言
- POJ2553解题报告 强连通分支
- POJ2186 强连通分支(Strongly_Connected_Components)
- poj1236 强连通分支+缩点
- ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务的解决方法
- LeetCode主题整理(2)树问题和相关问题
- Remove Element 删除元素
- 剑指XX游戏(九) - C/C++必知必会
- 低成本开始互联网创业:探讨域名、服务器、CDN、邮箱等节流之道
- 连通分支
- LA 3027 Corporative Network / 并查集
- 用OpenSSL编写SSL,TLS程序 - Win32版
- smbmount命令
- CentOS 6.4 安装 Emacs 24.3
- POJ 3177(双联通分量)
- 黑马程序员_StringBuffer
- (java)正确使用volatile变量(managing volatility)
- 黑马程序员-文件操作