题目链接

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. TCP/IP通信过程(以发送电子邮件为例)(转)

    1.应用程序处理 (1)A用户启动邮件应用程序,填写收件人邮箱和发送内容,点击“发送”,开始TCP/IP通信: (2)应用程序对发送的内容进行编码处理,这一过程相当于OSI的表示层功能: (3)由A用 ...

  2. Chrome插件安装和用法

    XPath Helper 下载插件,拖入chrome://extensions/ 使用方法:ctrl+shift+x呼出 JSONView的使用: 安装JSONView插件 下载插件,拖入chrome ...

  3. PID动图——很形象

    p是控制现在,i是纠正曾经,d是管控未来! pid的公式: 其中Kp为比例带,TI为积分时间,TD为微分时间.PID控制的基本原理就是如此. pid的原理和代码,在木南创智的博客园中有很好的教程:ht ...

  4. 设置hystrix的熔断时间

    hystrix的熔断时间默认为1秒,这对于一个要部署的服务器来说太短了,所以可以把这个时间设置大一点 这个时间设置在yml中没有提示,下面是设置的代码: hystrix: command: defau ...

  5. 项目(二) esp32-cam 网页图像人脸

    https://randomnerdtutorials.com/esp32-cam-video-streaming-face-recognition-arduino-ide/ ESP32-CAM Pi ...

  6. [CF787D] legacy

    题目 Rick和他的同事们研究出了一种新的有关放射的公式,于是许多坏人就在追赶他们.所以Rick希望在被坏人抓住之前把遗产给Morty. 在他们的宇宙里总共有n颗行星,每颗行星有它自己的编号(编号为1 ...

  7. MongoDB shell 5 游标方法

    方法名 描述 cursor.snapshot()   cursor.itcount()   cursor.batchSize()   cursor.pretty()   cursor.hint()   ...

  8. Wbbpack --3配置

    Wbbpack --3配置 什么是webpack webpack 是一个现代 JavaScript 应用程序的静态模块打包(modulebundler)当 webpack 处理应用程序时,它会递归地构 ...

  9. JS的ES6的iterator

    一.iterator 1.概念:iterator是一种接口机制,为各种不同的数据结构提供统一的访问机制. 2.作用: 为各种数据结构,提供一个统一的.简便的访问接口: 使得数据结构的成员能够按某种次序 ...

  10. 常用方法 Entitys转换为DataTable

    效率比较屁,将近可以用 public static DataTable EntitiesToDataTable<T>(List<T> entitys) { Type t = t ...