Https://www.hackerrank.com/challenges/matrix-rotation-algo
Another two hours more than a question, finished just remembered, this is not a few years ago in the POJ did a similar problem: permutation group problem.
Defines a matrix of MXN that allows you to rotate r times counterclockwise from outside to inside. If you're going to turn around again and again, it's out of the hole, because the brute force algorithm is harder to write than the efficient algorithm.
This is actually a special case of permutation, such as giving you a permutation [2, 4, 1, 3], which allows you to transform an array of length 4 by the corresponding relationship, asking the final result.
Did you think of anything? Of course, the fast power of the matrix! What matrix? A 01 sparse matrix .
[2, 4, 1, 3] can be expressed as:
[0 1 0 0]
[0 0 0 1]
[1 0 0 0]
[0 0 1 0]
Contact the linear algebra knowledge of the freshman, which is the corresponding left- multiply transformation of this permutation operation (linear transformation).
Put [A, B, C, d] through a transformation into [B, D, A, c]. So what's the change after 100 million times? In fact, the replacement is a circular section, but it doesn't matter, there is a quick power to worry about the cycle of the festival.
So the transformation of R Times is the matrix to the power of R, so the Fast power algorithm comes in handy.
This problem is similar, but constructs this transformation matrix trouble point, you want to see how the matrix rotates once is how corresponds. After the matrix is obtained, it is easy to find the power.
Time O (N * M * log (R)), as space.
1 ImportRe2 3 defGet2dmatrix (N, M, Val):4 return[[Val forJinchXrange (m)] forIinchxrange (n)]5 6 defRotate (A, R):7 #N is guaranteed to be even8n =Len (a)9m =Len (a[0])Tens =getdisplace (n, m) Ones =Displacepow (S, R) Ab =Get2dmatrix (n, m, 0) - forIinchxrange (n): - forJinchxrange (m): theB[I][J] = a[s[i * m + j]/M][s[i * m + j]%m] - returnb - - defgetdisplace (N, m): +s = Range (n *m) -i =0 + whileI < N/2 andI < M/2: ARR = n-2 *I atCC = M-2 *I - forJinchXrange (1, RR, 1): - # Left -s[(j + i) * m + i] = (j-1 + i) * m +I - forJinchXrange (1, CC, 1): - # Down ins[(rr-1 + i) * m + (j + i)] = (rr-1 + i) * m + (j-1 +i) - forJinchXrange (Rr-2,-1,-1): to # Right +s[(j + i) * m + (cc-1 + i)] = (j + 1 + i) * m + (cc-1 +i) - forJinchXrange (Cc-2,-1,-1): the #Top *S[i * m + (j + i)] = i * m + (j + 1 +)i) $i + = 1Panax Notoginseng returns - the defMultiply (A, b): +n =Len (a) Ac = [] the forIinchxrange (n): + c.append (A[b[i]) - returnC $ $ defDisplacepow (A, k): - ifK = = 1: - returna[:] theA2 = Displacepow (A, K >> 1) - ifK & 1:Wuyi returnMultiply (Multiply (A2, A2), a) the Else: - returnMultiply (A2, A2) Wu - if __name__=='__main__': AboutN, m, r = map (int, re.split ('\s+', Raw_input (). Strip ())) $A = [] - forIinchxrange (n): -A.append (Map (int, re.split ('\s+', Raw_input (). Strip () ))) -A =Rotate (A, R) A forIinchxrange (n): + Print(' '. Join (Map (str, a[i ))) the
Hackerrank-[Algo] Matrix Rotation