c++ - 给定岛的外围标记其在矩阵中的内部(算法)
问题描述
我们得到一个大小为 N 的矩阵,它完全用 0 填充,我们还得到一个坐标列表,其中包含一个岛屿外围的坐标。现在我们必须将岛(外围+岛的一部分)标记为1。
我在想出解决这个问题的方法时遇到了麻烦,我想到了 bfs/dfs 但想不出一种方法来实现这个。(请假设只有1个岛并且输入正确,即所有输入坐标形成闭合形状并且有效
N = 5
coordinates =
0,2
1,1
1,3
2,0
2,4
3,1
3,3
4,2
所以这就是输出应该是什么样子——
0 1 2 3 4
0 0 0 1 0 0
1 0 1 1 1 0
2 1 1 1 1 1
3 0 1 1 1 0
4 0 0 1 0 0
标记的所有坐标和其中标记的方框都是岛屿的一部分。
解决方案
似乎外围坐标只是填充水平线的末端。
在这种情况下,使用 for 循环将行内的元素设置为 1。伪代码:
lastrow = -1
lastcol = -1
for every line:
extract row, col
if row = lastrow:
for (i=lastcol + 1; i<=col; i++): //fill line
A[row][i] = 1
lastrow = -1 // to treat possible space in the line
else:
A[row][col] = 1 //set the only cell
lastrow = row
lastcol = col
推荐阅读
- python - Python 脚本适用于 vscode 但不适用于 rstudio
- python - 如何在新数据上更新时间序列模型(例如 facebook_prophet),而无需每次都重新训练数据?
- c++ - 如何使用 DirectShow 检测 PAL 或 NTSC 信号的存在?
- android - 颤振中的错误:任务':better_player:compileDebugJavaWithJavac'的执行失败
- c# - Adding custom Reference to Asp.Net project and CHTML page does not see it
- python - 使用 selfassertAlmostEqual 进行单元测试时出错
- swift - 如何根据 Swift 中的范围将整数转换为枚举?
- c# - ASP.NET Identity - 获取保存的第三方访问令牌
- azure - 无法在 Azure 数据工厂的复制活动中向接收器“Azure 数据资源管理器”添加其他列
- python - 如何使用函数从字符串中提取第一个、第二个和最后一个单词?