selcukmart/number-of-connected-groups-y42

1.0.0 2022-02-17 09:28 UTC

This package is auto-updated.

Last update: 2024-09-17 15:07:38 UTC


README

composer require selcukmart/number-of-connected-groups-y42

连通组数

其中包含两种解决方案。其中一个是非递归的(自定义坐标之间的距离计算技术),仅可迭代,另一个是经典解决方案,称为深度优先搜索(DFS)。

代码有单元测试。克隆后可以测试。

tests/CountForIterativeTest.php
tests/CountForRecursiveTest.php

迭代示例:

$matrix = [
[1, 1, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 1, 1],
[1, 0, 0, 0, 0],
[1, 0, 1, 0, 1]
];

$result = (new NumberOfConnectedGroupsIterative(5, 5))->countGroups($matrix);

只有水平和垂直链接时,结果将是5。

DFS示例

$matrix = [
            [1, 1, 0, 0, 0],
            [0, 1, 0, 0, 1],
            [1, 0, 0, 1, 1],
            [1, 0, 0, 0, 0],
            [1, 0, 1, 0, 1]
        ];

        $result = (new NumberOfConnectedGroupsRecursive(5, 5))->countGroups($matrix);

只有水平和垂直链接时,结果将是5。