Testdome 船运动问题无法理解地失败

问题描述 投票:0回答:1

我的代码未通过 testdome 上的(公开)测试。有问题的测试:https://www.testdome.com/library?page=1&skillArea=32&questionId=113618

我得到以下信息:

  • 示例:正确答案
  • 所有坐标都在网格内:错误答案
  • 某些坐标位于网格之外:错误答案

我检查路径中的所有值是否都在

$gameMatrix
数组中设置,包括开始和结束位置。
如果它们设置为 true 或 false,在我看来它一定是“在网格内”。

我的代码:

<?php
/**
 * @return boolean The destination is reachable or not
 */
function canTravelTo(array $gameMatrix, int $fromRow, int $fromColumn, 
                     int $toRow, int $toColumn) : bool
{
    if($fromRow == $toRow){ // Moving horizontally
        // if(abs($fromColumn - $toColumn) > 2) 
        //     return false; // Out of reach?
        $toCheck = range($fromColumn, $toColumn);
        foreach($toCheck as $col){
            if(!isset($gameMatrix[$fromRow][$col]))
                return false; // Out of bounds
            if(!$gameMatrix[$fromRow][$col])
                return false; // Path is blocked
        }
        return true;
    }elseif($fromColumn == $toColumn){ // Moving vertically
        // if(abs($fromRow - $toRow) > 2) 
        //     return false; // Out of reach?
        $toCheck = range($fromRow, $toRow);
        foreach($toCheck as $row){
            if(!isset($gameMatrix[$row][$fromColumn]))
                return false; // Out of bounds
            if(!$gameMatrix[$row][$fromColumn])
                return false; // Path is blocked
        }
        return true;
    }else{
        // Moving diagonally is not allowed
        return false;
    }
}

$gameMatrix = [
    [false, true,  true,  false, false, false],
    [true,  true,  true,  false, false, false],
    [true,  true,  true,  true,  true,  true],
    [false, true,  true,  false, true,  true],
    [false, true,  true,  true,  false, true],
    [false, false, false, false, false, false],
  ];
  

echo canTravelTo($gameMatrix, 3, 2, 2, 2) ? "true\n" : "false\n"; // true, Valid move
echo canTravelTo($gameMatrix, 3, 2, 3, 4) ? "true\n" : "false\n"; // false, Can't travel through land
echo canTravelTo($gameMatrix, 3, 2, 6, 2) ? "true\n" : "false\n"; // false, Out of bounds

我尝试使用各种边缘情况创建自己的单元测试,但我无法重现失败。
我尝试过增加船的最大范围,但这似乎不是问题,文档中也没有提到。

我在这里遗漏了一些明显的东西吗?

php
1个回答
0
投票

以下为通过测试。

<?php
/**
 * @return boolean The destination is reachable or not
 */
function canTravelTo(array $gameMatrix, int $fromRow, int $fromColumn, 
                     int $toRow, int $toColumn) : bool
{
    // Out of bounds
    if(
        $toRow > count($gameMatrix) - 1
        || $fromRow > count($gameMatrix) - 1
        || $toColumn > count($gameMatrix[0]) - 1
        || $fromColumn > count($gameMatrix[0]) - 1 
    ) {
        return false;
    }

    // Moving illegally
    if(
        // ... within the same column, up or down
        (
            $fromRow !== $toRow && abs($toRow - $fromRow) > 1
        )
        // ... within the same row, left or once/twice (at most) right
        ||
        (
            $fromColumn !== $toColumn && (
                ($fromColumn - $toColumn) > 1
                || ($toColumn - $fromColumn) > 2
            )
        ) 
    ) {
        return false;
    }
    
    if($fromRow == $toRow){ // Moving horizontally
        $toCheck = range($fromColumn, $toColumn);
        foreach($toCheck as $col){
            if(!$gameMatrix[$fromRow][$col])
                return false; // Path is blocked
        }
        return true;
    }elseif($fromColumn == $toColumn){ // Moving vertically
        $toCheck = range($fromRow, $toRow);
        foreach($toCheck as $row){
            if(!$gameMatrix[$row][$fromColumn])
                return false; // Path is blocked
        }
        return true;
    }else{
        // Moving diagonally is not allowed
        return false;
    }
}

$gameMatrix = [
    [false, true,  true,  false, false, false],
    [true,  true,  true,  false, false, false],
    [true,  true,  true,  true,  true,  true],
    [false, true,  true,  false, true,  true],
    [false, true,  true,  true,  false, true],
    [false, false, false, false, false, false],
  ];
  

echo canTravelTo($gameMatrix, 3, 2, 2, 2) ? "true\n" : "false\n"; // true, Valid move
echo canTravelTo($gameMatrix, 3, 2, 3, 4) ? "true\n" : "false\n"; // false, Can't travel through land
echo canTravelTo($gameMatrix, 3, 2, 6, 2) ? "true\n" : "false\n"; // false, Out of bounds

我已经从

foreach
循环中删除了越界检查,并将其设置为第一个要检查的事情。我假设每一行都有相同数量的值,因此进行
count($gameMatrix[0]) > 1
检查。

现在,关键是第二次检查。即:

    // Moving illegally
    if(
        // ... within the same column, or down
        (
            $fromRow !== $toRow && abs($toRow - $fromRow) > 1
        )
        // ... within the same row, left or once/twice (at most) right
        ||
        (
            $fromColumn !== $toColumn && (
                ($fromColumn - $toColumn) > 1
                || ($toColumn - $fromColumn) > 2
            )
        ) 
    ) {
        return false;
    }

如果您注释掉这部分,您将得到失败的检查。原因是 - 如果您在第 2 行内,使用原始代码,从 (2,0) 到 (2,5) 的移动将被视为合法,但事实并非如此,至少根据设置不合法:

玩家控制具有特定运动模式的船只单元。它只能从当前位置移动到固定目的地,如下图所示:

© www.soinside.com 2019 - 2024. All rights reserved.