题目链接

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. React.js Tutorial: React Component Lifecycle

    Introduction about React component lifecycle. 1 Lifecycle A React component in browser can be any of ...

  2. k8gege的Ladon使用笔记

    自己今天看到了这个工具,感觉挺实用的,尝试学习用法 资产扫描模块 初级用法: Ladon.exe 192.168.1.8/24 OnlinePC(扫当前机器所处C段的存活主机,其它模块同理) 总结:在 ...

  3. python--io多路复用之select实现

    1.I/O多路复用指:通过一种机制,可以监视多个描述符,一旦某个描述符就绪(一般是读就绪或者写就绪),能够通知程序进行相应的读写操作. 2.I/O多路复用避免阻塞在io上,原本为多进程或多线程来接收多 ...

  4. Scrapy爬虫案例 | 数据存储至MySQL

    首先,MySQL创建好数据库和表 然后编写各个模块 item.py import scrapy class JianliItem(scrapy.Item): name = scrapy.Field() ...

  5. 3495: PA2010 Riddle 2-sat 前缀优化

    3495: PA2010 Riddle 2-sat 前缀优化 链接 bzoj 思路 不想说啥了,看hwim的吧,我去睡觉了zZ. 代码 /******************************* ...

  6. luogu P2353 背单词

    二次联通门 : luogu P2353 背单词 一眼看过去, 卧槽,AC自动机板子题 写完后T成SB 卧槽10^6 做个篮子啊 重构思路... 恩..Hash + 莫队... 恶心啊.. 找xxy d ...

  7. eclipseWeb项目如何实现网址发送给外人——内部穿透

    教程:https://blog.csdn.net/Feihongxiansen/article/details/94480480 部署完成后: 打开cmd命令: 暴露端口8081成功: 将eclips ...

  8. SpringCloud:学习Gateway网关拦截器的ServerWebExchange

    1.Gateway的拦截器 我们要在项目中实现一个拦截器,需要继承两个类:GlobalFilter, Ordered GlobalFilter:全局过滤拦截器,在gateway中已经有部分实现,具体参 ...

  9. 【CSP模拟赛】Freda的旗帜

    题目描述  要开运动会了,Freda承担起了制作全校旗帜的工作.旗帜的制作方法是这样的:Freda一共有C种颜色的布条,每种布条都有无数个,你可以认为这些布条的长.宽.厚都相等,只有颜色可能不同.每个 ...

  10. BiseNet阅读总结

    一.思路 语义分割既需要丰富的空间信息,又需要较大的感受野.然而,现代方法通常会牺牲空间分辨率来实现实时推理速度,导致性能低下.本文提出了一种新的双边分割网络(BiSeNet)来解决这一难题.我们首先 ...