Consider a checkerboard with n × n squares and a cost-function c(i, j) which returns a cost associated with square i, j (i being the row, j being the column). For instance (on a 5 × 5 checkerboard),
6 | 7 | 4 | 7 | 8 |
7 | 6 | 1 | 1 | 4 |
3 | 5 | 7 | 8 | 2 |
– | 6 | 7 | 0 | – |
– | – | *5* | – | – |
Thus c(1, 3) = 5
Let us say you had a checker that could start at any square on the first rank (i.e., row) and you wanted to know the shortest path (sum of the costs of the visited squares are at a minimum) to get to the last rank, assuming the checker could move only diagonally left forward, diagonally right forward, or straight forward. That is, a checker on (1,3) can move to (2,2), (2,3) or (2,4).
x | x | x | ||
o | ||||
This problem exhibits optimal substructure. That is, the solution to the entire problem relies on solutions to subproblems. Let us define a function q(i, j) as
If we can find the values of this function for all the squares at rank n, we pick the minimum and follow that path backwards to get the shortest path.
Note that q(i, j) is equal to the minimum cost to get to any of the three squares below it (since those are the only squares that can reach it) plus c(i, j). For instance:
A | ||||
B | C | D | ||
Now, let us define q(i, j) in somewhat more general terms:
The first line of this equation is there to make the recursive property simpler (when dealing with the edges, so we need only one recursion). The second line says what happens in the last rank, to provide a base case. The third line, the recursion, is the important part. It is similar to the A,B,C,D example. From this definition we can make a straightforward recursive code for q(i, j). In the following pseudocode, n is the size of the board, c(i, j)
is the cost-function, and min()
returns the minimum of a number of values:
function minCost(i, j) if j < 1 or j > n return infinity else if i = 1 return c(i, j) else return min( minCost(i-1, j-1), minCost(i-1, j), minCost(i-1, j+1) ) + c(i, j)
It should be noted that this function only computes the path-cost, not the actual path. We will get to the path soon. This, like the Fibonacci-numbers example, is horribly slow since it wastes time recomputing the same shortest paths over and over. However, we can compute it much faster in a bottom-up fashion if we store path-costs in a two-dimensional array q[i, j]
rather than using a function. This avoids recomputation; before computing the cost of a path, we check the array q[i, j]
to see if the path cost is already there.
We also need to know what the actual shortest path is. To do this, we use another array p[i, j]
, a predecessor array. This array implicitly stores the path to any square s by storing the previous node on the shortest path to s, i.e. the predecessor. To reconstruct the path, we lookup the predecessor ofs, then the predecessor of that square, then the predecessor of that square, and so on, until we reach the starting square. Consider the following code:
function computeShortestPathArrays() for x from 1 to n q[1, x] := c(1, x) for y from 1 to n q[y, 0] := infinity q[y, n + 1] := infinity for y from 2 to n for x from 1 to n m := min(q[y-1, x-1], q[y-1, x], q[y-1, x+1]) q[y, x] := m + c(y, x) if m = q[y-1, x-1] p[y, x] := -1 else if m = q[y-1, x] p[y, x] := 0 else p[y, x] := 1
Now the rest is a simple matter of finding the minimum and printing it.
function computeShortestPath() computeShortestPathArrays() minIndex := 1 min := q[n, 1] for i from 2 to n if q[n, i] < min minIndex := i min := q[n, i] printPath(n, minIndex)
function printPath(y, x) print(x) print("<-") if y = 2 print(x + p[y, x]) else printPath(y-1, x + p[y, x])
From:
相关推荐
checkerboard_7x6_50x50cm.yaml
在讨论“Deconvolution and Checkerboard Artifacts.pdf”这份文件时,我们首先要理解文档标题所表达的核心概念。标题中的“Deconvolution”指的是在神经网络中用于图像生成的一种操作,常用于将低分辨率的图像上...
相机标定是计算机视觉领域中的一个关键步骤,用于获取相机的内在参数和外在参数,以便精确地将图像坐标系转换为真实世界坐标系。在这个过程中,棋盘格图案经常被用作标定对象,因为它提供了多个已知的二维特征点,...
用于生成二维和三维的棋盘,注释清楚,大小任你调
首先,"create-a-checkerboard-image.zip" 是一个包含了创建棋盘格图像程序的压缩文件。棋盘格通常由黑白相间的正方形格子组成,这种图案设计便于计算机算法识别和处理。在标定过程中,我们会用到不同角度和位置放置...
checkerboard.cpython-39.pyc
Checkerboard-A4-25mm-10x7.svg
在衍射现象中,棋盘格(checkerboard)的光学应用是一类特殊的相位板结构,用于在光学重建图案中消除零阶光强,提高衍射效率和图像质量。 在文档“Redistribution of the zero-order by the use of a phase ...
checkerboard.m
《Checkerboard Background 4 Transparent Images-crx插件详解与应用》 Checkerboard Background 4 Transparent Images是一款专为处理透明图像而设计的CRX扩展程序,它主要服务于那些需要在网页浏览器环境中查看和...
CheckerBoard是一款由Martin Fierz开发的图形用户界面(GUI)应用程序,专用于8x8棋盘游戏的对弈。在编程领域,GUI是指使用图形元素如窗口、按钮、菜单等来与用户交互的应用程序,而CheckerBoard就是这样一个为国际...
标题中的“去卷积”(Deconvolution)和“棋盘伪像”(Checkerboard Artifacts)是深度学习领域中的重要概念,特别是在图像处理和计算机视觉任务中。去卷积通常用于逆向操作卷积神经网络(CNNs),用于上采样或恢复...
### 低雷达截面棋盘式超表面与传输窗口的研究 #### 概述 本文介绍了一种具有低雷达截面(RCS)特性的棋盘式超表面,并在其较高频段设计了一个传输窗口。该研究由西安空军工程大学基础科学部的李全、庞永强等人完成...
接下来,`CheckerBoard.cpp`文件可能包含了`CheckerBoard`类的实现细节,如覆盖算法的具体步骤。这可能涉及到一些递归或迭代策略,用于选择合适的子集来覆盖棋盘。算法可能基于贪心策略,每次选择能覆盖最多未被覆盖...
在图像处理领域,压缩算法是不可或缺的一部分,它们用于减少图像数据的存储空间需求,同时保持图像的质量可接受。本资源“图像压缩算法Matlab集合”提供了多种经典图像压缩方法的Matlab实现,这对于学习和研究图像...
在MATLAB中创建棋盘是一项基础的图像处理任务,它涉及到矩阵操作和图像显示的知识。MATLAB是一款强大的数学计算和编程环境,对于图形用户界面(GUI)和图像处理有着丰富的功能。下面我们将深入探讨如何利用MATLAB来...
浅墨出品,零资源分下载,分享精神至上~ 我们用滑动条来控制阈值,动态进行角点检测,得到不同效果的角点检测图。 图片素材是极具异域风格的建筑群,很有感觉~ 博文《【OpenCV入门教程之十六】OpenCV角点检测之...
在图像处理和数字通信领域,图像编码与压缩是至关重要的技术。它们用于减小图像数据的大小,以便更有效地存储和传输。本篇将重点讨论一种经典的无损压缩方法——行程编码(Run-Length Encoding,RLE),以及如何使用...
【作品名称】:基于 C++实现的带禁手的五子棋【C++课程...Checkerboard 棋盘类,保存棋盘数据,提供下棋接口和展示棋盘的接口 ChessPiece 棋子类 Judge 裁判类,判定五连和禁手 Player 真人玩家类 Machine 电脑玩家类
Checkerboard 棋盘类,保存棋盘数据,提供下棋接口和展示棋盘的接口 ChessPiece 棋子类 Judge 裁判类,判定五连和禁手 Player 真人玩家类 Machine 电脑玩家类 【资源声明】:本资源作为“参考资料”而不是“定制...