Python代码 - > C ++代码 [英] Python code-> C++ code

查看:66
本文介绍了Python代码 - > C ++代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请将此代码更改为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。

请帮帮我。

解决方案

这里有两个选项:

1)我们不做你的作业:它是设置的因为某种原因。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是和你想的一样困难!



如果遇到具体问题,请询问相关问题,我们会尽力提供帮助。但是我们不会为你做这一切!





2)我们不为你工作。

如果你想让别人写你的代码,你必须支付 - 我建议你去Freelancer.com并在那里问。



但是要知道:你得到了你付出的代价。支付花生,买猴子。



发展的概念就像这句话所暗示的那样:系统地运用科学和技术知识来满足特定的目标或要求。 BusinessDictionary.com [ ^ ]

这与有一个不一样快速谷歌并放弃,如果我找不到完全正确的代码。

所以要么付钱给别人去做,要么学会如何自己写。我们不是在这里为你做的。



这两个人都忽略了翻译的代码并不意味着目标语言中的优秀代码 - 通常有很多在每种语言中做一些简单的方法。


引用:

我不知道python。



你没有表现出最轻微的努力,你没有被困,你没有问题,所以更容易让我们去做你的家庭作业。

我们不做你的HomeWork。



我想你不会喜欢第三种选择:

3)学习Python,它并不复杂。



如果你敢于搜索,互联网上充满了资源。


你需要了解那个python代码起初。你最好问一些伙伴或同事。它不应该那么难,如果我没记错,def正在启动一个函数,return结束它。



要用C ++编写,你需要学习一些 C ++教程。在C ++中也是一个很好的兰特函数



比在你的作业中一起烘焙。 ; - )

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.

解决方案

There are two options here:
1) We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!

Or
2) We do not do your work for you.
If you want someone to write your code, you have to pay - I suggest you go to Freelancer.com and ask there.

But be aware: you get what you pay for. Pay peanuts, get monkeys.

The idea of "development" is as the word suggests: "The systematic use of scientific and technical knowledge to meet specific objectives or requirements." BusinessDictionary.com[^]
That's not the same thing as "have a quick google and give up if I can't find exactly the right code".
So either pay someone to do it, or learn how to write it yourself. We aren't here to do it for you.

And both of those ignore that translated code doesn't mean good code in the target language - there are often much simpler ways to do some things in each language.


Quote:

i don't know python.


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.


You need to understand that python code at first. At best you ask some mate or collegue for that. It shouldnt be so hard, if I remember right the "def" is starting a function and "return" ends it.

For writing it in C++ you need to learn some C++ tutorial. In C++ is also a fine rand function.

Than bake all together in YOUR homework. ;-)


这篇关于Python代码 - &gt; C ++代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆