Advertisement
Guest User

Untitled

a guest
Jul 28th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. )
  6.  
  7. type coord struct {
  8. x int
  9. y int
  10. }
  11.  
  12. type matrixRing struct {
  13. matrix [][]int
  14. upperLeftCoord coord
  15. length int
  16. height int
  17. }
  18.  
  19. func (mr *matrixRing) ringLength() int {
  20. if mr.height == 1 {
  21. return mr.length
  22. } else if mr.length == 1 {
  23. return mr.height
  24. } else {
  25. return 2*mr.length + 2*(mr.height-2)
  26. }
  27. }
  28.  
  29. func (mr *matrixRing) shiftRight(n int) {
  30. rotationN := n % mr.ringLength()
  31.  
  32. mr.reverse(0, mr.ringLength()-1)
  33. mr.reverse(0, rotationN-1)
  34. mr.reverse(rotationN, mr.ringLength()-1)
  35. }
  36.  
  37. func (mr *matrixRing) reverse(start int, end int) {
  38. i := start
  39. j := end
  40. for i < j {
  41. mr.swap(i, j)
  42.  
  43. i++
  44. j--
  45. }
  46. }
  47.  
  48. func (mr *matrixRing) swap(indexA int, indexB int) {
  49. coordA := mr.indexToCoord(indexA)
  50. coordB := mr.indexToCoord(indexB)
  51.  
  52. temp := mr.matrix[coordA.y][coordA.x]
  53. mr.matrix[coordA.y][coordA.x] = mr.matrix[coordB.y][coordB.x]
  54. mr.matrix[coordB.y][coordB.x] = temp
  55. }
  56.  
  57. func (mr *matrixRing) indexToCoord(index int) coord {
  58. upperRightIndex := mr.length - 1
  59. bottomRightIndex := upperRightIndex + mr.height - 1
  60. bottomLeftIndex := bottomRightIndex + mr.length - 1
  61.  
  62. if index <= upperRightIndex {
  63. return coord{x: mr.upperLeftCoord.x + index, y: mr.upperLeftCoord.y}
  64. }
  65.  
  66. if index <= bottomRightIndex {
  67. return coord{x: mr.upperLeftCoord.x + mr.length - 1, y: mr.upperLeftCoord.y + index - upperRightIndex}
  68. }
  69.  
  70. if index <= bottomLeftIndex {
  71. bottomRightX := mr.upperLeftCoord.x + mr.length - 1
  72. return coord{x: bottomRightX - (index - bottomRightIndex), y: mr.upperLeftCoord.y + mr.height - 1}
  73. }
  74.  
  75. bottomLeftY := mr.upperLeftCoord.y + mr.height - 1
  76. return coord{x: mr.upperLeftCoord.x, y: bottomLeftY - (index - bottomLeftIndex)}
  77. }
  78.  
  79. func main() {
  80. matrix1 := [][]int{{1, 2, 3, 4, 5}}
  81. testRotation(matrix1, 3)
  82.  
  83. matrix2 := [][]int{
  84. {1},
  85. {2},
  86. {3},
  87. {4},
  88. }
  89. testRotation(matrix2, 3)
  90.  
  91. matrix3 := [][]int{
  92. {1, 2},
  93. {3, 4},
  94. {5, 6},
  95. }
  96. testRotation(matrix3, 3)
  97.  
  98. matrix4 := [][]int{
  99. {1, 2, 3},
  100. {4, 5, 6},
  101. }
  102. testRotation(matrix4, 3)
  103.  
  104. matrix5 := [][]int{
  105. {0, 1, 2, 3},
  106. {4, 5, 6, 7},
  107. {8, 9, 0, 1},
  108. }
  109. testRotation(matrix5, 3)
  110. }
  111.  
  112. func testRotation(matrix [][]int, n int) {
  113. printMatrix(matrix)
  114. rotateMatrix(matrix, n)
  115.  
  116. fmt.Print("-----------------------\n")
  117. printMatrix(matrix)
  118. fmt.Println()
  119. }
  120.  
  121. func printMatrix(matrix [][]int) {
  122. for _, row := range matrix {
  123. fmt.Printf("%v\n", row)
  124. }
  125. }
  126.  
  127. func rotateMatrix(matrix [][]int, n int) {
  128. ringHeight := len(matrix)
  129. ringLength := len(matrix[0])
  130. numRings := (min(ringHeight, ringLength) - 1) / 2
  131.  
  132. for i := 0; i <= numRings; i++ {
  133. ring := matrixRing{
  134. matrix: matrix,
  135. upperLeftCoord: coord{x: i, y: i},
  136. length: ringLength,
  137. height: ringHeight,
  138. }
  139.  
  140. ring.shiftRight(n)
  141.  
  142. ringHeight -= 2
  143. ringLength -= 2
  144. }
  145. }
  146.  
  147. func min(a, b int) int {
  148. if a < b {
  149. return a
  150. }
  151. return b
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement