POJ 2386 DFS深搜入门


Time Limit: 1000MS


Memory Limit: 65536K

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 John would like to figure out how many ponds have formed in his field. A pond is a connected set of squares with water in them, where a square is considered adjacent to all eight of its neighbors.

Given a diagram of Farmer John‘s field, determine how many ponds he has.

Input

* Line 1: Two space-separated integers: N and M

* Lines 2..N+1: M characters per line representing one row of Farmer John‘s field. Each character is either ‘W‘ or ‘.‘. The characters do not have spaces between them.

Output

* Line 1: The number of ponds in Farmer John‘s field.

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

3

Hint

OUTPUT DETAILS: There are three ponds: one in the upper left, one in the lower left,and one along the right side.

解题思路

简单的DFS入门,写好之后一直WA,然后发现scanf的一个注意事项之前一直没注意,cin读入字符会自动忽略换行符和空格,但是scanf不会,所以涉及到换行和空格的时候要用getchar()跳过换行符和空格。

AC代码

#include<cstdio>
#include<cstring>
using namespace std;

const int N = 105;
char map[N][N];
int vis[N][N];//访问标记
int n, m;
int dx[8] = { -1,0,1,-1,1,-1,0,1 };
int dy[8] = { 1,1,1,0,0,-1,-1,-1 };//结点周边从上到下,从左到右八个点
int ans = 0;//湖数

bool valid(int x, int y)
{
    return (x >= 0 && y >= 0 && x < n && y < m);
}

void DFS(int x, int y)
{
    vis[x][y] = 1;//已访问
    for (int i = 0; i < 8; i++)
    {
        int newx = x + dx[i];
        int newy = y + dy[i];
        if (valid(newx, newy))
        {
            if (map[newx][newy] == ‘W‘&&vis[newx][newy] == -1) DFS(newx, newy);
        }
    }
}

int main()
{
    memset(vis, -1, sizeof(vis));
    scanf("%d%d", &n, &m);
    getchar();
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            scanf("%c", &map[i][j]);
        }
        getchar();
    }
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (map[i][j] == ‘W‘&&vis[i][j] == -1)
            {
                ans++;
                DFS(i, j);
            }
        }
    }
    printf("%d", ans);
    return 0;
}

更简单的解决:不用vis数组,直接将访问过的“W”变成“.”。

原文地址:https://www.cnblogs.com/yun-an/p/11104824.html

时间: 2024-08-08 07:43:53

POJ 2386 DFS深搜入门的相关文章

hdu1010-Tempter of the Bone DFS深搜入门题+奇偶剪枝

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 69699    Accepted Submission(s): 19176 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

CodeM美团点评编程大赛初赛B轮 黑白树【DFS深搜+暴力】

[编程题] 黑白树 时间限制:1秒 空间限制:32768K 一棵n个点的有根树,1号点为根,相邻的两个节点之间的距离为1.树上每个节点i对应一个值k[i].每个点都有一个颜色,初始的时候所有点都是白色的. 你需要通过一系列操作使得最终每个点变成黑色.每次操作需要选择一个节点i,i必须是白色的,然后i到根的链上(包括节点i与根)所有与节点i距离小于k[i]的点都会变黑,已经是黑的点保持为黑.问最少使用几次操作能把整棵树变黑. 输入描述: 第一行一个整数n (1 ≤ n ≤ 10^5) 接下来n-1

A Knight&#39;s Journey(DFS)深搜

A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33634 Accepted: 11450 Description Background The knight is getting bored of seeing the same black and white squares again and again and has decided to make a journey around

DFS深搜-Red and Black

深搜,从一点向各处搜找到所有能走的地方. Problem 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

dfs深搜

dfs深搜 什么是深搜? 百度百科:按照一定的顺序.规则,不断去试探,直到找到问题的解,试完了也没有找到解,那就是无解,试探时一定要试探完所有的情况(实际上就是穷举) 个人理解:暴力穷举每一种状态 它有什么好处? 容易理解 骗分利器 好写 它有什么弊端? 慢.毕竟是穷举每一种状态 一搜到底.对于递归次数非常多的话,dfs会非常浪费时间,甚至有可能会爆栈 如何实现? 算法流程图如下: #include <iostream> #include <cstdio> void dfs(int

NYoj 素数环(深搜入门)

题目链接: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=488 深搜模板: 1 void dfs(int 当前状态) 2 { 3 if(当前状态为边界状态) 4 { 5 记录或输出 6 return; 7 } 8 for(i=0;i<n;i++) //横向遍历解答树所有子节点 9 { 10 //扩展出一个子状态. 11 修改了全局变量 12 if(子状态满足约束条件) 13 { 14 dfs(子状态) 15 } 16 恢复全局变量//回

【DFS深搜初步】HDOJ-2952 Counting Sheep、NYOJ-27 水池数目

[题目链接:HDOJ-2952] Counting Sheep Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2476    Accepted Submission(s): 1621 Problem Description A while ago I had trouble sleeping. I used to lie awake,

HDU 1045 Fire Net【DFS深搜】

Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9880    Accepted Submission(s): 5739 Problem Description Suppose that we have a square city with straight streets. A map of a city is a

HDU-1016 Prime Ring Problem(DFS深搜+打表)

题目回顾(HDU-1016): Prime Ring Problem Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime.Note: the number