我正在编写一个程序,用于查找检查器可以采用的最大路径数。它从棋盘起始行的图 block 开始,到棋盘结束行的图 block 结束。问题是我无法弄清楚如何将机器可读的图 block 标签映射到人类可读的图 block 标签。
1 2 3 4 A B
R|B|R|B R|B|R|B
B|R|B|R B|R|B|R
R|B|R|B R|B|R|B
B|R|B|R B|R|B|R
1 2 3 4 1 2
当我的程序计算路径时,我希望它能够按照左侧所示的方式查看棋盘。然而,当它找到具有最大路径数的结束图 block 时,我希望它按照右侧所示的方式读取棋盘。我正在考虑有一个“减半”数组,其中每个图 block 编号连续存储两次。例如,它可以是 [1, 1, 2, 2] 而不是 [1, 2, 3, 4]。我只是不确定如何实现这一点。这是我的程序的一部分:
// place checker on each bottom-row black space, and count paths
for (int checkerPos = 1; checkerPos < rFringe; checkerPos += 2)
{ // always starts in bottom-left-hand corner
board = resetBoard(board); // clear board for new checker
board[bottomRow][checkerPos] = 1; // put checker on starting location
// calculate # of paths from starting location to each end tile
for (int r = bottomRow - 1; r > 0; r--) // start in row above bottom, and end right before top fringe (i.e. row 0)
{
for (int c = 1; c < rFringe; c++)
board[r][c] = board[r + 1][c - 1] + board[r + 1][c + 1];
}
// find end tile with max paths
max = board[1][1]; // default max is upper-left space on checkerboard
for (int c = 2; c < rFringe; c++) // don't re-check first column and don't check fringe
{
// compare this to other top-row boxes to find one with highest value
if (board[1][c] > max)
{
max = board[1][c];
startLoc = checkerPos; // GETS WRONG VALUE
endLoc = c; // GETS WRONG VALUE
}
}
maxInfo[maxCount] = max; // add current piece's max to max array
maxInfo[maxCount + 1] = startLoc; // save start location
maxInfo[maxCount + 2] = endLoc; // save end location
maxCount += 3; // go to next empty slot in array
}
如您所见,如果没有办法将 checkerPos
和 c
映射到 startLoc
和 endLoc
,我无法获得这些变量的准确值。
最佳答案
为了解决这个问题,我实现了一个“减半”数组。
int[] halved = new int[size]; // used for mapping the machine-readable tile #s to human-readable tile #s and letters
// populate halved array
for (int halvedIdx = 0, i = 1; halvedIdx < size - 1; halvedIdx += 2, i++)
{
halved[halvedIdx] = i;
halved[halvedIdx + 1] = i;
}
除此之外,我还改变了
startLoc = checkerPos;
endLoc = c;
至
startLoc = halved[checkerPos];
endLoc = halved[c];
我不确定这是否是最好的解决方案。如果有人有建议,请随时发表评论。
更新
此解决方案的一个问题是,如果棋盘的大小为奇数,则 checkerPos 最终会超出减半数组的边界。
关于java - 如何找到跳棋的起始和结束棋子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26318568/