Java 如何查找棋盘格的开始和结束分幅?
我正在编写一个程序,它可以找到一个检查器可以使用的最大路径数。它从板的起始行的瓷砖开始,到板的结束行的瓷砖结束。问题是我无法找出如何将机器可读的磁贴标签映射到人类可读的磁贴标签Java 如何查找棋盘格的开始和结束分幅?,java,Java,我正在编写一个程序,它可以找到一个检查器可以使用的最大路径数。它从板的起始行的瓷砖开始,到板的结束行的瓷砖结束。问题是我无法找出如何将机器可读的磁贴标签映射到人类可读的磁贴标签 12344ab 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 当我的程序在计算路径时,我希望它能
12344ab
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
}
//在每行底部的空白处放置检查器,并计算路径数
for(int checkerPos=1;checkerPos0;r--)//从底部上方的行开始,在顶部边缘之前结束(即第0行)
{
对于(int c=1;c最大值)
{
最大值=板[1][c];
startOC=checkerPos;//获取错误的值
endLoc=c;//获取错误的值
}
}
maxInfo[maxCount]=max;//将当前工件的max添加到max数组中
maxInfo[maxCount+1]=startOC;//保存开始位置
maxInfo[maxCount+2]=endLoc;//保存结束位置
maxCount+=3;//转到数组中的下一个空插槽
}
如您所见,如果没有将
checkerPos
和c
映射到startoc
和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];