定义一个二维数组 N*M ,如 5 × 5 数组下所示:
int maze[5][5] = {
0, 1, 0, 0, 0,
0, 1, 1, 1, 0,
0, 0, 0, 0, 0,
0, 1, 1, 1, 0,
0, 0, 0, 1, 0,
};
它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的路线。入口点为[0,0],既第一格是可以走的路。
数据范围: , 输入的内容只包含
定义一个二维数组 N*M ,如 5 × 5 数组下所示:
int maze[5][5] = {
0, 1, 0, 0, 0,
0, 1, 1, 1, 0,
0, 0, 0, 0, 0,
0, 1, 1, 1, 0,
0, 0, 0, 1, 0,
};
它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的路线。入口点为[0,0],既第一格是可以走的路。
数据范围: , 输入的内容只包含
输入两个整数,分别表示二维数组的行数,列数。再输入相应的数组,其中的1表示墙壁,0表示可以走的路。数据保证有唯一解,不考虑有多解的情况,即迷宫只有一条通道。
左上角到右下角的最短路径,格式如样例所示。
5 5 0 1 0 0 0 0 1 1 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)
5 5 0 1 0 0 0 0 1 0 1 0 0 0 0 0 1 0 1 1 1 0 0 0 0 0 0
(0,0) (1,0) (2,0) (3,0) (4,0) (4,1) (4,2) (4,3) (4,4)
注意:不能斜着走!!
from collections import deque m,n = map(int, input().split()) maze = [] def bfs(maze, start, target): queue = deque([start]) visited = [] director = [(0,1), (0,-1), (-1,0), (1,0)] while queue: node = queue.popleft() visited.append(node) i, j = node maze[i][j] = -1 # 已访问过的节点不可再次访问 if node == target: return visited for x in director: i, j = node i = i + x[0] j = j + x[1] if 0 <= i < m and 0 <= j < n and maze[i][j] == 0: queue.append((i,j)) return "no way" for i in range(m): maze.append([int(i) for i in input().split()]) visited = bfs(maze, (0,0), (m - 1,n - 1)) # 找成功路径 visited = visited[::-1] target = visited[0] route = [] route.append(target) for x in visited: if (abs(x[0]-route[-1][0])==1 and x[1]==route[-1][1])&nbs***bsp;(x[0]==route[-1][0] and abs(x[1]-route[-1][1]) == 1): route.append(x) for i in route[::-1]: print(f"({i[0]},{i[1]})")
import sys import copy as cp def solutation(maze_, n, m): routes = [ [(0, 0)] ] res = None while len(routes) > 0: new_routes = [] for route in routes: route:list prev_i, prev_j = -1, -1 i, j = route[-1] if len(route) > 1: prev_i, prev_j = route[-2] if i == n-1 and j == m-1: res = route break # 遭遇墙壁, 剪去该分支 if maze_[i][j] == 1: continue # 朝上进行探索 if i - 1 >= 0 and i - 1 != prev_i: route_cp = cp.deepcopy(route) route_cp.append((i-1, j)) new_routes.append(route_cp) # 朝下进行探索 if i + 1 < n and i + 1 != prev_i: route_cp = cp.deepcopy(route) route_cp.append((i+1, j)) new_routes.append(route_cp) # 朝左探索 if j -1 >= 0 and j -1 != prev_j: route_cp = cp.deepcopy(route) route_cp.append((i, j-1)) new_routes.append(route_cp) # 朝右探索 if j +1 < m and j +1 != prev_j: route_cp = cp.deepcopy(route) route_cp.append((i, j+1)) new_routes.append(route_cp) if res is not None: break routes = new_routes return res n, m = input().split() n, m = int(n), int(m) maze = [] for i in range(0, n): maze.append( [int(i) for i in sys.stdin.readline().split()] ) route = solutation(maze, n, m) for node in route: x, y = node print(f"({x},{y})")
h, w = list(map(int, input().strip().split(" "))) map_num = [] for i in range(h): map_num.append(list(map(int, input().strip().split(" ")))) route = [] def find_route(coordi_pair, up=0, down=0, left=0, right=0): if coordi_pair == [h - 1, w - 1]: return 1 if up == 0 and down == 0 and left == 0 and right == 0: down_num = ( map_num[coordi_pair[0] + 1][coordi_pair[1]] if coordi_pair[0] + 1 < h else 1 ) right_num = ( map_num[coordi_pair[0]][coordi_pair[1] + 1] if coordi_pair[1] + 1 < w else 1 ) if down_num == 0: if ( find_route( [coordi_pair[0] + 1, coordi_pair[1]], up=1 ) == 1 ): route.append((coordi_pair[0] + 1, coordi_pair[1])) return 1 if right_num == 0: if find_route([coordi_pair[0], coordi_pair[1] + 1], left=1) == 1: route.append((coordi_pair[0], coordi_pair[1] + 1)) return 1 return -1 if up == 1: down_num = ( map_num[coordi_pair[0] + 1][coordi_pair[1]] if coordi_pair[0] + 1 < h else 1 ) right_num = ( map_num[coordi_pair[0]][coordi_pair[1] + 1] if coordi_pair[1] + 1 < w else 1 ) left_num = ( map_num[coordi_pair[0]][coordi_pair[1] - 1] if coordi_pair[1] - 1 >= 0 else 1 ) if down_num and right_num and left_num: return -1 if down_num == 0: if find_route([coordi_pair[0] + 1, coordi_pair[1]], up=1) == 1: route.append((coordi_pair[0] + 1, coordi_pair[1])) return 1 if right_num == 0: if find_route([coordi_pair[0], coordi_pair[1] + 1], left=1) == 1: route.append((coordi_pair[0], coordi_pair[1] + 1)) return 1 if left_num == 0: if find_route([coordi_pair[0], coordi_pair[1] - 1], right=1) == 1: route.append((coordi_pair[0], coordi_pair[1] - 1)) return 1 if down == 1: up_num = ( map_num[coordi_pair[0] - 1][coordi_pair[1]] if coordi_pair[0] - 1 >= 0 else 1 ) right_num = ( map_num[coordi_pair[0]][coordi_pair[1] + 1] if coordi_pair[1] + 1 < w else 1 ) left_num = ( map_num[coordi_pair[0]][coordi_pair[1] - 1] if coordi_pair[1] - 1 >= 0 else 1 ) if up_num and right_num and left_num: return -1 if up_num == 0: if find_route([coordi_pair[0] - 1, coordi_pair[1]], down=1) == 1: route.append((coordi_pair[0] - 1, coordi_pair[1])) return 1 if right_num == 0: if find_route([coordi_pair[0], coordi_pair[1] + 1], left=1) == 1: route.append((coordi_pair[0], coordi_pair[1] + 1)) return 1 if left_num == 0: if find_route([coordi_pair[0], coordi_pair[1] - 1], right=1) == 1: route.append((coordi_pair[0], coordi_pair[1] - 1)) return 1 if left == 1: up_num = ( map_num[coordi_pair[0] - 1][coordi_pair[1]] if coordi_pair[0] - 1 >= 0 else 1 ) down_num = ( map_num[coordi_pair[0] + 1][coordi_pair[1]] if coordi_pair[0] + 1 < h else 1 ) right_num = ( map_num[coordi_pair[0]][coordi_pair[1] + 1] if coordi_pair[1] + 1 < w else 1 ) if down_num and right_num and up_num: return -1 if down_num == 0: if find_route([coordi_pair[0] + 1, coordi_pair[1]], up=1) == 1: route.append((coordi_pair[0] + 1, coordi_pair[1])) return 1 if right_num == 0: if find_route([coordi_pair[0], coordi_pair[1] + 1], left=1) == 1: route.append((coordi_pair[0], coordi_pair[1] + 1)) return 1 if up_num == 0: if find_route([coordi_pair[0] - 1, coordi_pair[1]], down=1) == 1: route.append((coordi_pair[0] - 1, coordi_pair[1])) return 1 if right == 1: up_num = ( map_num[coordi_pair[0] - 1][coordi_pair[1]] if coordi_pair[0] - 1 >= 0 else 1 ) down_num = ( map_num[coordi_pair[0] + 1][coordi_pair[1]] if coordi_pair[0] + 1 < h else 1 ) left_num = ( map_num[coordi_pair[0]][coordi_pair[1] - 1] if coordi_pair[1] - 1 >= 0 else 1 ) if down_num and up_num and left_num: return -1 if down_num == 0: if find_route([coordi_pair[0] + 1, coordi_pair[1]], up=1) == 1: route.append((coordi_pair[0] + 1, coordi_pair[1])) return 1 if up_num == 0: if find_route([coordi_pair[0] - 1, coordi_pair[1]], down=1) == 1: route.append((coordi_pair[0] - 1, coordi_pair[1])) return 1 if left_num == 0: if find_route([coordi_pair[0], coordi_pair[1] - 1], right=1) == 1: route.append((coordi_pair[0], coordi_pair[1] - 1)) return 1 find_route((0, 0)) route.append((0, 0)) route.reverse() for item in route: print(f'({item[0]},{item[1]})')
from collections import deque maze = [] m,n = map(int,input().split()) for i in range(m): maze.append(list(map(int,input().split()))) q = deque([[(0,0)]]) visited = [(0,0)] # 记录遍历过的点 while q: x = q.popleft() # 如果到达终点(m-1,n-1),输出路径 if x[-1] == (m-1,n-1): for i in x: print(f"({i[0]},{i[1]})") break # (r,c) # 向上 if 0<= x[-1][0]-1 and maze[x[-1][0]-1][x[-1][1]] == 0: if (x[-1][0]-1,x[-1][1]) not in visited: q.append(x+[(x[-1][0]-1,x[-1][1])]) visited.append((x[-1][0]-1,x[-1][1])) # 向下 if x[-1][0]+1 < m and maze[x[-1][0]+1][x[-1][1]] == 0: if (x[-1][0]+1,x[-1][1]) not in visited: q.append(x+[(x[-1][0]+1,x[-1][1])]) visited.append((x[-1][0]+1,x[-1][1])) # 向左 if 0 <= x[-1][1]-1 and maze[x[-1][0]][x[-1][1]-1] == 0: if (x[-1][0],x[-1][1]-1) not in visited: q.append(x+[(x[-1][0],x[-1][1]-1)]) visited.append((x[-1][0],x[-1][1]-1)) # 向右 if x[-1][1]+1 < n and maze[x[-1][0]][x[-1][1]+1] == 0: if (x[-1][0],x[-1][1]+1) not in visited: q.append(x+[(x[-1][0],x[-1][1]+1)]) visited.append((x[-1][0],x[-1][1]+1))深度优先搜索(dfs),主要用来遍历所有路径
# 深度优先搜索 def fn(r,c,l=[(0,0)]): if 0 <= c-1 and maze[r][c-1] == 0: if (r,c-1) not in l: fn(r,c-1,l+[(r,c-1)]) if 0 <= r-1 and maze[r-1][c] == 0: if (r-1,c) not in l: fn(r-1,c,l+[(r-1,c)]) if c+1 < len(maze[0]) and maze[r][c+1] == 0: if (r,c+1) not in l: fn(r,c+1,l+[(r,c+1)]) if r+1 < len(maze) and maze[r+1][c] == 0: if (r+1,c) not in l: fn(r+1,c,l+[(r+1,c)]) if r==m-1 and c==n-1: for i in l: print(f"({i[0]},{i[1]})") m,n = map(int,input().split()) maze = [] for i in range(m): maze.append(list(map(int,input().split()))) fn(0,0)
def fn(lq,maze,vis): if len(lq) > 0: i,j = lq[0] vis = maze[i][j]+1 if i < n-1&nbs***bsp;j < m-1: if i+1 < n: if maze[i+1][j] == 0: lq.append([i+1,j]) maze[i+1][j] = vis if j+1 < m: if maze[i][j+1] == 0: lq.append([i,j+1]) maze[i][j+1] = vis if i-1 >= 0: if maze[i-1][j] == 0: lq.append([i-1,j]) maze[i-1][j] = vis if j-1 >= 0: if maze[i][j-1] == 0: lq.append([i,j-1]) maze[i][j-1] = vis lq.pop(0) fn(lq,maze,vis) n,m = list(map(int,input().split())) maze = [] for i in range(n): maze.append(list(map(int,input().split()))) lqueue = [[0,0]] maze[0][0] = 2 # 标记路线 每次+1 及路线上的值为[2,3,4,5,6,7,..,n+m] fn(lqueue,maze,maze[0][0]+1) ans = [[n-1,m-1]] # 从右下角遍历回左上角 while ans[-1][0] > 0&nbs***bsp;ans[-1][1] > 0: i,j = ans[-1] vis = maze[i][j] if i-1 >= 0: if maze[i-1][j] == vis-1: ans.append([i-1,j]) continue if j-1 >=0: if maze[i][j-1] == vis-1: ans.append([i,j-1]) continue if i+1 < n: if maze[i+1][j] == vis-1: ans.append([i+1,j]) continue if j+1 < m: if maze[i][j+1] == vis-1: ans.append([i,j+1]) continue ans.reverse() for item in ans: print(f'({item[0]},{item[1]})')
dfs
def dfs(maze_mat,routes,p = (0,0)): routes.append(p) if((p[0] == len(maze_mat)-1)and(p[1]==len(maze_mat[0])-1)): return True if((p[0] < 0) or (p[1] < 0) or (p[0] >= len(maze_mat)) or (p[1]>=len(maze_mat[0])) or (maze_mat[p[0]][p[1]]==1)): routes.pop() return False for dir_vec in ((1,0),(-1,0),(0,1),(0,-1)): next_p = (p[0]+dir_vec[0],p[1]+dir_vec[1]) if(next_p in routes): continue # 防成环 if(dfs(maze_mat,routes,next_p) == True): return True routes.pop() return False maze_height = int(input().split()[0]) maze_mat = [] for i in range(maze_height): maze_mat.append(list(map(int,input().split()))) routes = [] dfs(maze_mat,routes) for p in routes: print(str(p).replace(" ",""))
# 接收行列参数 a, b = map(int, input().split()) route_list = [] for i in range(a): route_list.append(list(map(int, input().split()))) lis = [] lis.append((0,0)) def check_loc(x,y): # 防止移动越界 global a global b if x < 0: x = 0 elif x >= a-1: x = a-1 if y < 0: y = 0 elif y >= b-1: y = b-1 return x,y while True: x, y = lis[-1] if x == a - 1 and y == b -1: for i in lis: print(f"({i[0]},{i[1]})") break # 向右移动 x1,y1 = check_loc(x,y+1) if route_list[x1][y1] == 0: lis.append((x1,y1)) route_list[x1][y1] = 2 continue # 向下移动 x1,y1 = check_loc(x+1,y) if route_list[x1][y1] == 0: lis.append((x1,y1)) route_list[x1][y1] = 2 continue # 向上移动 x1,y1 = check_loc(x-1,y) if route_list[x1][y1] == 0: lis.append((x1,y1)) route_list[x1][y1] = 2 continue # 向左移动 x1,y1 = check_loc(x,y-1) if route_list[x1][y1] == 0: lis.append((x1,y1)) route_list[x1][y1] = 2 continue lis.pop()
n, m = map(int, input().split()) nums = [] for i in range(n): nums.append([int(i) for i in input().split()]) route = [] def dfs(i, j): if nums[i][j] == 1: return False if i == n - 1 and j == m - 1: return True nums[i][j] = 1 for x, y in ((i - 1, j), (i + 1, j), (i, j - 1), (i, j + 1)): if -1 < x < n and -1 < y < m and dfs(x, y): route.append((x, y)) return True dfs(0, 0) for i in [(0, 0)] + route[::-1]: print(f"({i[0]},{i[1]})")
# 采用动态规划的方法 import sys ''' 1. dp[i][j]表示走到 [i,j]这个位置需要的最小步数,初始赋值一个达不到的最大值,此处采用 m*n+1, dp[0][0]=1 2. 递推关系 dp[i][j] = min(dp[i-1][j]+1, dp[i][j-1]+1, dp[i+1][j]+1, dp[i][j+1]+1),即判断[i,j]上下左右四个方向 的最小值中一步再加一步 3. 更新dp[i][j],有些值可能第一次循环没有更新到,所以需要多次循环,直到dp[i][j]没有变化。此处采用hash存储可能更加节省计算量 ''' # 读取数据,并给动态规划赋值 m, n = map(int, input().split()) path = [] maze = [] dp = [] max_val = m * n + 1 for i in range(m): maze.append(list(map(int, input().split()))) dp.append([max_val] * n) # 更新dp[i][j] has_updated = True dp[0][0] = 1 while has_updated: has_updated = False for i in range(m): for j in range(n): if maze[i][j] == 1: # 障碍物,用最大值代替 dp[i][j] = max_val continue elif dp[i][j] < max_val: # 已经更新过的路径不再更新,如果要求最小路径,可能需要调整。 continue else: prev = dp[i][j] # 四个方向上的值,抵达不了的用最大值代替 a = max_val if j == 0 else dp[i][j - 1] + 1 b = max_val if i == 0 else dp[i - 1][j] + 1 c = max_val if i == m - 1 else dp[i + 1][j] + 1 d = max_val if j == n - 1 else dp[i][j + 1] + 1 dp[i][j] = min(a, b, c, d) if prev != dp[i][j]: # 判断是否存在元素更新 has_updated = True # 基于dp[i][j]反向计算来时候的路径,存储到path中 i = m - 1 j = n - 1 path.append([i, j]) while i+j>0: if i > 0 and dp[i - 1][j] == dp[i][j] - 1: path.append([i - 1, j]) i -= 1 elif j > 0 and dp[i][j - 1] == dp[i][j] - 1: path.append([i, j - 1]) j -= 1 elif i < m - 1 and dp[i + 1][j] == dp[i][j] - 1: path.append([i + 1, j]) i += 1 elif j < n - 1 and dp[i][j + 1] == dp[i][j] - 1: path.append([i, j + 1]) j += 1 #逆向打印path for i, j in path[::-1]: print("({},{})".format(i, j))
def main(): N, M = map(int, input().split()) maze = [list(map(int, input().split())) for i in range(int(N))] start = (0,0) path = [] def dfs(maze, start, visit): if start[0] < 0 or start[0] >= N or start[1] < 0 or start[1] >= M or maze[start[0]][ start[1]] == 1 or start in visit: return False visit.append(start) if start[0] == N - 1 and start[1] == M - 1: return True if dfs(maze, (start[0], start[1] + 1), visit) or dfs(maze, (start[0], start[1] - 1), visit) or dfs(maze, ( start[0] + 1, start[1]), visit) or dfs(maze, (start[0] - 1, start[1]), visit): return True visit.pop() return False if dfs(maze, start, path): [print(f"({path[i][0]},{path[i][1]})") for i in range(0,len(path))] if __name__=="__main__": main()