NUC1158 Lake Counting【DFS】
Lake Counting
时间限制: 1000ms 内存限制: 65536KB
通过次数: 1总提交次数: 1
问题描述
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.
输入描述
* 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.
输出描述
* Line 1: The number of ponds in Farmer John's field.
样例输入
10 12
W........WW.
.WWW.....WWW
....WW...WW.
.........WW.
.........W..
..W......W..
.W.W.....WW.
W.W.W.....W.
.W.W......W.
..W.......W.
样例输出
3
来源
USACO 2004 November Gold
提示
OUTPUT DETAILS: There are three ponds: one in the upper left, one in the lower left,and one along the right side.
问题分析:(略)
这个问题和《POJ2386 Lake Counting【DFS】》是同一个问题,代码直接用就AC了。
程序说明:参见参考链接。
参考链接:POJ2386 Lake Counting【DFS】
题记:程序做多了,不定哪天遇见似曾相识的。
AC的C++程序如下:
/* POJ2386 Lake Counting */#include <stdio.h>
#include <string.h>#define DIRECTSIZE 8struct direct {int drow;int dcol;
} direct[DIRECTSIZE] ={{0, -1}, {0, 1}, {-1, 0}, {1, 0}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}};#define MAXN 100char grid[MAXN+2][MAXN+2];void dfs(int row, int col)
{int i;for(i=0; i<DIRECTSIZE; i++) {int nextrow = row + direct[i].drow;int nextcol = col + direct[i].dcol;if(grid[nextrow][nextcol] == 'W') {grid[nextrow][nextcol] = '.';dfs(nextrow, nextcol);}}
}int main(void)
{int m, n, count, i, j;while(scanf("%d%d", &m, &n) != EOF) {// 清零:边界清零memset(grid, 0, sizeof(grid));// 读入数据for(i=1; i<=m; i++)scanf("%s", grid[i]+1);// 计数清零count = 0;// 深度优先搜索for(i=1; i<=m; i++)for(j=1; j<=n; j++)if(grid[i][j] == 'W') {count++;grid[i][j] = '.';dfs(i, j);}// 输出结果printf("%d\n", count);}return 0;
}
NUC1158 Lake Counting【DFS】相关推荐
- Openjudge1388 Lake Counting【DFS/Flood Fill】
http://blog.csdn.net/c20182030/article/details/52327948 1388:Lake Counting 总时间限制: 1000ms 内存限制: 65 ...
- Lake Counting【POJ2386】
题目链接 解题思路 1.深搜或者广搜 2.并查集 版本2 这是挑战编程竞赛里的版本,如果数据范围较大,需要使用BFS #include <iostream> #include <cs ...
- CF990G-GCD Counting【dfs】
正题 题目链接:https://www.luogu.com.cn/problem/CF990G 题目大意 给出一棵有点权的树,对于每个kkk求有多条路径的点权gcdgcdgcd为kkk 1≤n≤2×1 ...
- Bailian2815 城堡问题【DFS】
2815:城堡问题 总时间限制: 1000ms 内存限制: 65536kB 描述 1 2 3 4 5 6 7 ############################# 1 # | # | # | | ...
- Bailian2816 红与黑【DFS】
2816:红与黑 总时间限制: 1000ms 内存限制: 65536kB 描述 有一间长方形的房子,地上铺了红色.黑色两种颜色的正方形瓷砖.你站在其中一块黑色的瓷砖上,只能向相邻的黑色瓷砖移动.请写一 ...
- NUC1399 Sum It Up【DFS】
Sum It Up 时间限制: 1000ms 内存限制: 65535KB 通过次数: 1总提交次数: 1 问题描述 Given a specified total t and a list of n ...
- HDU1181 变形课【DFS】(废除)
新题解参见:HDU1181 变形课[DFS+关系闭包+bitset] 变形课 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 13107 ...
- 【DFS】巧妙取量的倒油问题
题目描述 [题目描述] 有三个容器,容量分别为 a,b,c(a> b > c ),一开始a装满油,现在问是否只靠abc三个容器量出k升油.如果能就输出"yes",并且 ...
- [kuangbin]专题三 Dancing Links Squiggly Sudoku HDU - 4069【DFS】【精确覆盖】
[题目描述] Today we play a squiggly sudoku, The objective is to fill a 9*9 grid with digits so that each ...
最新文章
- android studio字符串转整型,Android Studio 中的FindBugs插件使用,轻松帮你发现Bug (转)...
- python自动华 (十四)
- 手机扫一扫就能“隔空移物”?AR炫酷新玩法,快来解锁新技能吧!
- 部署zabbix企业监控平台
- intelliJ Idea 添加 Tomcat部署(详细步骤)
- struts2实现文件查看、下载
- hdu4565之矩阵快速幂
- 【代码笔记】iOS-清除图片缓存UIActionSheet
- 【今日CV 视觉论文速览】20 Nov 2018
- Leetcode475.Heaters供暖器
- Docker安装Nacos教程
- Windows 7 beta 1补充汉化文件
- KORG Software TRITON for mac(虚拟合成器软件)
- sqlmap使用教程
- 攻防世界-music-高手进阶区-miscmisc
- 统计学权威盘点过去50年最重要的统计学思想,因果推理、bootstrap等上榜
- 开发中接口的类型都有哪些以及如何调用?
- UCWEB手机浏览器(可以和opera mini 媲美的手机浏览器)
- MSP432 BSL流程(UART)
- 怪盗基德的滑翔翼 线性DP 最长上升子序列