题目链接

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

W........WW.
.WWW.....WWW
....WW...WW.
.........WW.
.........W..
..W......W..
.W.W.....WW.
W.W.W.....W.
.W.W......W.
..W.......W.

Sample Output


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 = ;
char map[N][N];
int vis[N][N];//访问标记
int n, m;
int dx[] = { -,,,-,,-,, };
int dy[] = { ,,,,,-,-,- };//结点周边从上到下,从左到右八个点
int ans = ;//湖数 bool valid(int x, int y)
{
return (x >= && y >= && x < n && y < m);
} void DFS(int x, int y)
{
vis[x][y] = ;//已访问
for (int i = ; i < ; i++)
{
int newx = x + dx[i];
int newy = y + dy[i];
if (valid(newx, newy))
{
if (map[newx][newy] == 'W'&&vis[newx][newy] == -) DFS(newx, newy);
}
}
} int main()
{
memset(vis, -, sizeof(vis));
scanf("%d%d", &n, &m);
getchar();
for (int i = ; i < n; i++)
{
for (int j = ; j < m; j++)
{
scanf("%c", &map[i][j]);
}
getchar();
}
for (int i = ; i < n; i++)
{
for (int j = ; j < m; j++)
{
if (map[i][j] == 'W'&&vis[i][j] == -)
{
ans++;
DFS(i, j);
}
}
}
printf("%d", ans);
return ;
}
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std; char map[][];
int vis[][] = { };
int n, m; int dx[] = { -,,,-,,-,, };
int dy[] = { ,,,,,-,-,- }; bool valid(int x, int y)
{
return(x >= && x < n&&y >= && y < m);
} void dfs(int x, int y)
{
vis[x][y] = ;
for (int i = ; i < ; i++)
{
int nx = x + dx[i];
int ny = y + dy[i];
if (valid(nx, ny))
{
if (map[nx][ny] == 'W' && !vis[nx][ny])dfs(nx, ny);
}
}
} int main()
{
scanf("%d%d", &n, &m);
int cnt = ;
for (int i = ; i < n; i++) scanf("%s", map[i]);
for (int i = ; i < n; i++)
{
for (int j = ; j < m; j++)
{
if (!vis[i][j] && map[i][j] == 'W')
{
dfs(i, j);
cnt++;
}
}
}
printf("%d\n", cnt);
return ;
}

二刷

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

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

  1. DFS 深搜专题 入门典例 -- 凌宸1642

    DFS 深搜专题 入门典例 -- 凌宸1642 深度优先搜索 是一种 枚举所有完整路径以遍历所有情况的搜索方法 ,使用 递归 可以很好的实现 深度优先搜索. 1 最大价值 题目描述 ​ 有 n 件物品 ...

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

    [编程题] 黑白树 时间限制:1秒 空间限制:32768K 一棵n个点的有根树,1号点为根,相邻的两个节点之间的距离为1.树上每个节点i对应一个值k[i].每个点都有一个颜色,初始的时候所有点都是白色 ...

  3. poj 2386:Lake Counting(简单DFS深搜)

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18201   Accepted: 9192 De ...

  4. POJ 2488:A Knight's Journey 深搜入门之走马观花

    A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35342   Accepted: 12 ...

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

    [题目链接:HDOJ-2952] Counting Sheep Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  6. DFS深搜——Red and Black——A Knight&#39;s Journey

    深搜,从一点向各处搜找到全部能走的地方. Problem Description There is a rectangular room, covered with square tiles. Eac ...

  7. NYoj 素数环(深搜入门)

    题目链接: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=488 深搜模板: void dfs(int 当前状态) { if(当前状态为边界状 ...

  8. Red and Black(DFS深搜实现)

    Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...

  9. UVA 165 Stamps (DFS深搜回溯)

     Stamps  The government of Nova Mareterrania requires that various legal documents have stamps attac ...

随机推荐

  1. redux学你参考网站

    redux官方网站 http://cn.redux.js.org/docs/api/combineReducers.html https://www.redux.org.cn 从设计的角度看Redux ...

  2. Jupyter的快捷键使用

    命令模式 (按键 Esc 开启) Enter : 转入编辑模式 Shift-Enter : 运行本单元,选中下个单元 Ctrl-Enter : 运行本单元 Alt-Enter : 运行本单元,在其下插 ...

  3. learning shell monitor prog function

    [Purpose]        Shell script monitor prog function   [Eevironment]        Ubuntu 16.04 bash env   [ ...

  4. 如何查看WinDbg扩展有哪些命令

    如果您想查看任何windbg扩展所支持的命令,可以采用各种方法. 你可以用!<ext_name>.help命令查看该扩展支持的所有命令.用扩展模块名替换<ext_name>.( ...

  5. call JSON.parse JSON.stringify typeof 的使用及严格模式this的使用

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. A`>G?~C009

    A`>G?~C009 这场怎么才5题...看完猫的提交记录以为猫猫没写这场F A Multiple Array 直接做 B Tournament 直接树d C Division into Two ...

  7. 使用kubectl访问kubernetes集群

    之前访问k8s都是通过token进去dashboard,如下所示.但是现在希望通过kubectl访问k8s,所以还需要进一步的配置. 1. 安装kubectl命令行工具,配置环境变量,环境变量的值指向 ...

  8. ubuntu笔记2-误删dpkg的/var/lib/dpkg/info文件夹

    由于误删了dpkg的/var/lib/dpkg/info文件夹,导致安装文件的时候报错 错误提示:E: Sub-process /usr/bin/dpkg returned an error code ...

  9. MVVC与乐观锁和悲观锁

    在并发读写数据库时,读操作可能会不一致的数据(脏读).为了避免这种情况,需要实现数据库的并发访问控制,最简单的方式就是加锁访问.由于,加锁会将读写操作串行化,所以不会出现不一致的状态.但是,读操作会被 ...

  10. 2018-2019-2 20175211 实验四《Android程序设计》实验报告

    目录 一.实验内容及步骤 1.Android Studio的安装测试 2.Activity测试 3.UI测试 4.布局测试 5.事件处理测试 二.问题及解决方法 三.代码托管 四.实验心得体会 一.实 ...