Depth First Search (DFS)入门
来源:互联网 发布:搜索引擎优化外包 编辑:程序博客网 时间:2024/05/25 19:57
Oil Deposits hdu1241
#include<iostream>using namespace std; int n,m; const int maxn=100+5; char s[maxn][maxn]; int dir[8][2]= { 1,1, -1,1, 1,0, -1,0, 1,-1, -1,-1, 0,-1, 0,1 }; void dfs(int x,int y) { s[x][y]='*'; for(int i=0;i<8;i++) { int tx=x+dir[i][0],ty=y+dir[i][1]; if(tx<0||tx>=n||ty<0||ty>=m)continue; if(s[tx][ty]=='@')dfs(tx,ty); } return ; }int main(){ while(cin>>n>>m&&m) { int ans=0; for(int i=0;i<n;i++) for(int j=0;j<m;j++) cin>>s[i][j]; for(int i=0;i<n;i++) for(int j=0;j<m;j++) { if(s[i][j]=='@') { dfs(i,j); ans++; } } cout<<ans<<endl; } return 0;}
HDu1312Red and Black
直接贴代码了:
#include <bits/stdc++.h>using namespace std;const int maxn=100000+10;char s[maxn/1000][maxn/1000];int n,m;int derict[4][2]={ -1,0, 1,0, 0,1, 0,-1,};int ans;bool flag[maxn/1000][maxn];void dfs(int x,int y){ for(int i=0;i<4;i++) { int xx=x+derict[i][0]; int yy=y+derict[i][1]; if(xx>=0 && xx<n && yy>=0 && yy<m && flag[xx][yy] && s[xx][yy] == '.') { ans++; flag[xx][yy] = false; dfs(xx,yy); } } return ;}int main(){ while(~scanf("%d%d",&m,&n) && (n || m)) { memset(flag , true ,sizeof(flag)); for(int i=0;i<n ;i++) scanf("%s",s[i]); ans=0; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(s[i][j] == '@') { flag[i][j] = false; ans++; dfs(i,j); break; } } } cout<<ans<<endl; } return 0;}
#include <bits/stdc++.h>using namespace std;const int maxn=100000+10;char s[maxn/1000][maxn/1000];int n,m;int derict[4][2]={ -1,0, 1,0, 0,1, 0,-1,};int ans;void dfs(int x,int y){ for(int i=0;i<4;i++) { int xx=x+derict[i][0]; int yy=y+derict[i][1]; if(xx>=0 && xx<n && yy>=0 && yy<m && s[xx][yy] == '.') { ans++; s[xx][yy ] = '*'; dfs(xx,yy); } } return ;}int main(){ while(~scanf("%d%d",&m,&n) && (n || m)) { for(int i=0;i<n ;i++) scanf("%s",s[i]); ans=0; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(s[i][j] == '@') { s[i][j]= '*'; ans++; dfs(i,j); break; } } } cout<<ans<<endl; } return 0;}Lake Counting poj2386
//#include <bits/stdc++.h>#include<iostream>#include<cstdio>using namespace std;const int maxn=100000+10;char s[maxn/1000][maxn/1000];int n,m;int derict[8][2]={ -1,0, 1,0, 0,1, 0,-1, 1,1, 1,-1, -1,1, -1,-1,};void dfs(int x,int y){ for(int i=0;i<8;i++) { int xx=x+derict[i][0]; int yy=y+derict[i][1]; if(xx>=0 && xx < n&& yy>=0 && yy<m && s[xx][yy] == 'W') { s[xx][yy] = '.'; dfs(xx,yy); } } return ;}int main(){ while(~scanf("%d%d",&n,&m)) { int 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(s[i][j] == 'W') { s[i][j]= '.'; dfs(i,j); ans++; } } } cout<<ans<<endl; } return 0;}
小X学游泳 百炼10141
#include <bits/stdc++.h>//#include<iostream>//#include<cstdio>using namespace std;const int maxn=100000+10;char s[maxn/1000][maxn/1000];int temp;char tempc;int n,m;int derict[4][2]={ -1,0, 1,0, 0,-1, 0,1,};bool flag[maxn/1000][maxn/1000];void dfs(int x,int y){ for(int i=0;i<4;i++) { int xx=x+derict[i][0]; int yy=y+derict[i][1]; if(xx>=0 && xx<n && yy>=0 && yy < m && flag[xx][yy] && s[xx][yy ] == tempc) { flag[xx][yy] = false; temp++; dfs(xx,yy); } }}int main(){ while(~scanf("%d%d",&n,&m)) { int ans=0; memset(flag,true ,sizeof(flag)); 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(flag[i][j]) { temp=1; tempc=s[i][j]; flag[i][j] = false; dfs(i,j);// cout<<i+1<<" "<<j+1<<" "<<temp<<" "<<tempc<<endl; if(temp > ans) ans = temp; } } } cout<<ans<<endl; } return 0;}
#include<iostream>using namespace std;const int maxn=100+5;char map[maxn][maxn];int dir[4][2]={ 1,0,-1,0,0,-1,0,1};int n,m;int ans;void dfs(int x,int y,int k){ map[x][y]=0; for(int i=0;i<4;i++) { int dx=x+dir[i][0]; int dy=y+dir[i][1]; if(dx>=0&&dx<n&&dy>=0&&dy<m&&map[dx][dy]==k) { ans++; dfs(dx,dy,k); //cout<<ans<<endl; } }}int main(){ while(cin>>n>>m) { for(int i=0;i<n;i++) cin>>map[i]; int maxm=0; for(int i=0;i<n;i++) for(int j=0;j<m;j++) { ans=0; if(map[i][j]) { ans++;dfs(i,j,map[i][j]);} //cout<<ans<<endl; if(ans>maxm) maxm=ans; } cout<<maxm<<endl; } return 0;}
0 0
- Depth First Search (DFS)入门
- DFS(Depth-First-Search)
- Tree-DFS(Depth-First-Search)
- Depth-First-Search(DFS伪代码)
- 经典dfs(depth-first search)
- Depth First Search (DFS) | Iterative & Recursive Implementation
- 深度优先搜索(Depth-First-Search,DFS)
- 深度优先搜索Depth-first search (DFS) for undirected graphs
- DFS --- Depth First Search 深度优先搜索算法
- 深度优先搜索DFS(depth first search),拓扑排序
- 图的深度优先搜索(Depth First Search,DFS)
- 利用DFS拆分数系列;Depth First Search
- 5.3.2 深度优先搜索(Depth-First-Search,DFS)
- DFS-深度优先搜索(Depth First Search)—1
- [数据结构]深度优先搜索算法(Depth-First-Search,DFS)
- TensorFlow中的深度优先搜索(Depth-first search, DFS)
- DFS——深度优先算法(Depth First Search)
- Adjacency List表示下Depth-First-Search(DFS)及Breadth-First-Search(BFS)的c++实现
- 排序-堆排序
- Android加载大图片方法-避免oom错误
- 记事本
- Leetcode no. 335
- HDU 1069
- Depth First Search (DFS)入门
- 算法: 快速算法
- CentOS7开启和关闭自动下载更新
- <<unix/linux programming>>学习标记整理
- Android大牛博客地址集合
- react-native 手势应答系统
- C语言中的static作用
- 分析型数据库 ADS 报错 Query misses join index
- 爱情数字(蘑菇街在线笔试)