2020-07-13 17:02:04 +08:00
|
|
|
/*
|
|
|
|
|
* @Author : huangzj
|
|
|
|
|
* @Time : 2020/7/13 15:08
|
|
|
|
|
* @Description:矩阵旋转问题解决(这个是之前在2048游戏里面)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package problem
|
|
|
|
|
|
2020-12-16 11:30:31 +08:00
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
2020-07-13 17:02:04 +08:00
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
SIZE = 4
|
|
|
|
|
)
|
|
|
|
|
|
2020-12-16 11:30:31 +08:00
|
|
|
func TestMatrixRotationTest(t *testing.T) {
|
2020-07-13 17:02:04 +08:00
|
|
|
//矩阵旋转.....
|
|
|
|
|
e := [SIZE][SIZE]int{{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11}, {12, 13, 14, 15}}
|
|
|
|
|
MatrixRotation(e)
|
|
|
|
|
MatrixRotationAnti(e)
|
|
|
|
|
MatrixRotationHalfCircle(e)
|
|
|
|
|
MatrixRotationAntiHalfCircle(e)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//矩阵旋转-逆时针旋转90度
|
|
|
|
|
func MatrixRotationAnti(matrix [SIZE][SIZE]int) [SIZE][SIZE]int {
|
|
|
|
|
var temp [SIZE][SIZE]int
|
|
|
|
|
for i := 0; i < SIZE; i++ {
|
|
|
|
|
for j := 0; j < SIZE; j++ {
|
|
|
|
|
temp[SIZE-j-1][i] = matrix[i][j]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fmt.Println(temp)
|
|
|
|
|
return temp
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//顺时针旋转90度
|
|
|
|
|
func MatrixRotation(matrix [SIZE][SIZE]int) [SIZE][SIZE]int {
|
|
|
|
|
var temp [SIZE][SIZE]int
|
|
|
|
|
for i := 0; i < SIZE; i++ {
|
|
|
|
|
for j := 0; j < SIZE; j++ {
|
|
|
|
|
temp[j][SIZE-1-i] = matrix[i][j]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fmt.Println(temp)
|
|
|
|
|
return temp
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//矩阵逆时针旋转180度 --顺逆时针结果一致
|
|
|
|
|
func MatrixRotationAntiHalfCircle(matrix [SIZE][SIZE]int) [SIZE][SIZE]int {
|
|
|
|
|
var temp [SIZE][SIZE]int
|
|
|
|
|
for i := 0; i < SIZE; i++ {
|
|
|
|
|
for j := 0; j < SIZE; j++ {
|
|
|
|
|
temp[SIZE-1-i][SIZE-1-j] = matrix[i][j]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fmt.Println(temp)
|
|
|
|
|
return temp
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//矩阵顺时针旋转180度
|
|
|
|
|
func MatrixRotationHalfCircle(matrix [SIZE][SIZE]int) [SIZE][SIZE]int {
|
|
|
|
|
var temp [SIZE][SIZE]int
|
|
|
|
|
for i := 0; i < SIZE; i++ {
|
|
|
|
|
for j := 0; j < SIZE; j++ {
|
|
|
|
|
temp[SIZE-1-i][SIZE-1-j] = matrix[i][j]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fmt.Println(temp)
|
|
|
|
|
return temp
|
|
|
|
|
}
|