poj2386(简单dfs)

就是求图中有多少个水洼。对图进行dfs遍历,并把是水洼的地方全部标记。然后从下一个是水哇的地方再进行dfs。

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 using namespace std;
 5 int graph[105][105];
 6 int sum;
 7 bool vis[105][105];
 8 int dx[] = {-1, -1, -1,  0, 0,  1, 1, 1};
 9 int dy[] = {-1,  0,  1, -1, 1, -1, 0 ,1};
10 int row, col;
11
12 bool check(int x, int y)
13 {
14     if( x >= 0 && x < row && y >= 0 && y < col)
15         return true;
16     return false;
17 }
18
19 void dfs(int x, int y)
20 {
21     int ix, iy;
22     for(int i = 0; i < 8; i++)
23     {
24         ix = x + dx[i];
25         iy = y + dy[i];
26         if(!vis[ix][iy] && check(ix, iy) && graph[ix][iy] == 1)
27         {
28             vis[ix][iy] = true;
29             dfs(ix, iy);
30         }
31     }
32 }
33 int main()
34 {
35     //freopen("in.txt", "r", stdin);
36     cin >> row >> col;
37     getchar();
38     memset(vis, false, sizeof(vis));
39
40     char c;
41     for(int i = 0; i < row; i ++)
42     {
43         for(int j = 0; j < col; j++)
44         {
45             c = getchar();
46             graph[i][j] = (c == ‘.‘) ? 0 : 1;
47         }
48         getchar();
49     }
50     sum = 0;
51
52     for(int i  = 0; i < row; i ++)
53     {
54         for(int j = 0; j < col; j++)
55         {
56             if(graph[i][j] == 1 && vis[i][j] == false)
57             {
58                 sum++;
59                 vis[i][j] = true;
60                 dfs(i, j);
61             }
62         }
63     }
64
65     cout << sum << endl;
66     return 0;
67 }
时间: 2024-10-09 16:52:06

poj2386(简单dfs)的相关文章

poj2386 Lake Counting(简单DFS)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=1562 ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢

hdu 1016 Prime Ring Problem (简单DFS)

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25700    Accepted Submission(s): 11453 Problem Description A ring is compose of n circles as shown in diagram. Put natural numb

Red and Black(简单dfs)

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12519    Accepted Submission(s): 7753 Problem Description There is a rectangular room, covered with square tiles. Each tile is color

ZOJ2165 简单DFS搜索

1 #include<iostream> 2 #include<cstring> 3 #include<cstdlib> 4 #include<algorithm> 5 #define MAXN 25 6 using namespace std; 7 int h,w; 8 int ans; 9 int dir[4][2]={-1,0,1,0,0,-1,0,1}; 10 char map[MAXN][MAXN]; 11 12 bool ok(int x,int

hdu 4739Zhuge Liang&#39;s Mines(简单dfs,需要注意重点)

Zhuge Liang's Mines Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1166    Accepted Submission(s): 505 Problem Description In the ancient three kingdom period, Zhuge Liang was the most famous

POJ 1979 Red and Black (简单dfs)

题目: 简单dfs,没什么好说的 代码: #include <iostream> using namespace std; typedef long long ll; #define INF 2147483647 int w,h; char a[22][22]; int dir[4][2] = {-1,0,1,0,0,-1,0,1}; int ans = 0; void dfs(int x,int y){ if(x < 0 || x >= h || y < 0 || y &g

POJ 1979 Red and Black(简单DFS)

Red and Black Description There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he

POJ1979 Red and Black (简单DFS)

POJ1979 Description There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can mo

uva 784 Maze Exploration(简单dfs)

这道题看上去很麻烦,什么迷宫啊,门之类的,其实挺简单的,就是让把与 * 连通的都置为 # 包括 * , 直接dfs就可以了,不过我wa了好多次...最后竟然是多读了一个换行,忘了加getchar()了,gets()函数 会把缓冲区里面的换行给读进去的...应该把换行去掉,血的教训啊... 代码: #include<stdio.h> #include<string.h> #include<stdlib.h> int dx[4]={-1,0,0,1}; int dy[4]=