19蓝桥杯省赛 迷宫
题目描述:给定一个30行50列的迷宫,从左上角移动到右下角,并输出移动路径。
移动有四种方式“U,D,L,R”,分别表示往上下左右走。‘0’表示可以走,‘1’代表墙不能走。
解题思路:其实参加比赛的时候根本就不会,只学了个语言基础,连bfs都不会,赛后听学长说是bfs,后来学了bfs,还是不知道怎么解决输出路径的问题。过了好久才知道,只需要维护一个队首head和队尾tail指针就行了。在bfs的过程中,记录每个点的前驱状态,也就是记录下head指针,并且不要销毁队列,最后在到达终点时,从尾节点开始,根据head指针回溯到先前的状态,然后记录下移动路径。最后把路径倒置一下输出就是答案了。
例题:
30 50
01010101001011001001010110010110100100001000101010
00001000100000101010010000100000001001100110100101
01111011010010001000001101001011100011000000010000
01000000001010100011010000101000001010101011001011
00011111000000101000010010100010100000101100000000
11001000110101000010101100011010011010101011110111
00011011010101001001001010000001000101001110000000
10100000101000100110101010111110011000010000111010
00111000001010100001100010000001000101001100001001
11000110100001110010001001010101010101010001101000
00010000100100000101001010101110100010101010000101
11100100101001001000010000010101010100100100010100
00000010000000101011001111010001100000101010100011
10101010011100001000011000010110011110110100001000
10101010100001101010100101000010100000111011101001
10000000101100010000101100101101001011100000000100
10101001000000010100100001000100000100011110101001
00101001010101101001010100011010101101110000110101
11001010000100001100000010100101000001000111000010
00001000110000110101101000000100101001001000011101
10100101000101000000001110110010110101101010100001
00101000010000110101010000100010001001000100010101
10100001000110010001000010101001010101011111010010
00000100101000000110010100101001000001000000000010
11010000001001110111001001000011101001011011101000
00000110100010001000100000001000011101000000110011
10101000101000100010001111100010101001010000001000
10000010100101001010110000000100101010001011101000
00111100001000010000000110111000000001000000001011
10000001100111010111010001000110111010101101111000
AC代码
// 时隔三个月,终于回来补上了代码
#include <bits/stdc++.h>
const int N = 1e4;
using namespace std;
struct node{
int x,y,dir,id,pre; // pre指针记录前一个元素用于回溯
node(){}
node(int a,int b,int c,int d,int e)
{
x = a,y = b,dir = c,pre = d,id = e;
}
}que[N],tmp; // 模拟队列数据结构
int chdir[4][2] = {-1,0,1,0,0,-1,0,1}; // 方向数组
char dir[4] = {'U','D','L','R'}; // 方向数组
int maze[50][50]; // 迷宫数组
int vis[50][50]; // 标记数组
int n,m; // 行数,列数
char ans[2500]; // 答案数组
int bfs()
{
int fro = 0,top = 0;
memset(vis,0,sizeof(vis)); // 清空标记
tmp.x = 0,tmp.y = 0,tmp.dir = 0; tmp.pre = -1,tmp.id = 0;
que[top++] = tmp; // 起点入队
vis[0][0] = 1; // 标记
while(fro <= top) // 队列非空判断
{
tmp = que[fro++]; // 取出队首元素
if(tmp.x == n-1 && tmp.y == m-1) // 判断是否到达终点
return tmp.id;
for(int i = 0 ; i < 4 ; i ++) // 遍历四个方向
{
struct node p = node(tmp.x+chdir[i][0],tmp.y+chdir[i][1],i,tmp.id,top);
if( p.x >= 0 && p.x < n &&
p.y >= 0 && p.y < m &&
!vis[p.x][p.y] && maze[p.x][p.y] == 0) // 满足条件的入队
{
vis[p.x][p.y] = 1;
que[top++] = p;
}
}
}
return -1;
}
int main()
{
freopen("in.txt","r",stdin);
cin>>n>>m;
for(int i = 0 ; i < n ; i ++)
for(int j = 0 ; j < m ; j ++)
{
char c;
cin>>c;
maze[i][j] = c-'0'; // 记录迷宫
}
int pos = bfs(); // bfs搜索
int tot = 0;
while(pos)
{
ans[tot++] = dir[que[pos].dir]; // 记录方向
pos = que[pos].pre; // 根据pre指针往前回溯
}
for(int i = tot-1 ; i>=0; i --) // 输出答案
cout<<ans[i];
cout<<endl;
return 0;
}
Escape the maze
codewars 4 kyu Escape the maze
题目描述:给一个迷宫,里面一个箭头,箭头有四个方向[“v”,">","^","<"],箭头只能朝前走,帮箭头找最短路径走出来。返回移动过程。
移动只有四种方式
- 左转 L
- 右转 R
- 往后转 B
- 向前走一格 F
例题:
空格表示可以走,‘#’ 表示墙
1
'# #########',
'# >#',
'###########'
2
'####### #',
'#># # #',
'# # #',
'#########'
3
"#########################################",
"#< # # # # # # #",
"##### # ##### # ### # # ##### # # # ### #",
"# # # # # # # # # # # #",
"# # # ### # ########### # ####### # # # #",
"# # # # # # # # # # # #",
"####### # # # ##### # ### # # # #########",
"# # # # # # # # # # #",
"# # ####### ### ### ##### ### # ####### #",
"# # # # # # # # #",
"# ############### ### ##### ##### # # # #",
"# # # # # # # #",
"##### ####### # ######### # # # ### #####",
"# # # # # # # # # # #",
"# # # # # # ### # # ####### # # ### ### #",
"# # # # # # # # # # # #",
"# # ##### # # ####### # ##### ####### # #",
"# # # # # # # # # # # # #",
"# ##### ### # ### # # ##### # # ### ### #",
"# # # # # # # # ",
"#########################################"
解题思路:BFS + 记忆路径,只要在一般的BFS的基础上,维护有一个head头指针和一个tail尾指针即可,队列不要清空,并且每个节点记录下他的父节点也就是走到这的时候的head指针,等到达出口后,从队尾开始,从后往前根据head找到对应的路径。这题麻烦一点的地方在于,要控制箭头的转向,而不能单纯的往上下左右走,因此还得记录在每个位置时箭头的朝向。这里只要先把所有箭头移动的可能情况枚举出来,然后根据情况判断移动方式就行。
AC代码
def escape(maze):
verx = len(maze) # 迷宫的宽
hori = len(maze[0]) # 迷宫的长
di1 = ["v",">","^","<"] # 箭头的四个方向
for i in maze: # 先在迷宫中定位箭头的位置
for j in i:
if j in di1:
x,y,dir2 = maze.index(i),i.index(j),j
break
# 枚举箭头所有可能的状态
dic = {(">","<"):"B",("^","v"):"B",("<",">"):"B",("v","^"):"B",
(">","^"):"L",(">","v"):"R",(">",">"):"",
("<","^"):"R",("<","v"):"L",("<","<"):"",
("^","^"):"", ("^",">"):"R",("^","<"):"L",
("v","v"):"", ("v",">"):"L",("v","<"):"R",}
di = [(1,0),(0,1),(-1,0),(0,-1)] # 四个方向移动对应的坐标变化
ans = [] # 答案数组
que = [(x,y,dir2,-1)] # BFS队列
vis = [[False for _ in range(hori)] for _ in range(verx)] #访问标记数组,初始化为未访问状态
vis[0][0] = True
head,tail = 0,1 # 头尾指针
flag = 1 # 成功标志
while head != tail and flag:
pos = que[head]
for j,i in enumerate(di): # 遍历往四个方向上的移动
x = pos[0] + i[0]
y = pos[1] + i[1]
if 0 <= x < verx and 0 <= y < hori and maze[x][y] == ' 'and not vis[x][y]:
que.append((x,y,di1[j],head))
vis[x][y] = True
tail += 1
# 成功找到出口则退出循环
if (x == verx-1 or y == hori-1 or x == 0 or y == 0 ) and maze[x][y] == " ":
flag = 0
break
head += 1
if flag:
return []
# 找到队尾元素
p = que[tail-1][3]
ans.append("F")
if dic[(que[p][2],que[tail-1][2])] != '':
ans.append(dic[(que[p][2],que[tail-1][2])])
while p != 0: # 从队尾往前找,记录路径
ans.append("F")
if dic[(que[que[p][3]][2],que[p][2])] != '':
ans.append(dic[(que[que[p][3]][2],que[p][2])])
p = que[p][3] # 根据记录的父节点回溯到来到这个点之前的状态
return ans[::-1] # 最后把找到的路径倒置过来就是答案了