如何查找检查器的起始和结束图块?
问题描述:
我正在编写一个程序,查找检查程序可以采用的最大路径数。它从棋盘起始行中的棋子开始,到棋盘末尾行中的棋子上结束。问题是我无法弄清楚如何将机器可读瓦片标签映射到人类可读瓦片标签。如何查找检查器的起始和结束图块?
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
虽然我的程序正在计算路径,但我希望它能够按照它在左侧描绘的方式来看板。然而,在找到具有最多路径的结束瓦片时,我希望它按照右侧描述的方式来读取该板。我正在考虑有一个“减半”的数组,其中每个图块编号连续存储两次。例如,它可能是[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];
我不知道这是否是最好的解决办法。如果有人有任何建议,请随时发表评论。
UPDATE
这种解决方案的一个问题是,如果板的尺寸是奇数,checkerPos最终被对开阵列的边界的外侧。