- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
所以情况是:
1. 有NxN个格子所以是正方形格子
2. 步数有上限
3.网格内的每个单元格都有一定数量的值,会减少最大步数
4.我们只能往右下方移动。
5.起点在网格的左上角,目标在网格的右下角。
6. 我们需要确定最长的路径(剩余最大步数最少的路径)
7. 如果没有可能的路径,结果将为 -1
所以目前我已经编写了一个适用于某些情况但仍不是最佳情况的代码。
我现在正在做的是:
1.检查下一个正确的值和下一个下面的值。
2. 转到更大的值。
3.如果最大步数变为0,则返回上一个单元格并移动到另一个方向。
4. 如果正确的值和下面的值相同,我将检查下一个单元格之后的下一个单元格。
看起来问题出在第 4 点。
这是我针对第 4 点的代码:
private static int determineBestNext(int[][] grid, int currentX, int currentY) {
int nextRight = 0;
int nextBelow = 0;
int numberOfRows = grid.length - 1;
for(int i=currentX+1;i<numberOfRows-1;i++) {
nextRight += grid[currentY][i+1];
if(currentY != numberOfRows) {
nextRight += grid[currentY+1][i+1];
}
}
for(int i=currentY+1;i<numberOfRows-1;i++) {
nextBelow = grid[i+1][currentX];
if(currentX != numberOfRows) {
nextBelow += grid[i+1][currentX+1];
}
}
if(nextRight > nextBelow) {
return 1;
} else if (nextBelow > nextRight) {
return 2;
} else {
return determineBestNext(grid, currentX+1,currentY+1);
}
}
我猜回退是当 X 大于 Y 时,Y 中的步数更大,因此正确值的机会将大于 X,反之亦然。
你们有别的想法吗?谢谢!
谢谢!
最佳答案
您可以在 O(n^2)
中找到最佳路径。我将 (1,1) 称为左上角的单元格(起点),而 grid(i,j) 将是该单元格的值。 steps(i,j) 是到达 cell(i,j) 所需的最少步数。
可以快速识别关系
steps(i,j) = min(steps(i-1,j), steps(i,j-1)) + grid(i,j) // if i,j > 1
如果 i = 1
或 j = 1
或 i = j = 1
就更容易了,因为只有一个可能的路径。
所以我们要计算steps(N,N)
,我们得到steps(N,N) = min(steps(N-1,N), steps(N,N -1)) + 网格(N,N)
。对于计算,我们需要 steps(N-1,N)
和 steps(N,N-1)
。所以 steps(N-1,N) = min(steps(N-2,N), steps(N-1,N-1)) + grid(N-1,N)
和 步数(N,N-1) = min(步数(N-1,N-1),步数(N,N-2)) + 网格(N,N-1)
。我们看到,对于每个结果,我们都需要 steps(N-1,N-1)
的值。将这个值计算两次,那就是腰了。如果我们只计算一次并记住这个值,我们可以节省一次计算。这些事情经常发生。
最好的内存方法是有一个固定的评估顺序。下面是一些伪代码:
function getBestPath(grid, N)
steps = new N x N int-array //initialized with 0
//first row
steps[0][0] = grid[0][0]
// only one path to get to (0,0), it's doing nothing
for (i = 1; i < N; i++)
steps[0][i] = steps[0][i-1] + grid[0][i]
// only one path each, just going right
//other rows
for (row = 1; row < N; row++)
steps[row][0] = steps[row-1][0] + grid[row][0]
//only one way to get to (row,0), only go down
for (i = 1; i < N; i++)
steps[row][i] = min(steps[row-1][i], steps[row][i-1]) + grid[row][i]
return steps[N-1][N-1]
关于算法 - 最长路径网格拼图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27287192/
我在某处遇到了以下难题 #include int main() { { /*Fill in something here to make this code compile
我正在尝试为 iOS 创建一个拼图游戏应用程序。从我在互联网上的搜索来看,确实没有任何关于这个主题的教程。有谁知道任何人都知道的好教程或游戏教程的链接?谢谢。顺便说一下,iOS4 将不胜感激。 最佳答
如果必须使用 Promises,您会如何编写以下代码? 这个想法是,“私有(private)”方法 p1 调用一个执行异步操作的函数,然后,当异步调用的结果准备就绪时,控制权将传递给“私有(priva
下面是其中一个 facebook 谜题:我无法理解如何进行此操作。 你有 C 个容器、B 个黑球和无限数量的白球。您希望以一种方式在容器之间分配球,即每个容器至少包含一个球,并且选择白球的概率大于或等
有 5 位成员围坐在一张 table 旁。关键值是坐在 table 周围的成员数量。所以现在关键值将是 5。一个恐怖分子告诉成员,因为你们是 5 个成员,所以我将从第一个成员开始数,数到 5 的人将被
你能在不抛出错误的情况下解决这个问题吗?答案是单线。这是来自一个死的职位发布,在回复中要求回答。我认为这是剔除受访者的聪明方法,但我似乎无法在不出错的情况下回答它。 显而易见的解决方案: f.moo(
此源输出 G'Day Mate. 这是怎么发生的? public static void main(String args[]) { System.out.println("Hello Wor
我正在 android 中开发一个 slider 拼图,它有一个图像被分解成小图像,我们需要对这些 fragment 进行排序以形成正确的图像。我使用了一个 3x3 的 GridView ,其中包含
我遇到了以下难题,无法在 Picat 中制定解决方案: You will generate 5-digit numbers, where each digit is in 1..5 and diffe
我是 Javascript 新手,并且正在努力解决 CodeWars 中的这个难题。 约翰想用壁纸装饰房间。房间的尺寸为:宽度(w)、高度(h)、长度(l)。一卷壁纸的尺寸为 52cm 宽,10m 长
我对 Java 还很陌生,尝试过 Best Before puzzle from Spotify昨天。当我发送它时,我收到“错误答案”错误。检查其他解决方案没有帮助,我无法弄清楚哪个输入给出了错误的答
我正在尝试恢复我拥有的一些旧代码,这是一个拼图游戏。它从文件夹中加载图像(拼图),将它们随机放置在页面周围,然后拖放到板上。这曾经有效,但当我今天尝试使用它时,它只是抛出错误(见下文)。 HTML:
这对你们来说可能是个愚蠢的问题。它是关于 CSS Sprites 的。我有一个包含 4 个菜单的导航,例如 .. HOME COMPANY SERVICES SUPPORT 尽管我使用了一个 css
我需要创建一个标题,可以根据正在构建的页面轻松添加或删除部分,但我在处理其中一部分时遇到了问题。 我有一个标题,看起来像这样将所有组件放在 如果导航被移除,它应该看起来像这样(垂直居中) 我的问题是如
我在 JS 中构建了一个 15 拼图,但我的随机拼图生成正在创建无法解决的拼图实例。这可能是因为我不是计算机科学专业的负责人,但我不确定如何计算代码排列中的反转次数。我想知道如何编写我的代码,以便我可
我正在寻找 8 Puzzle graphs tree generator,最好是 (php+) html+css+javascript。我需要的是类似 3 2 1 6 8 7 5 4 会生成所有可
我住在德国,在 Android Market 上发布“Last Call Widget”。随着时间的推移,我一直在稳步改进它,但一组用户仍然提示它无法在他们的设备上运行。 我的小部件监听“androi
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我正在尝试制作一个看起来像这样的拼图游戏。我试过的看起来像这样。 https://jsfiddle.net/uccfb46z/ 现在如果我想改变碎片的形状我需要修改这部分 - outside: fu
首先,让我为缺少 SSCCE 表示歉意。我在这方面真的没有足够的专业知识来弄清楚什么是相关的,什么不是。 简而言之,问题是在两台运行相同分辨率 (1366x768) 的不同计算机上,我女朋友的 tum
我是一名优秀的程序员,十分优秀!