POJ 2386 Lake Counting(DFS)

题意:有一个大小为N×M的园子,雨后积起了水。八连通的积水被认为是连在一起的。求园子里一共有多少水洼?

* * *

* W*    (八连通指的就是左图中相对W的*的部分)

* * *

Sample Input

10 12
W........WW.
.WWW.....WWW
....WW...WW.
.........WW.
.........W..
..W......W..
.W.W.....WW.
W.W.W.....W.
.W.W......W.
..W.......W.

Sample Output

3PS:样例中的3个水洼分别是在左上,左下,和右方。这是的一道入门的宽度优先搜索题,很简单~~从任意的W开始,不停地把邻接的部分(八连通)用‘.‘代替。1次DFS后与初始的这个W连接的所有W就都被替换成了‘.‘,因此知道图中不在存在W为止,总共进行DFS的次数就是答案了。8个方向的共对应了8种状态转移,每个格子作为DFS的参数至多被调用一次,所以复杂度为O(8×M×N)=O(M×N)。
#include<iostream>
#include<algorithm>
using namespace std;
char field[105][105];
int N,M;
void dfs(int x, int y)
{
    //现在的位置(x,y)
    field[x][y] = ‘.‘;//将现在所在位置替换为‘.‘
    for (int dx = -1; dx <= 1; dx++)//循环遍历连通的8个方向
    {
        for (int dy = -1; dy <= 1; dy++)
        {
            int nx = x + dx, ny = y + dy;//向x方向移动dx,向y方向移动dy,移动的结果为(nx,ny)
            if (0 <= nx&&nx <= N && 0 <= ny&&ny <= M&&field[nx][ny] == ‘W‘)//判断(nx,ny)是不是在园子里,以及是否有积水
                dfs(nx, ny);
        }
    }
}
int main()
{
    cin >> N >> M;
    for (int i = 0; i < N; i++)
        for (int j = 0; j < M; j++)
            cin >> field[i][j];
    int res = 0;
    for(int i=0;i<N;i++)
        for(int j=0;j<M;j++)
            if (field[i][j] == ‘W‘)//从有积水的地方开始dfs
            {
                dfs(i, j);
                res++;
            }
    cout << res << endl;
    return 0;
}
 
				
时间: 2024-07-28 23:44:11

POJ 2386 Lake Counting(DFS)的相关文章

POJ 2386 Lake Counting(bfs解法)

Description Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water ('W') or dry land ('.'). Farmer

POJ 2386 Lake Counting 搜索题解

简单的深度搜索就可以了,看见有人说什么使用并查集,那简直是大算法小用了. 因为可以深搜而不用回溯,故此效率就是O(N*M)了. 技巧就是增加一个标志P,每次搜索到池塘,即有W字母,那么就认为搜索到一个池塘了,P值为真. 搜索过的池塘不要重复搜索,故此,每次走过的池塘都改成其他字母,如'@',或者'#',随便一个都可以. 然后8个方向搜索. #include <stdio.h> #include <vector> #include <string.h> #include

POJ 1699 Best Sequence(DFS)

題目鏈接 題意 : 將幾個片段如圖所示方法縮成一個序列,求出最短這個序列. 思路 : 其實我也不知道怎麼做.....看網上都用了DP.....但是我不會.....這個DP不錯,還有用KMP+状压DP做的 1 //1699 2 #include <iostream> 3 #include <stdio.h> 4 #include <string.h> 5 #include <string> 6 7 using namespace std; 8 9 string

POJ 1856 Sea Battle(dfs)

Description During the Summit, the armed forces will be highly active. The police will monitor Prague streets, the army will guard buildings, the Czech air space will be full of American F-16s. Moreover, the ships and battle cruisers will be sent to

【POJ - 2386】Lake Counting (dfs+染色)

-->Lake Counting 直接上中文了 Descriptions: 由于近日阴雨连天,约翰的农场中中积水汇聚成一个个不同的池塘,农场可以用 N x M (1 <= N <= 100; 1 <= M <= 100) 的正方形来表示.农场中的每个格子可以用'W'或者是'.'来分别代表积水或者土地,约翰想知道他的农场中有多少池塘.池塘的定义:一片相互连通的积水.任何一个正方形格子被认为和与它相邻的8个格子相连. 给你约翰农场的航拍图,确定有多少池塘 Input Line 1

《挑战》2.1 POJ 2386 Lake Counting (简单的dfs)

1 # include<cstdio> 2 # include<iostream> 3 4 using namespace std; 5 6 # define MAX 123 7 8 char grid[MAX][MAX]; 9 int nxt[8][2] = { {1,0},{0,-1},{-1,0},{0,1},{1,1},{1,-1},{-1,1},{-1,-1} }; 10 int n,m; 11 12 int can_move( int x,int y ) 13 { 14

poj 2386 Lake Counting

Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24578   Accepted: 12407 Description Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <= 10

POJ 3009-Curling 2.0(DFS)

Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12158   Accepted: 5125 Description On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is

POJ 2251 Dungeon Master(dfs)

Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot m