博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ Problem 3620 Avoid The Lakes 【DFS】
阅读量:6343 次
发布时间:2019-06-22

本文共 2057 字,大约阅读时间需要 6 分钟。

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7748   Accepted: 4092

Description

Farmer John's farm was flooded in the most recent storm, a fact only aggravated by the information that his cows are deathly afraid of water. His insurance agency will only repay him, however, an amount depending on the size of the largest "lake" on his farm.

The farm is represented as a rectangular grid with N (1 ≤ N ≤ 100) rows and M (1 ≤ M ≤ 100) columns. Each cell in the grid is either dry or submerged, and exactly K (1 ≤ K ≤ N × M) of the cells are submerged. As one would expect, a lake has a central cell to which other cells connect by sharing a long edge (not a corner). Any cell that shares a long edge with the central cell or shares a long edge with any connected cell becomes a connected cell and is part of the lake.

Input

* Line 1: Three space-separated integers: NM, and K

* Lines 2..K+1: Line i+1 describes one submerged location with two space separated integers that are its row and column: R and C

Output

* Line 1: The number of cells that the largest lake contains. 

Sample Input

3 4 53 22 23 12 31 1

Sample Output

4

Source

用DFS从左上角开搜,如果碰到湖水,就把他填平。

#include 
#include
#include
#include
using namespace std;bool map[105][105];int n, m, k;int dfs(int x, int y) { if (!map[x][y] || x < 1|| y < 1 || x > n || y > m) return 0; else { map[x][y] = false; return 1+dfs(x, y+1)+dfs(x, y-1)+dfs(x+1, y)+dfs(x-1, y); }}int main() { int row, col; while (scanf("%d%d%d", &n, &m, &k) != EOF) { memset(map, false, sizeof(map)); int ans = 0; for (int i = 0; i < k; i++) { scanf("%d%d", &row, &col); map[row][col] = true; } //进行从左上角的遍历,ans统计湖面积最大值。 for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { ans = max(ans, dfs(i, j)); } } printf("%d\n", ans); } return 0;}

 

 

 

 

转载于:https://www.cnblogs.com/cniwoq/p/6770894.html

你可能感兴趣的文章
免费的编程中文书籍索引(2018第三版)
查看>>
WebAPI技术介绍
查看>>
集体通宵发版怎么破?阿里敏捷教练开出四道“药方”
查看>>
50个关于MongoDB模型设计的技巧
查看>>
不要忽视代码审查的重要性
查看>>
《Java8实战》-第十一章笔记(CompletableFuture:组合式异步编程)
查看>>
JavaScript深入之继承的多种方式和优缺点
查看>>
慕课网_《iOS-动画入门》学习总结
查看>>
【three.js】入门1 - 创建场景
查看>>
webpack打包后的静态资源问题
查看>>
Eclipse和tomcat的配置-实用篇
查看>>
201701月赛-rsa
查看>>
[翻译]在生产环境使用Kuberntes一年后,我们总结了这些经验和教训
查看>>
使用 Flask 开发 Web 应用(二)
查看>>
Spring Boot集成Freemarker和iText生成PDF文档
查看>>
“迁移策略+新容器运行时”应对有状态应用的冷热迁移挑战
查看>>
Visual Studio 2017的第五个更新包扩展了调试工具
查看>>
精益企业中架构师的角色
查看>>
开源Pravega架构解析:如何通过分层解决流存储的三大挑战?
查看>>
密歇根大学最新成果:教会无人车预测行人运动趋势
查看>>