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
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深搜入门的更多相关文章
- DFS 深搜专题 入门典例 -- 凌宸1642
DFS 深搜专题 入门典例 -- 凌宸1642 深度优先搜索 是一种 枚举所有完整路径以遍历所有情况的搜索方法 ,使用 递归 可以很好的实现 深度优先搜索. 1 最大价值 题目描述 有 n 件物品 ...
- CodeM美团点评编程大赛初赛B轮 黑白树【DFS深搜+暴力】
[编程题] 黑白树 时间限制:1秒 空间限制:32768K 一棵n个点的有根树,1号点为根,相邻的两个节点之间的距离为1.树上每个节点i对应一个值k[i].每个点都有一个颜色,初始的时候所有点都是白色 ...
- poj 2386:Lake Counting(简单DFS深搜)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18201 Accepted: 9192 De ...
- POJ 2488:A Knight's Journey 深搜入门之走马观花
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35342 Accepted: 12 ...
- 【DFS深搜初步】HDOJ-2952 Counting Sheep、NYOJ-27 水池数目
[题目链接:HDOJ-2952] Counting Sheep Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
- DFS深搜——Red and Black——A Knight's Journey
深搜,从一点向各处搜找到全部能走的地方. Problem Description There is a rectangular room, covered with square tiles. Eac ...
- NYoj 素数环(深搜入门)
题目链接: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=488 深搜模板: void dfs(int 当前状态) { if(当前状态为边界状 ...
- Red and Black(DFS深搜实现)
Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...
- UVA 165 Stamps (DFS深搜回溯)
Stamps The government of Nova Mareterrania requires that various legal documents have stamps attac ...
随机推荐
- node 文件上传
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- UI系统的分类
1.DSL系统:UI领域特定语言 html markdown; 与平台无关,只与通用UI领域有关: 2.平台语言系统(通用语言系统) UI概念在平台和通用语言中的表示. 一.信息表达: 基本信息:文本 ...
- linux下如何完全删除用户
1.首先进入系统创建一个用户 [root@localhost /]# useradd haha #创建用户 haha是用户名 [root@localhost /]# passwd haha ...
- Internal.Cryptography.CryptoThrowHelper+WindowsCryptographicException 拒绝访问 / 出现了内部错误 c# – 当使用X509Certificate2加载p12/pfx文件时出现
环境:iis/netcore 2.2 初始调用:X509Certificate2 certificate = new X509Certificate2(input.Path, CER_PASSWORD ...
- vim文本编辑器——删除、复制、剪切、更改某一个字符、替换、撤销、关键字搜索
1.删除: (1)删除光标所在处的字符: 如上图所示:点击一次x键只能删除一个字符. (2)删除光标所在处后的n个字符(nx): 删除前: 输入6x: (3)删除光标所在的行(dd): 删除前: 输入 ...
- Struts2框架的搭建
Struts2是WebWork框架的升级版本,替代了Servlet. 由于用IDEA下载jar包失败,直接创建手动导包. 1.导包: (1)Struts2的目录结构: (2)导入jar包: 2.书写A ...
- 常用方法 读取 Excel的单位格 为 日期格式 的数据
原文:地址忘了 百度应该有 Excel的单元格为日期格式,数值型日期,可用下面这个方法得到正常的数据 /// <summary> /// 数字格式的时间 转换为 字符串格式的时间 /// ...
- 如何把Eclipse项目迁移到AndroidStudio(如何把项目导入安卓)--这我很困惑
学习android对我来说,就是兴趣,所以我以自己的兴趣写出的文章,希望各位多多支持!多多点赞,评论讨论加关注. 大佬必备功能. 把Eclipse项目迁移到AndroidStudio 现在就叫你如何把 ...
- NOIP 2013货车运输
当然这题有很多做法,但是我看到没有人写DSU的很惊奇 按照之前做连双向边题的经验,这题可以用并查集维护联通 然后对于每个询问\(x,y\),考虑启发式合并 当两个点集\(x,y\)合并时,一些涉及到其 ...
- 查看大图、html查看大图、js查看大图
$(".pimg").click(function(){ var _this = $(this);//将当前的pimg元素作为_this传入函数 imgShow("#ou ...