华为上机迷宫问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yexudengzhidao/article/details/82778051

题目描述

定义一个二维数组N*M(其中2<=N<=10;2<=M<=10),如5 × 5数组下所示:

int maze[5][5] = {

    0, 1, 0, 0, 0,

    0, 1, 0, 1, 0,

    0, 0, 0, 0, 0,

    0, 1, 1, 1, 0,

    0, 0, 0, 1, 0,

};

它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。入口点为[0,0],既第一空格是可以走的路。

Input

一个N × M的二维数组,表示一个迷宫。数据保证有唯一解,不考虑有多解的情况,即迷宫只有一条通道。

Output

左上角到右下角的最短路径,格式如样例所示。
Sample Input
0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0
Sample Output
(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)

输入描述:

输入两个整数,分别表示二位数组的行数,列数。再输入相应的数组,其中的1表示墙壁,0表示可以走的路。数据保证有唯一解,不考虑有多解的情况,即迷宫只有一条通道。

输出描述:

左上角到右下角的最短路径,格式如样例所示。

示例1
输入
5 5
0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0
输出
(0,0)
(1,0)
(2,0)
(2,1)
(2,2)
(2,3)
(2,4)
(3,4)
(4,4)

我的代码:

#第一步:完成数组输入
n = input()
arr_data = n.split(' ')
row = int(arr_data[0])
col = int(arr_data[1])
maze = []
for i in range(row):
    row_data = input()
    row_data_arr = row_data.split(' ')
    row_data_arr_int = list(map(int, row_data_arr))
    maze.append(row_data_arr_int)

route_stack = [[0, 0]]
route_history = [[0, 0]]
source = maze


def up(location):
    # 横坐标为0,无法再向上走
    if location[1] == 0:
        return False
    else:
        new_location = [location[0], location[1] - 1]
        # 已经尝试过的点不会尝试第二次
        if new_location in route_history:
            return False
        # 碰到墙不走
        elif source[new_location[0]][new_location[1]] == 1:
            return False
        else:
            route_stack.append(new_location)
            route_history.append(new_location)
            return True


def down(location):
    if location[1] == 4:
        return False
    else:
        new_location = [location[0], location[1] + 1]
        if new_location in route_history:
            return False
        elif source[new_location[0]][new_location[1]] == 1:
            return False
        else:
            route_stack.append(new_location)
            route_history.append(new_location)
            return True


def left(location):
    if location[0] == 0:
        return False
    else:
        new_location = [location[0] - 1, location[1]]
        if new_location in route_history:
            return False
        elif source[new_location[0]][new_location[1]] == 1:
            return False
        else:
            route_stack.append(new_location)
            route_history.append(new_location)
            return True


def right(location):
    if location[0] == 4:
        return False
    else:
        new_location = [location[0] + 1, location[1]]
        if new_location in route_history:
            return False
        elif source[new_location[0]][new_location[1]] == 1:
            return False
        else:
            route_stack.append(new_location)
            route_history.append(new_location)
            return True


lo = [0, 0]
while route_stack[-1] != [row-1, col-1]:
    if up(lo):
        lo = route_stack[-1]
        continue
    if down(lo):
        lo = route_stack[-1]
        continue
    if left(lo):
        lo = route_stack[-1]
        continue
    if right(lo):
        lo = route_stack[-1]
        continue
    route_stack.pop()
    lo = route_stack[-1]


def change(x):

    return tuple(x)

res = list(map(change, route_stack))
for i in res:
    print(i, end='\n')



猜你喜欢

转载自blog.csdn.net/yexudengzhidao/article/details/82778051