如何找到检查器的起始和结束图块?

时间:2014-10-11 19:42:19

标签: java

我编写的程序可以找到检查程序可以采用的最大路径数。它从电路板的起始行中的瓷砖开始,并在电路板的结束行中的瓷砖上结束。问题是我无法弄清楚如何将机器可读的图块标签映射到人类可读的图块标签。

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
}

如您所见,如果无法将checkerPosc映射到startLocendLoc,我无法获得这些变量的准确值。

1 个答案:

答案 0 :(得分:1)

为了解决这个问题,我实施了一个&#34;减半&#34;阵列。

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最终会在对分的数组范围之外。

相关问题