6

我有一个字符串描述了一个 nxm 元素的矩阵,如下所示:

§inputmap = "
~~~~~~~~~~~~~~~~~~~~B~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~BBB........BBB~~~~~~~~~~~~~
~~~~~~~~~~BB...............FBB~~~~~~~~~~
~~~~~~~~BB....................BB~~~~~~~~
~~~~~~BB.....F..................BB~~~~~~
~~~~~BB.....................F.....B~~~~~
~~~~B..............................B~~~~
~~~B........F.......................B~~~
~~BB.........F......................BB~~
~~B................F.................BB~
~BF....F....F........................FB~
~B.....................................B
B.....................................FB
B........F......F......................B
B...........................F..........B
B......................................B
B......................................B
B.......F.......................F......B
B......FFF.............................B
B.......F.............................FB
~B..................F.................FB
~BF...........................F.......B~
~~B...F...........F..........FFFFF.F.BB~
~~BB..................F..F....F.....BB~~
~~~B.......................FF.FF....B~~~
~~~~B..............................B~~~~
~~~~~BB...........................B~~~~~
~~~~~~BB........................BB~~~~~~
~~~~~~~~BB..........F..........B~~~~~~~~
~~~~~~~~~~BB................BB~~~~~~~~~~
~~~~~~~~~~~~~BBB.......F.BBB~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~BBBBBB~~~~~~~~~~~~~~~~~
";
$inputmap = trim($inputmap);

我需要构建一个正则表达式(或其他东西)来搜索字符串:

$search = "
*F*
FFF
*F*
";
$search = trim($search);

在整个网格上。另一方面,我需要找到一个由 5 个不同字母“F”(3 个垂直和 3 个水平)组成的模式,以获取在地图上找到的模式的行/列位置。

考虑到输入矩阵可能不同(5x5 或 10x10 或 20x25 或 ...),有没有办法解决我的 php 和正则表达式问题?

4

5 回答 5

4

您可以使用$length=strstr($inputmap,"\n")来查找每条线的宽度。然后,您可以构造一个正则表达式,它会找到一个 F,然后是($length-2)其他字符,然后是 3 个 F,然后是($length-2)其他字符,然后是 F。

于 2013-06-10T14:43:46.757 回答
3

You can do something like this (without regular expressions) :

$map = explode("\n", $inputmap);

for ($vcount = 0; $vcount < sizeof($map); ++$vcount) {  // Loop through the map vertically
    for ($hcount = 0; $hcount < strlen($map[$vcount]); ++$hcount) { // Loop through each character of each line
        if ($map[$vcount][$hcount] == "F") {
            if ($map[$vcount + 1][$hcount - 1] == "F" && $map[$vcount + 1][$hcount] == "F" && 
                $map[$vcount + 1][$hcount + 1] == "F" && $map[$vcount + 2][$hcount] == "F")
                echo "Pattern found, starting at : (v)$vcount x (h)$hcount";
        }
    }
}

$> php test.php
php test.php
Pattern found, starting at : (v)18 x (h)8
Pattern found, starting at : (v)22 x (h)30
$>

But I must admit that it could take a while for extra-big maps.

/!\ add some code to verify that the line $vcount + 1/2 actually exists, and the char $hcount + 1 too.

于 2013-06-10T14:48:56.510 回答
2

您可以使用以下代码:

$lines = array_filter(preg_split("#\r\n?|\n#", $string)); // Creating array of lines
$matrix = array_map('str_split', $lines); // Creating a matrix

foreach($lines as $line_number => $line_content){ // Looping through the lines
    $pos = strpos($line_content, 'FFF');
    if(!$pos === false){// If FFF found
        while(true){
            if(isset($matrix[$line_number-1][$pos+1],$matrix[$line_number+1][$pos+1]) && $matrix[$line_number-1][$pos+1] == 'F' && $matrix[$line_number+1][$pos+1] == 'F'){ //Checking ...
                echo 'Found at: X:'.$pos.' & Y:'.$line_number.'<br>'; // Ouput
            }
            $pos = strpos($line_content, 'FFF', $pos+1); // Search further
            if(!is_int($pos)){
                break;
            }
        }
    }
}

这是怎么回事 ?

  1. 我们创建了一系列线
  2. 我们创建一个矩阵
  3. 我们遍历这些并搜索FFF这是为了提高性能。因此,我们不是循环遍历整个矩阵并搜索F,而是直接搜索FFF
  4. 检查矩阵中是否存在该值(以防止未定义的索引错误),然后检查它是否等于F
  5. 输出
  6. 在同一行进一步搜索

在线演示

请注意,坐标是 + 的中心

于 2013-06-10T15:21:16.270 回答
1

try with this pattern:

if the end of line is \n:

$pattern = '~F.{'. ($n-2) . '}FFF.{' . ($n-2) . 'F~s';

if the end of line is \r\n then replace by $n-3

or better use quinxorin trick to know the line length until the \n

于 2013-06-10T14:49:31.503 回答
1

@FrankieTheKneeMan & all...我最初的方法是为输入网格图的“n”个字符构建“n”个正则表达式。就像是...

$search_001 = "
.F......................................
FFF.....................................
.F......................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
";


$search_002 = "
..F.....................................
.FFF....................................
..F.....................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
";

(……省略……)

$search_100 = "
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
..........F.............................
.........FFF............................
..........F.............................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
";

(……省略……)

$search_999 = "
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
......................................F.
.....................................FFF
......................................F.
";

在哪里 ”。” obvioulsy 表示任何字符。

这是一个愚蠢的想法吗?!?

于 2013-06-10T15:04:26.570 回答