【问题标题】:Find route from starting point and return to start point从起点寻找路线并返回起点
【发布时间】:2018-01-08 01:19:08
【问题描述】:

示例 1

这是我们提供给 PHP 脚本的数据(字符串)

T1_P8,
T1_P2, 
T1_P3,
P8_P3,

这些数据可以在数组中,如:

$TUBEDATA=array("t1_p8","t1_p2","t1_p3","t8_p3");

现在脚本必须从数据中提取所有内容并在屏幕上输出所有可能的组合。 组合的规则是,如果我们以 T1 开头,我们必须以 T1 结尾,并且我们需要传递所有可用的组合。 上述数据的输出应为:

T1 -> P8 -> P3 -> T1
T1 -> P3 -> P8 -> T1
P8 -> T1 -> P3 -> P8
P8 -> P3 -> T1 -> P8

示例 2

如果我们为 TUBEDATA 添加新值

T1_P8,
T1_P2,
T1_P3,
P8_P3,
P2_P3 <-new value

这些数据可以在数组中,如:

$TUBEDATA=array("t1_p8","t1_p2","t1_p3","t8_p3",“p2_p3“);

那么我们有更多的组合,输出应该是:

T1 -> P8 -> P3 -> T1
T1 -> P3 -> P8 -> T1
P8 -> T1 -> P3 -> P8
P8 -> P3 -> T1 -> P8
T1 -> P2 -> P3 -> T1            
T1 -> P2 -> P3 -> P8 -> T1       
P2 ->T1 ->P8 ->P3 ->P2            
P2 ->T1 ->P3 ->P2                      
P2 ->P3 ->P8 ->T1 ->P2           
P2 ->P3 ->T1 ->P2               

需要注意的是不要创建循环。 循环示例(无效组合):

T1 -> P8 -> P3 -> P8 -> P3 -> T1

如果我们有 allredy P8 在场,我们就不会再去了。

到目前为止我做了什么

$given_data = array("t1_p8", "t1_p2", "t1_p3", "p8_p3");

$array_index = 3;

$visited = array();

$current_index = 0;

$current_data = explode("_",$given_data[$array_index]);

$starting_point = $current_data[0];//t1
$search_point =  $current_data[1];//p8

$generated_rout =  $starting_point."->".$search_point;

$round = true;

$i = 0;

do {

    if( $current_index == $i ){
        $i++;
        continue;
    }

    $pointing_data = explode("_",$given_data[$i]);

    $t1 = $pointing_data[0];
    $t2 = $pointing_data[1];

    if( $search_point == $t1 ){
        $current_index = $i;
        $generated_rout .= "->".$t2;
        if( $starting_point == $t2 ){
            break;
        }
        $search_point = $t2;
        $i = 0;

    }
    elseif( $search_point == $t2 ){
        $current_index = $i;
        $generated_rout .= "->".$t1;
        if( $starting_point == $t1 ){
            break;
        }
        $search_point = $t1;
        $i = 0;
    }
    else{
        $i++;
    }

} while ( $i < 4 );

echo $generated_rout;

我的逻辑的问题是我只能从一个方向找到路线,但是当我尝试使用反之亦然的条件(例如交换 $starting_point 和 $search_point)时,它不会产生正确的结果。

请帮助我解决上述问题,并提前感谢

【问题讨论】:

    标签: php algorithm data-structures logic


    【解决方案1】:

    这个问题很容易通过图的逻辑来解决。让我们通过一个例子来很好地理解这个问题。

    例子:-

    T1_P8,
    T1_P2, 
    T1_P3,
    P8_P3
    

    现在让我们拆分(分解)每个字符串,因为它们是起点和终点,并用下划线分隔。

    所以我们的顶点将是 - T1, P8, P2, P3

    此外,我从示例中了解到,该图似乎是无向的。

    现在如果我们用图形来可视化这个例子,如果看起来像这样:-

                    T1 ---- P8
                 /    \       |
                /      \      |
               /        \     |
               P2          P3
    

    所以,从图中可以清楚地看出有 4 条边:-

    T1 - P8
    T1 - P2
    T1 - P3
    P3 - P8
    

    现在创建图表后,只需在每个节点上启动 dfs 并保留一个 visited 数组以跟踪您访问过的所有节点,这将阻止您再次循环通过该节点.

    现在我不会为你编写整个代码,我希望你尝试这么多,现在这个概念对你来说已经很清楚了。

    在评论中告诉我,你怎么看。

    希望这会有所帮助!

    【讨论】:

    • 非常感谢您的帮助,但我想在访问节点后返回起点(找到所有可能的方式),而 dfs 仅用于访问所有节点,所以我该如何返回开始点?
    猜你喜欢
    • 2012-07-28
    • 1970-01-01
    • 2014-05-19
    • 1970-01-01
    • 1970-01-01
    • 2014-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多