问题描述
请将此代码更改为c ++
来自随机导入randint
R,L,D,U,SOLVE = 1,2,4,8,16RLDU = [R,L,D,U] MOVE_X = [0,0,1,-1]
MOVE_Y = [1,-1,0,0]
def make_way(x1,y1,x2,y2,org_maze,solve = 0):
x_size = len(org_maze)
y_size = len(org_maze [0] )
cnt = 0
而cnt< x_size * y_size:
cnt + = 1
x,y = x1,y1
maze = [org_maze中行的列表(行)]
访问= []
而True:
direction = randint(0,3)
如果参观方向:
继续
其他:
访问+ = [方向]
next_x = x + MOVE_X [方向]
next_y = y + MOVE_Y [方向]
如果-1< next_x< x_size和-1< next_y< y_size而不是is_way_xy(next_x,next_y,迷宫):
迷宫[x] [y] | = RLDU [方向]
如果解决==解决:
迷宫[x] [y] | =解决
x = next_x
y = next_y
访问= []
如果next_x == x2和next_y == y2:
返回迷宫
如果len(已访问)== 4:
休息
def random_xy(迷宫,is_way):
x_size = len(迷宫)
y_size = len(迷宫[0])
而True:
x = randint(0,x_size - 1)
y = randint(0,y_size - 1)
if(x == 0和y) == 0)或(x == x_size - 1和y == y_size - 1):
继续
如果is_way和迷宫[x] [y]!= 0:
返回x,y
如果不是is_way而不是is_way_xy(x,y,迷宫):
返回x,y
def is_way_xy(x,y,迷宫):
如果迷宫[x] [y]!= 0:
返回True
否则:
如果y> 0和迷宫[x] [y - 1]& R:
返回True
如果y< y_size - 1和迷宫[x] [y + 1]& L:
返回True
如果x> 0和迷宫[x - 1] [y]& D:
返回True
如果x< x_size - 1和迷宫[x + 1] [y]& U:
返回True
返回False
x_size = 10
y_size = 10
wrong_way = 10
迷宫= [[0] * y_size for i in range( x_size)]
maze = make_way(0,0,x_size - 1,y_size - 1,迷宫,SOLVE)
for i in range(wrong_way):
而True:
x1,y1 = random_xy(迷宫,真)
x2,y2 = random_xy(迷宫,假)
next_maze = make_way(x1,y1,x2,y2,迷宫)
if next_maze:
break
maze = next_maze
我尝试过:
i不懂python。
请帮帮我。
你没有表现出最轻微的努力,你没有被困,你没有问题,所以更容易让我们去做你的家庭作业。
我们不做你的HomeWork。
我想你不会喜欢第三种选择:
3)学习Python,它并不复杂。
如果你敢于搜索,互联网上充满了资源。
Please change this code to c++
from random import randint
R, L, D, U, SOLVE = 1, 2, 4, 8, 16RLDU = [R, L, D, U]MOVE_X = [0, 0, 1, -1]
MOVE_Y = [1, -1, 0, 0]
def make_way(x1, y1, x2, y2, org_maze, solve=0):
x_size = len(org_maze)
y_size = len(org_maze[0])
cnt = 0
while cnt < x_size * y_size:
cnt += 1
x, y = x1, y1
maze = [list(row) for row in org_maze]
visited = []
while True:
direction = randint(0, 3)
if direction in visited:
continue
else:
visited += [direction]
next_x = x + MOVE_X[direction]
next_y = y + MOVE_Y[direction]
if -1 < next_x < x_size and -1 < next_y < y_size and not is_way_xy(next_x, next_y, maze):
maze[x][y] |= RLDU[direction]
if solve == SOLVE:
maze[x][y] |= SOLVE
x = next_x
y = next_y
visited = []
if next_x == x2 and next_y == y2:
return maze
if len(visited) == 4:
break
def random_xy(maze, is_way):
x_size = len(maze)
y_size = len(maze[0])
while True:
x = randint(0, x_size - 1)
y = randint(0, y_size - 1)
if (x == 0 and y == 0) or (x == x_size - 1 and y == y_size - 1):
continue
if is_way and maze[x][y] != 0:
return x, y
if not is_way and not is_way_xy(x, y, maze):
return x, y
def is_way_xy(x, y, maze):
if maze[x][y] != 0:
return True
else:
if y > 0 and maze[x][y - 1] & R:
return True
if y < y_size - 1 and maze[x][y + 1] & L:
return True
if x > 0 and maze[x - 1][y] & D:
return True
if x < x_size - 1 and maze[x + 1][y] & U:
return True
return False
x_size = 10
y_size = 10
wrong_way = 10
maze = [[0] * y_size for i in range(x_size)]
maze = make_way(0, 0, x_size - 1, y_size - 1, maze, SOLVE)
for i in range(wrong_way):
while True:
x1, y1 = random_xy(maze, True)
x2, y2 = random_xy(maze, False)
next_maze = make_way(x1, y1, x2, y2, maze)
if next_maze:
break
maze = next_maze
What I have tried:
i don't know python.
please help me.
You don't show the slightest effort, you are not stuck, you don't have a question, So it is easier to just ask us to do your HomeWork.
We don't do your HomeWork.
I guess you will not like the third option:
3) Learn Python, it is not so complicated.
Internet is full of resources if you dare to search.
这篇关于Python代码 - > C ++代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!