你能查看我的 python 代码并更正它们吗?它与算法问题(实现)有关
can you review my python code and correct them? It's related with algorithm problem(implementation)
数组(5,5)
(1,1) (1,2) (1,3) (1,4) (1,5)
(2,1) (2,2) (2,3) (2,4) (2,5)
(3,1) (3,2) (3,3) (3,4) (3,5)
(4,1) (4,2) (4,3) (4,4) (4,5)
(5,1) (5,2) (5,3) (5,4) (5,5)
我们从 (1,1) 开始 /
'L' 表示 'move to the left for 1' /
'R' 表示 'move to the right for 1' /
'U' 表示'移动到上侧 1 /
'D' 表示“向下移动 1 /
如果你把/
5 /
R R /
那么你会得到 (1,3)
如果你把/
5 /
研发 /
那么你会得到 (2,3)
如果你把/
5 /
R R D L /
那么你会得到 (2,2)
但是如果你把/
5 /
R R U /
那么你将得到 (1,3) 而不是 (0,3) /
即使你把'U'(往上边)也没有办法去。 /
所以答案不是 (0,3),而是 (1,3) /
#-----------------------------------------
n=int(input('insert n : '))
x,y=0,0
run=input('move plan : ').split()
for r in run:
if r=='L':
if y==0:
continue
else:
y-=1
elif r=='R':
if y==n-1:
continue
else:
y+=1
elif r=='U':
if x==0:
continue
else:
x-=1
elif r=='D':
if x==n-1:
continue
else:
x+=1
else:
continue
print(x+1,y+1)
#---------------------------------------- --------------------------------------
以上是正确的代码,运行很好,我本来打算实现的。
为了获得此代码,我多次尝试实现代码。
这里的代码不太好 运行 但我不知道下面这段代码有什么问题
.能顺利把代码改成运行吗?
#---------------------------------------- --------------------------------------
n=int(input('insert n : '))
array=[[0 for i in range(n)] for j in range(n)]
array[0][0]=1
run=input('move plan : ').split()
for r in run:
for i,j in zip(range(n), range(n)):
if r=='L' and array[i][j]==1:
if j==0:
continue
else:
array[i][j],array[i][j-1]=array[i][j-1],array[i][j]
elif r=='R' and array[i][j]==1:
if j==n-1:
continue
else:
array[i][j],array[i][j+1]=array[i][j+1],array[i][j]
elif r=='U' and array[i][j]==1:
if i==0:
continue
else:
array[i][j],array[i-1][j]=array[i-1][j],array[i][j]
elif r=='D' and array[i][j]==1:
if i==n-1:
continue
else:
array[i][j],array[i+1][j]=array[i+1][j],array[i][j]
else:
continue
for i in range(n):
print(array[i])
"""
put
5
R R R U D D
out
3 4
"""
问题是 - 当我 运行 代码时,它停在位置 (1,2)...
下面这行没有意义:
for i,j in zip(range(n), range(n)):
您已经在循环移动,因此不需要其他(嵌套)循环:每个移动只会执行 一个 动作。
这个循环将产生 i
和 j
对 i == j
。所以它让你沿着网格的对角线走。你没有理由那样做。
你应该在外循环开始之前将i
和j
初始化为零,然后在if
条件下你应该根据移动更新 i
或 j
(或两者都不更新)。
附带说明:您决定创建矩阵并用 1 标记单元格。将矩阵保存在内存中有助于可视化您所走过的路径,但最终它对算法来说并不是必需的。本质上,您只需要跟踪坐标;对于调试,您可以输出这些而不是矩阵。
数组(5,5)
(1,1) (1,2) (1,3) (1,4) (1,5)
(2,1) (2,2) (2,3) (2,4) (2,5)
(3,1) (3,2) (3,3) (3,4) (3,5)
(4,1) (4,2) (4,3) (4,4) (4,5)
(5,1) (5,2) (5,3) (5,4) (5,5)
我们从 (1,1) 开始 / 'L' 表示 'move to the left for 1' / 'R' 表示 'move to the right for 1' / 'U' 表示'移动到上侧 1 / 'D' 表示“向下移动 1 /
如果你把/ 5 / R R / 那么你会得到 (1,3)
如果你把/ 5 / 研发 / 那么你会得到 (2,3)
如果你把/ 5 / R R D L / 那么你会得到 (2,2)
但是如果你把/ 5 / R R U / 那么你将得到 (1,3) 而不是 (0,3) / 即使你把'U'(往上边)也没有办法去。 / 所以答案不是 (0,3),而是 (1,3) /
#-----------------------------------------
n=int(input('insert n : '))
x,y=0,0
run=input('move plan : ').split()
for r in run:
if r=='L':
if y==0:
continue
else:
y-=1
elif r=='R':
if y==n-1:
continue
else:
y+=1
elif r=='U':
if x==0:
continue
else:
x-=1
elif r=='D':
if x==n-1:
continue
else:
x+=1
else:
continue
print(x+1,y+1)
#---------------------------------------- --------------------------------------
以上是正确的代码,运行很好,我本来打算实现的。 为了获得此代码,我多次尝试实现代码。 这里的代码不太好 运行 但我不知道下面这段代码有什么问题 .能顺利把代码改成运行吗?
#---------------------------------------- --------------------------------------
n=int(input('insert n : '))
array=[[0 for i in range(n)] for j in range(n)]
array[0][0]=1
run=input('move plan : ').split()
for r in run:
for i,j in zip(range(n), range(n)):
if r=='L' and array[i][j]==1:
if j==0:
continue
else:
array[i][j],array[i][j-1]=array[i][j-1],array[i][j]
elif r=='R' and array[i][j]==1:
if j==n-1:
continue
else:
array[i][j],array[i][j+1]=array[i][j+1],array[i][j]
elif r=='U' and array[i][j]==1:
if i==0:
continue
else:
array[i][j],array[i-1][j]=array[i-1][j],array[i][j]
elif r=='D' and array[i][j]==1:
if i==n-1:
continue
else:
array[i][j],array[i+1][j]=array[i+1][j],array[i][j]
else:
continue
for i in range(n):
print(array[i])
"""
put
5
R R R U D D
out
3 4
"""
问题是 - 当我 运行 代码时,它停在位置 (1,2)...
下面这行没有意义:
for i,j in zip(range(n), range(n)):
您已经在循环移动,因此不需要其他(嵌套)循环:每个移动只会执行 一个 动作。
这个循环将产生
i
和j
对i == j
。所以它让你沿着网格的对角线走。你没有理由那样做。
你应该在外循环开始之前将i
和j
初始化为零,然后在if
条件下你应该根据移动更新 i
或 j
(或两者都不更新)。
附带说明:您决定创建矩阵并用 1 标记单元格。将矩阵保存在内存中有助于可视化您所走过的路径,但最终它对算法来说并不是必需的。本质上,您只需要跟踪坐标;对于调试,您可以输出这些而不是矩阵。