poj2386(简单dfs)
2014-12-06 11:36
363 查看
就是求图中有多少个水洼。对图进行dfs遍历,并把是水洼的地方全部标记。然后从下一个是水哇的地方再进行dfs。
#include <cstdio> #include <iostream> #include <cstring> using namespace std; int graph[105][105]; int sum; bool vis[105][105]; int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1}; int dy[] = {-1, 0, 1, -1, 1, -1, 0 ,1}; int row, col; bool check(int x, int y) { if( x >= 0 && x < row && y >= 0 && y < col) return true; return false; } void dfs(int x, int y) { int ix, iy; for(int i = 0; i < 8; i++) { ix = x + dx[i]; iy = y + dy[i]; if(!vis[ix][iy] && check(ix, iy) && graph[ix][iy] == 1) { vis[ix][iy] = true; dfs(ix, iy); } } } int main() { //freopen("in.txt", "r", stdin); cin >> row >> col; getchar(); memset(vis, false, sizeof(vis)); char c; for(int i = 0; i < row; i ++) { for(int j = 0; j < col; j++) { c = getchar(); graph[i][j] = (c == '.') ? 0 : 1; } getchar(); } sum = 0; for(int i = 0; i < row; i ++) { for(int j = 0; j < col; j++) { if(graph[i][j] == 1 && vis[i][j] == false) { sum++; vis[i][j] = true; dfs(i, j); } } } cout << sum << endl; return 0; }
相关文章推荐
- poj2386 Lake Counting(简单DFS)
- poj2386 Lake Counting(简单DFS)
- poj2386----简单dfs,a一送一
- POJ2386-简单BFS/DFS
- HDU1241&POJ2386 dfs简单题
- 简单dfs--poj2386
- POJ2386 Lake Counting简单dfs
- hdu 1241 Oil Deposits 简单八个方向的DFS
- POJ1164 + USACO Section 2.1 The Castle - 简单的DFS...
- poj1573(简单的dfs)
- NYOJ 27水池的数目——简单的DFS
- UVA 572 - Oil Deposits(简单DFS)
- POJ 3051 Satellite Photographs 简单DFS
- dfs最简单的应用——输出全排列
- HDU 1010题解这是一道简单的DFS加回溯题,看懂后就会对递归和回溯有较深刻的理解。
- hdu 1342 Lotto (简单dfs)
- 【dfs找不在简单路径的点】ZOJ 10th Anniversary Contest C ZOJ 3583
- 一个DFS最简单的入门小程序
- POJ 2386 Lake Counting 简单的DFS搜索
- POJ 1111 Image Perimeters 简单DFS