广度优先搜索(BFS)走迷宫:

广度优先搜索(BFS)走迷宫:

广度优先搜索(BFS)是一种经典的图遍历算法,在解决路径查找、迷宫问题、拓扑排序等问题

前置知识:队列:

队列与栈类似,但是先进先出,而不是栈的先进后出。

原理:

相当于爆破,将所有的位置都走一遍

通用迷宫代码实现:

#include <iostream>
#include <string>
#include <queue>
using namespace std;
struct point{
  int x,y,sted;
  string path;
};
queue<point> r;
int main()
{
int n,m;//这是行和宽,需要填入数据
int dx[4]={0,1,0,-1};//按照右,下,左,上的顺序
int dy[4]={1,0,-1,0};//按照右,下,左,上的顺序
char dw[4]={'d','s','a','w'};
int maze[n][m];//填上地图
int v[n][m]={0};
int startx,starty,endx,endy; //填入起点和重点
point start;
start.x=startx;
start.y=starty;
start.sted=0;
start.path="";
r.push(start);
v[startx][starty]=1;
while (!r.empty())
{
  auto current =r.front();
  r.pop();
  if(current.x==endx&&current.y==endy){
    cout<<current.sted<<endl;//输出步数
    cout<<current.path<<endl;//输出路径
    break;
  }
  for(int i=0;i<4;i++){
    int tx,ty;
    tx=current.x+dx[i];
    ty=current.y+dy[i];
    if(tx<0||tx>n||ty<0||ty>m)//边界处理
      continue;
    if(maze[tx][ty]==0&&v[tx][ty]==0){
     point next{tx,ty,current.sted+1,current.path+dw[i]};
      v[tx][ty]=1;
    }
  }
}
}

例题:

[SWPUCTF 2021 新生赛]老鼠走迷宫

WP:

文件由Pylnstaller打包而成,为可执行的exe程序,

使用pyinstxtractor解包

输入指令python pyinstxtractor.py a

然后得到一个名为a_extracted的文件夹,里面全是PYC文件:

发现5.pyc这个文件比较特殊,刚开始使用pycdc进行反汇编,想将反汇编后的代码直接放到一个新文件里,结果发现一直报错:pycdc Warning: block stack is not empty!

没办法,只能用 uncompyle6了, uncompyle6 5.pyc > 5.py

后来发现pycdc其实已经创好py文件了,但创建到我安装pycdc的文件里,因为已经存在了,所以报错.....

反汇编后的代码:

# uncompyle6 version 3.9.2
# Python bytecode version base 3.7.0 (3394)
# Decompiled from: Python 3.12.4 | packaged by Anaconda, Inc. | (main, Jun 18 2024, 15:03:56) [MSC v.1929 64 bit (AMD64)]
# Embedded file name: 5.py
import random, msvcrt
row, col = (12, 12)
i, j = (0, 0)
maze = [
 [
  1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  1, 1, 1, 1, 1, 1],
 [
  1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  0, 0, 0, 1, 0, 1],
 [
  1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 
  1, 1, 0, 1, 0, 1],
 [
  1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 
  0, 0, 0, 1, 0, 1],
 [
  1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 
  0, 1, 1, 1, 0, 1],
 [
  1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 
  0, 1, 0, 0, 0, 1],
 [
  1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  1, 1, 1, 1, 0, 1],
 [
  1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 
  0, 0, 0, 1, 0, 1],
 [
  1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 
  1, 1, 0, 1, 0, 1],
 [
  1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 
  0, 1, 0, 0, 0, 1],
 [
  1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 
  0, 1, 1, 1, 0, 1],
 [
  1, 0, 1, 0, 0, 0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0, 0, 1],
 [
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  1, 1, 1, 1, 0, 1]]
print("Mice walk in a maze: wasd to move,q to quit")
print("flag is the shortest path's md5,example:if the shortest path is wasdsdw,the flag is md5('wasdsdw')")
i, j = (0, 1)
n = 0
while 1:
    if i == row * 2:
        if j == col * 2 - 1:
            print("ohhhh!!!!you did it")
            break
        print("your position:({},{})".format(i, j))
        inp = msvcrt.getch()
        n += 1
        ti, tj = i, j
        if b'a' == inp and i > 0:
            tj -= 1
        else:
            if b'w' == inp and j > 0:
                ti -= 1
            else:
                if b's' == inp and j < row * 2:
                    ti += 1
                else:
                    if b'd' == inp and i < col * 2:
                        tj += 1
                    else:
                        if b'q' == inp:
                            exit("bye!!")
                        else:
                            print("What???")
                            continue
        if maze[ti][tj] == 1:
            print(random.choice(["no wayy!!", "it's wall", "nop"]))
            continue
    elif maze[ti][tj] == 0:
        print(random.choice(["nice!!", "yeah!!", "Go on"]))
        i, j = ti, tj

# okay decompiling 5.pyc

EXP:

#include <iostream>
#include <string>
#include <queue>
using namespace std;
struct point{
  int x,y,sted;
  string path;
};
queue<point> r;
int main()
{
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
char dw[4]={'d','s','a','w'};
int maze[25][25]={
        {1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {
         1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1},
        {
         1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1},
        {
         1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1},
        {
         1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1},
        {
         1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1},
        {
         1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1},
        {
         1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1},
        {
         1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1},
        {
         1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1},
        {
         1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1},
        {
         1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1},
        {
         1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1},
        {
         1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1},
        {
         1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1},
        {
         1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1},
        {
         1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1},
        {
         1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1},
        {
         1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1},
        {
         1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1},
        {
         1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1},
        {
         1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1},
        {
         1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1},
        {
         1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1},
        {
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}}; 
int v[25][25]={0};
int startx=0,starty=1,endx=24,endy=23; 
point start;
start.x=startx;
start.y=starty;
start.sted=0;
start.path="";
r.push(start);
v[startx][starty]=1;
while (!r.empty())
{
  auto current =r.front();
  r.pop();
  if(current.x==endx&&current.y==endy){
    cout<<current.sted<<endl;
    cout<<current.path<<endl;
    break;
  }
  for(int i=0;i<4;i++){
    int tx,ty;
    tx=current.x+dx[i];
    ty=current.y+dy[i];
    if(tx<0||tx>24||ty<0||ty>24)
      continue;
    if(maze[tx][ty]==0&&v[tx][ty]==0){
     point next{tx,ty,current.sted+1,current.path+dw[i]};
      v[tx][ty]=1;
    }
  }
}
}

最短步数: 154(这个不是必须的,只能别人代码有,就跟着学了一遍)

路径方向: sssssddssddssaaaassssddwwddddssssssaawwaassssddssaassddddwwddssddwwwwwwwwaawwddwwwwaaaawwddwwwwddssssddwwwwddddwwddddssaassaassddddssddssaassssssddsssssss

路径要用md5编码后才是flag:

NSSCTF{69193150b15c87d39252d974bc323217}

posted @ 2025-03-21 10:53  漫宿骄盛  阅读(96)  评论(0)    收藏  举报