逆向常见题目—迷宫类题目
迷宫(maze)
思路:
1.找到地图(字符串)
2.找到方向(上左下右)
3.找到起点到终点
然后将路径输出即可
特征: 标题,hint为maze 或者 看到字符串###等等
整理字符串为图形.py
(要是不是正方形需要自己输出行和列)
import math
def arrange_string_to_square():
# 获取用户输入的字符串
input_str = input("请输入要整理的字符串: ").strip()
length = len(input_str)
# 检查是否是完美平方数
sqrt = math.isqrt(length)
if sqrt * sqrt == length:
# 是完美平方数,自动确定行列数
rows = cols = sqrt
print(f"字符串长度{length}是完美平方数({sqrt}x{sqrt}),自动整理为{sqrt}x{sqrt}的图形。")
else:
# 不是完美平方数,让用户选择行列数
print(f"字符串长度{length}不是完美平方数,请指定行列数。")
while True:
try:
rows = int(input("请输入行数: "))
cols = int(input("请输入列数: "))
if rows * cols != length:
print(f"错误:行数×列数({rows}x{cols}={rows * cols})不等于字符串长度({length})")
continue
break
except ValueError:
print("请输入有效的整数!")
# 按行列数整理字符串
for i in range(rows):
start = i * cols
end = start + cols
print(input_str[start:end])
# 返回行列数供后续使用
return rows, cols
# 调用函数
rows, cols = arrange_string_to_square()
请输入要整理的字符串: S ####### ## #### # ##### ### # ####### ## #######E#
字符串长度64是完美平方数(8x8),自动整理为8x8的图形。
S ######
# ## ##
## # ##
### ##
# # #
###### #
# #
######E#
整理图形到输出路径.py
import math
from collections import deque
def create_grid(input_str, rows=None, cols=None):
"""将字符串转换为二维网格"""
length = len(input_str)
if rows is None or cols is None:
sqrt = math.isqrt(length)
if sqrt * sqrt == length:
rows = cols = sqrt
print(f"字符串长度{length}是完美平方数({sqrt}x{sqrt}),自动整理为{sqrt}x{sqrt}的图形。")
else:
raise ValueError("字符串长度不是完美平方数,必须指定行列数")
if rows * cols != length:
raise ValueError(f"行数×列数({rows}x{cols}={rows * cols})不等于字符串长度({length})")
grid = []
for i in range(rows):
start = i * cols
end = start + cols
grid.append(list(input_str[start:end]))
return grid
def print_grid(grid):
"""打印网格(不添加额外空格)"""
for row in grid:
print(''.join(row))
print()
def find_position(grid, char):
"""查找字符在网格中的位置"""
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == char:
return (i, j)
return None
def find_path(grid, start_char, end_char, moves):
"""使用BFS算法查找从起点到终点的路径,路径上只能走空格(起点和终点除外)"""
start = find_position(grid, start_char)
end = find_position(grid, end_char)
if not start:
raise ValueError(f"起点字符 '{start_char}' 不存在于网格中")
if not end:
raise ValueError(f"终点字符 '{end_char}' 不存在于网格中")
rows = len(grid)
cols = len(grid[0]) if rows > 0 else 0
directions = {
'w': (-1, 0), # 上
's': (1, 0), # 下
'a': (0, -1), # 左
'd': (0, 1) # 右
}
queue = deque([(start, [])])
visited = set([start])
while queue:
(x, y), path = queue.popleft()
if (x, y) == end:
return path
for move in moves:
dx, dy = directions[move]
nx, ny = x + dx, y + dy
# 检查新位置是否有效
if 0 <= nx < rows and 0 <= ny < cols and (nx, ny) not in visited:
# 如果是终点,直接可以到达
if (nx, ny) == end:
return path + [move]
# 路径中间必须是空格
if grid[nx][ny] == ' ':
visited.add((nx, ny))
queue.append(((nx, ny), path + [move]))
return None
def main():
input_str = input("请输入要整理的字符串: ").strip()
length = len(input_str)
try:
grid = create_grid(input_str)
except ValueError:
print(f"字符串长度{length}不是完美平方数,请指定行列数。")
while True:
try:
rows = int(input("请输入行数: "))
cols = int(input("请输入列数: "))
grid = create_grid(input_str, rows, cols)
break
except ValueError as e:
print(e)
print("\n生成的网格:")
print_grid(grid)
start_char = input("请输入起点字符: ").strip()
end_char = input("请输入终点字符: ").strip()
moves_input = input("请输入移动指令序列(WASD,如'wasd','上左下右'): ").strip().lower()
valid_moves = {'w', 'a', 's', 'd'}
moves = [m for m in moves_input if m in valid_moves]
if not moves:
print("错误: 没有输入有效的移动指令(WASD)")
return
path = find_path(grid, start_char, end_char, moves)
if path:
print(f"\n从 '{start_char}' 到 '{end_char}' 的路径:")
print(''.join(path)) # 修改为直接输出连接后的字符串
else:
print(f"\n无法从 '{start_char}' 到达 '{end_char}' 使用给定的移动指令")
if __name__ == "__main__":
main()
请输入要整理的字符串: S ####### ## #### # ##### ### # ####### ## #######E#
字符串长度64是完美平方数(8x8),自动整理为8x8的图形。
生成的网格:
S ######
# ## ##
## # ##
### ##
# # #
###### #
# #
######E#
请输入起点字符: S
请输入终点字符: E
请输入移动指令序列(WASD,如'wasd','上左下右'): wasd
从 'S' 到 'E' 的路径:
dsdsdsddsdsss
进程已结束,退出代码为 0
例题1--NSSCTF--RE6
ida打开
第一步:找到地图
shift+F6查看字符串
点进去,选中,按A排成一行
复制出来
S ####### ## #### # ##### ### # ####### ## #######E#
发现为64个字符
确认为8x8图形
第二步:找到方向
Tab查看汇编
119 115 97 100
经典是上下左右 wsad
第三步:确认起点终点
8个一行
S ####### ## #### # ##### ### # ####### ## #######E#
确认S为起点,E为终点
输入整理字符串到输出路径.py即可
请输入要整理的字符串: S ####### ## #### # ##### ### # ####### ## #######E#
字符串长度64是完美平方数(8x8),自动整理为8x8的图形。
生成的网格:
S ######
# ## ##
## # ##
### ##
# # #
###### #
# #
######E#
请输入起点字符: S
请输入终点字符: E
请输入移动指令序列(WASD,如'wasd','上左下右'): wasd
从 'S' 到 'E' 的路径:
dsdsdsddsdsss
进程已结束,退出代码为 0