关于牛客网迷宫问题的思考

题目描述

定义一个二维数组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],既第一空格是可以走的路。

本题含有多组数据。

输入描述:
输入两个整数,分别表示二位数组的行数,列数。再输入相应的数组,其中的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)

方法一:python代码
while True:
    try:
        def dfs(x, y, maze, path):
            if x == row - 1 and y == col - 1:
                # if len(res) == 0 or len(path) < len(res): #思考在python中如何使用这种条件
                #     res = path
                for i, j in path:
                    print('(' + str(i) + ',' + str(j) + ')')
            for tempx, tempy in [(1, 0), (0, 1),(-1, 0),  (0, -1)]:
                curx = x + tempx
                cury = y + tempy
                if 0 <= curx < row and 0 <= cury < col and maze[curx][cury] != 1:
                    path.append((curx, cury))
                    maze[curx][cury] = 1
                    dfs(curx, cury, maze, path)
                    path.pop()  #如果最终递归没有到达所期望的目标则回退一步
                    maze[curx][cury] = 0
        row, col = map(int, input().split())
        maze = []
        for _ in range(row):
            maze.append(list(map(int, input().split())))
        path = [(0, 0)]
        x = 0
        y = 0
        dfs(x, y, maze, path)
    except:
        break

这种做法就是按照题目意思,假设只有唯一的通道。

下面是C++代码
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int m, n;
struct Node
{
	int row;
	int col;
};
vector<Node> now_path;
void  FindPath(int** matrix, int x, int y, vector<Node>& v)
{
	matrix[x][y] = 1;
	now_path.push_back({ x, y });
	if (x == m - 1 && y == n - 1)
	{
		if (v.empty() || now_path.size() < v.size()) //能保证多条路径时,为最短路径
		{
			v = now_path;
		}
	}
	else
	{
		if (x < m - 1 && matrix[x + 1][y] == 0)
		{
			FindPath(matrix, x + 1, y, v);
	
		}
		if (x >= 1 && matrix[x - 1][y] == 0)
		{
			FindPath(matrix, x - 1, y, v);
		}
		if (y < n - 1 && matrix[x][y + 1] == 0)
		{
			FindPath(matrix, x, y + 1, v);
		}
		if (y >= 1 && matrix[x][y - 1] == 0)
		{
			FindPath(matrix, x, y - 1, v);
		}
	}
	matrix[x][y] = 0;
	now_path.pop_back();
}
void Show(const Node& no)
{
	cout << '(' << no.row << ',' << no.col << ')' << endl;
}
int main()
{
	
	while (cin >> m >> n)
	{
		int** maze = new int* [m];
		for (int i = 0; i < m; ++i)
		{
			maze[i] = new int[n];
		}
		for (int i = 0; i < m; ++i)
		{
			for (int j = 0; j < n; ++j)
			{
				cin >> maze[i][j];
			}
		}
		vector<Node> vec;
		now_path.clear();
		FindPath(maze, 0, 0, vec);
		for_each(vec.begin(), vec.end(), Show);
		vec.clear();
		for (int i = 0; i < m; ++i)
			delete[] maze[i];
		delete[] maze;
	}
	return 0;
}

C++代码可以使用引用参数,实参经过函数递归调用之后在主函数内输出结果。下面为仿照C++代码改写的python代码:仔细思考并验证是函数中的列表的值是不固定的,反复思考之后决定将可行路径的坐标集合转化为元组之后,再append进结果列表(result)中,这样就很好的实现了将多条可行的路径全部导出到函数外并做进一步处理的目的!
优化的最终python版本:

while True:
    try:
        def dfs(x, y, maze, pa):
            pa.append((x,y))
            maze[x][y] = 1
            if x == row - 1 and y == col - 1:
                result.append(tuple(pa))#在这里将列表转化为元组,使得存储坐标的容器不会变化,
            else:                       #故能将结果传到函数外部以便作进一步处理
                if x < row - 1 and maze[x+1][y] == 0:
                    dfs(x+1, y, maze, pa)
                if x >= 1 and maze[x-1][y] == 0:
                    dfs(x-1, y, maze, pa)
                if y < col - 1 and maze[x][y+1] == 0:
                    dfs(x, y+1, maze, pa)
                if y >= 1 and maze[x][y-1] == 0:
                    dfs(x, y-1, maze, pa)
            path.pop()
            maze[x][y] = 0
        row, col = map(int, input().split())
        maze = []
        for _ in range(row):
            maze.append(list(map(int, input().split())))
        path = []
        x = 0
        y = 0
        result = []
        dfs(x, y, maze, path)
        res = ()
        for i in result:
            if len(res) == 0 or len(i) < len(res):#添加这部分,如果有多条路线的话,则选择最短的路线!!!
                res = i
        for i in res:
            print('('+str(i[0])+','+str(i[1])+')')#元组本身的输出格式有个空格,故作这样的处理,输出格式符合要求
    except:
        break

下面是之前的版本,并不能达到判断多条可行路径哪条最优的目的。

while True:
    try:
        def dfs(x, y, maze, pa,res):
            pa.append((x, y))
            maze[x][y] = 1
            if x == row - 1 and y == col - 1:
            #增加这个条件判断,使得在多种路径中时能选择最短路径,
            #现在根据测试得到的结果看,并不能找到最短路径,实际可以参照上面的代码
                if len(res) == 0 or len(pa) < len(res): 
                    res = pa
                    for i, j in res:
                        print('(' + str(i) + ',' + str(j) + ')')
            else:
                if x < row - 1 and maze[x+1][y] == 0:
                    dfs(x+1, y, maze, pa, res)
                if x >= 1 and maze[x-1][y] == 0:
                    dfs(x-1, y, maze, pa, res)
                if y < col - 1 and maze[x][y+1] == 0:
                    dfs(x, y+1, maze, pa, res)
                if y >= 1 and maze[x][y-1] == 0:
                    dfs(x, y-1, maze, pa, res)
            path.pop()
            maze[x][y] = 0
        row, col = map(int, input().split())
        maze = []
        for _ in range(row):
            maze.append(list(map(int, input().split())))
        path = []
        result = []
        x = 0
        y = 0
        dfs(x, y, maze, path, result)
    except:
        break
还有另一个python代码:

比较简洁:
将一个空列表经过一个返回布尔值的函数处理,得到一个列表的列表,从而输出结果,但好像也没做最短路径的处理

def findout(prex, prey, x, y):
    if x == n - 1 and y == m - 1:
        result.append([x, y])
        return True
    else:
        if x + 1 < n and matrix[x + 1][y] == 0 and x + 1 != prex:
            if findout(x, y, x + 1, y) == True:
                result.append([x, y])
                return True
        if y + 1 < m and matrix[x][y + 1] == 0 and y + 1 != prey:
            if findout(x, y, x, y + 1) == True:
                result.append([x, y])
                return True
        if x - 1 >= 0 and matrix[x - 1][y] == 0 and x - 1 != prex:
            if findout(x, y, x - 1, y) == True:
                result.append([x, y])
                return True
        if y - 1 >= 0 and matrix[x][y - 1] == 0 and y - 1 != prey:
            if findout(x, y, x, y - 1) == True:
                result.append([x, y])
                return True
    return False
while True:
    try:
        n, m = list(map(int, input().strip().split()))
        matrix = []
        result = []
        for i in range(n):
            matrix.append(list(map(int, input().strip().split())))
        findout(0, 0, 0, 0)
        for x in result[::-1]:
            print("(%s,%s)" % (x[0], x[1]))
    except:
        break
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

笑着的程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值