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 ...
随机推荐
- http编程体系结构URL loading system
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/URLLoadingSystem/URLLoadi ...
- Daily consumption
Bill record, standard of living, record every consumption, income, expenditure, manage your own life
- 使用solr将CSV/XML/DB/JSON数据发布为Rest Service
Download http://lucene.apache.org/solr/downloads.html Apache Solr入门基础——Windows下安装与配置 https://blog.cs ...
- nginx 访问控制之 document_uri
这就用到了变量$document_uri,根据前面所学内容,该变量等价于$uri,其实也等价于location匹配. 示例1: if ($document_uri ~ "/admin/&qu ...
- shell 查找字符串中字符出现的位置
#!/bin/bash a="The cat sat on the mat" test="cat" awk -v a="$a" -v b=& ...
- <每日 1 OJ> -LeetCode 21. 合并两个有序链表
题目: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4输出:1->1-> ...
- <每日 1 OJ> -Table
上图是一个Mysql查询结果图,我们看到这个表格非常漂亮,只需要使用”+”和”-”两个符号就可以打印,现在你的任务是打印一个n×m的表格我们定义单位长度(水平方向有三个”-”,竖直方向有一个”| ”, ...
- Guava Cache 缓存实现与源码分析
目录 一.概述 1.内存缓存 2.核心数据结构 二.具体实现 0.一览众山小 1.CacheBuilder 构建器 2.LocalCache 一.概述 1.内存缓存 可看作一个jdk7的concurr ...
- bad ELF interpreter: No such file or directory
1.在64系统里执行32位程序如果出现/lib/ld-linux.so.2: bad ELF interpreter: No such file or directory,安装下glic即可 yum ...
- Python数据分析学习(二)
转摘:https://segmentfault.com/a/1190000015613967 本篇将继续上一篇数据分析之后进行数据挖掘建模预测,这两部分构成了一个简单的完整项目.结合两篇文章通过数据分 ...