给定一个维度为 NxN 的方阵 A。您需要对矩阵 A 应用下面给出的 3 个操作。
旋转:表示为 R S 其中 S 是 {90, 180, 270, 360, 450, ...} 中的整数,表示要旋转的度数。您需要以顺时针方向将矩阵 A 旋转角度 S。旋转角度 (S) 将始终为 90 度的倍数。
更新:表示为 U X Y Z。在初始矩阵 A(如输入中给出)中,您需要使用值 Z 更新行索引 X 和列索引 Y 处的元素。 更新后,所有先前的旋转操作都必须应用于更新后的初始矩阵。
查询:用Q K L表示,需要打印矩阵A的行索引K和列索引L处的值。
这是我的问题。这是我到目前为止所做的:
def ReadMatrix():
matrix = []
for i in range(int(input())):
row = [int(j) for j in input().split()]
matrix.append(row)
return matrix
def RotateMatrix(matrix, degrees):
n = len(matrix[0])
rotations = (degrees // 90) % 4
for r in range(rotations):
temp_matrix = []
for i in range(n):
column = [row[i] for row in matrix]
column.reverse()
temp_matrix.append(column)
matrix = temp_matrix
return matrix
matrix = ReadMatrix()
rotation = 0
while True:
line = input().split()
if line[0] == "-1":
break;
elif line[0] == "R":
rotation += int(line[1])
matrix = RotateMatrix(matrix, int(line[1]))
elif line[0] == "U":
matrix[int(line[1])][int(line[2])] = int(line[3])
matrix = RotateMatrix(matrix, rotation)
elif line[0] == "Q":
print(matrix[int(line[1])][int(line[2])])
else:
print(line[0])
exit(1)
我得到了这个输入的正确输出:
2
1 2
3 4
R 90
Q 0 0
Q 0 1
R 90
Q 0 0
U 0 0 6
Q 1 1
-1
但是我没有得到这个输入的正确输出
2
5 6
7 8
R 90
Q 0 1
R 270
Q 1 1
R 180
U 0 0 4
Q 0 0
-1
我为第二个输入得到的输出是 5 8 5
,但正确的输出是 5 8 8
。
答案 0 :(得分:2)
你们很亲近。你当然知道。 :)
您遇到的问题是您没有明确遵循更新操作的说明。它说您应该将更新应用于原始矩阵,然后重新旋转它。您正在将更新应用于已经旋转的矩阵,然后重新旋转它。
所以。我们如何解决?为此,我们需要维护原始矩阵的副本,然后将其用作更新命令的基础。 Python 有一个内置的 copy()
命令,在这种情况下它可以正常工作,因为您要复制的是整数类型列表(而不是引用变量)。
考虑像这样捕获原始矩阵:
matrix = ReadMatrix()
orig_matrix = matrix.copy()
然后您可以根据需要更新它并将更新后的矩阵传递到您的重新旋转操作中。
如果您想逐个观看比赛以验证发生了什么,我建议在循环的末尾添加一个小打印语句,当您对一切都感到满意时可以将其注释掉......
>...
else:
print(line[0])
sys.exit(1)
print('matrix:', matrix)
print('orig :', orig_matrix)
答案 1 :(得分:0)
exit=1
ar=[]
pr=[]
up=0
pu=0
def array_rot(rang, ar):
for k in range(rang):
for i in range(a//2):
c=''
for j in range(i, a-i-1):
temp=ar[i][j]
ar[i][j]=ar[a-1-j][i]
ar[a-1-j][i]=ar[a-1-i][a-1-j]
ar[a-1-i][a-1-j]=ar[j][a-1-i]
ar[j][a-1-i]=temp
return ar
for i in range(a):
inp=input().split()
ar.append(inp)
while(exit!=-1):
q=input()
r=q.split()
if(q[0]=='R'):
rang=int(r[1])//90
up=up+rang
pu=rang+pu
ar=array_rot(rang, ar)
elif(q[0]=='U'):
ran=0
while(up%4!=0):
ran=ran+1
up=up+1
ar=array_rot(ran, ar)
ar[int(r[1])][int(r[2])]=r[3]
ar=array_rot(pu,pr)
pu=0
elif(q[0]=='Q'):
print(ar[int(r[1])][int(r[2])])
elif(r[0]=='-1'):
exit=-1
答案 2 :(得分:0)
a = int(input())
matrix = []
rotation = 0
for i in range(a):
row = [int(j) for j in input().split()]
matrix.append(row)
orig_matrix = matrix.copy()
def RotateMatrix(matrix, degrees):
n = len(matrix[0])
rotations = (degrees // 90) % 4
for r in range(rotations):
temp_matrix = []
for i in range(n):
column = [row[i] for row in matrix]
column.reverse()
temp_matrix.append(column)
matrix = temp_matrix
return matrix
while True:
line = input().split()
if line[0] == "-1":
break;
elif line[0] == "R":
rotation += int(line[1])
matrix = RotateMatrix(matrix, int(line[1]))
elif line[0] == "U":
orig_matrix[int(line[1])][int(line[2])] = int(line[3])
matrix = RotateMatrix(orig_matrix, rotation)
elif line[0] == "Q":
print(matrix[int(line[1])][int(line[2])])
else:
print("Error: unexpected command '" + line[0] + "'")
exit(1)
your code is fine. but miss this condition
Update: It is represented as U X Y Z. In initial matrix A (as given in input), you need to update the element at row index X and column index Y with value Z. **After the update, all the previous rotation operations have to be applied to the *updated initial matrix*.**