php - 使用循环 PHP、对角线检查增加 IF 语句中的条件

标签 php arrays loops if-statement matrix

我有作业要在 PHP 中完成这个程序,它需要一个矩阵和关键字,所以它可以在矩阵中对角地找到它们:
enter image description here
所以这就是第一个矩阵的样子,有很多不同的关键字,例如关键字是“beef”和“pork”。所以我为输入制作了一个程序,看起来像这两个例子:
enter image description here enter image description here
这两个的正确输入在这里:
enter image description here
enter image description here
这是我的代码仅适用于第一种情况,我不知道如何创建一个循环来产生越来越多的条件,而不是在 if 语句中用 && 写下所有这些条件。
请给我一些关于如何做的提示:

<?PHP


$input_line = trim(fgets(STDIN));
$input_array = explode(" ",$input_line);

// get mojiban
$mojiban = array();
for($ix=0; $ix<$input_array[0];$ix++){
    $mojiban[] = str_split(trim(fgets(STDIN)));
}

//get words
    $words = array();
    for( $kx = 0; $kx < $input_array[1]; $kx++ ){
    $words[] = trim(fgets(STDIN));
}

////check verticales
//get word
    $wordCharArray = array();
for($dx=0; $dx < $input_array[1]; $dx++){
    $wordCharArray = str_split($words[$dx]);

//looping and checking
    
    for ( $line = 0; $line < $input_array[0] ; $line++) {
        for ( $column = 0; $column < $input_array[0] ; $column++ ){
           // for ($wordsNumber = 0; $wordsNumber < $input_array[1] ; $wordsNumber++){
            
                if ($mojiban[$line][$column] == $wordCharArray[0] && $mojiban[$line+1][$column+1] == $wordCharArray[1]) {
                    echo ($column+1)." ".($line+1)."\n";
                
            //}
        }
    }
}
提前致谢!

最佳答案

这是您现在问题的解决方案,它只检查对角线元素。您可以根据需要从下面重构。
有许多不同的解决方案,但我粘贴的代码解决方案首先检查数组中的字符匹配。如果有匹配项,它会从它找到第一个元素的位置开始对角地检查元素。

<?php

// First martix
// $searchWords = ["BEEF", "PORK"];
// $matrix = ["HPPLLM", "UROQUV", "FBSRZY", "DPEFKT", "GBBEUY", "EMCQFY"];

// Second martix
$searchWords = ["ABA", "BAB"];
$matrix = ["ACEG", "HBDF", "EGAC", "DFHB"];

// returns true if it diagonally matches the element in matrix from start row
function checkDiagonallyForString(string $search, array $matrix, int $startRow, int $firstMatchPosition)
{
    $endRow = $startRow + (strlen($search) - 2);
    $finding = true;

    foreach (range($startRow, $endRow) as $searchIndex => $rowValue) {
        if (!$finding) {
            break;
        }
        $char = $search[$searchIndex + 1];
        $finding = $matrix[$rowValue][$firstMatchPosition + $searchIndex] == $char;

    }
    return $finding;
}

// format: [word: [column, row]]
$found = [];

foreach ($matrix as $row => $matrixString) {
    if (!count($searchWords)) {
        break;
    }
    foreach ($searchWords as $wordRow => $word) {
        $position = strpos($matrixString, $word[1]);
        if ($position != false) {
            if (checkDiagonallyForString($word, $matrix, $row, $position)) {
                // $position = column of matrix
                // $row = row of matrix
                $found[$word] = [$position, $row];
                unset($searchWords[$wordRow]);
            }
        }
    }

}

// Pretty output
echo "<pre>";
print_r($found);

foreach ($found as $word => $indexes) {
    echo $word . " " . implode(" ", $indexes) . "\n";
}
结果是:
enter image description here enter image description here
注意:这是一个完整的答案,但在复制和粘贴之前,请尝试获得一个大致的想法并尝试自己解决。这不是唯一的方法。

关于php - 使用循环 PHP、对角线检查增加 IF 语句中的条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67698905/

相关文章:

php - 将用户数据从js发送到php

php - 将许多服务器端信息传递给 JavaScript 的最佳做法是什么?

PHP 无法连接到 MS SQL Express 2008

php - 如何从mysql中的原始数据中仅选择第一行

Python:在列表中查找最长/最短的单词并在函数中调用它们

java - 将字符串放入字节数组

javascript - 调试帕斯卡三 Angular 形

python - Numpy 无法从 dtype 转换 ufunc 乘法输出

python - 循环遍历多个数据帧并创建日期时间索引,然后连接数据帧

Python - 在指定时间内循环 100 次