Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "bufio"
- "fmt"
- "os"
- "strconv"
- "strings"
- )
- func main() {
- scanner := bufio.NewScanner(os.Stdin)
- buf := make([]byte, 1e4)
- scanner.Buffer(buf, 1e4)
- scanner.Scan()
- sizeAndDirection := strings.Fields(scanner.Text())
- n, _ := strconv.Atoi(sizeAndDirection[0])
- direction := sizeAndDirection[1]
- matrix := make([][]int64, n)
- for i := 0; i < n; i++ {
- scanner.Scan()
- matrix[i] = createIntSlice(n, strings.Fields(scanner.Text()))
- }
- operations := rotateMatrix(n, matrix, direction)
- //operations := rotateMatrix(n, direction)
- fmt.Println(len(operations))
- for _, op := range operations {
- fmt.Println(op[0], op[1], op[2], op[3])
- }
- }
- // func rotateMatrix(n int, direction string) [][]int {
- // operations := [][]int{}
- // for i := 0; i < n/2; i++ {
- // for j := i; j < n-i-1; j++ {
- // x1, y1 := i, j
- // x2, y2 := j, n-i-1
- // x3, y3 := n-i-1, n-j-1
- // x4, y4 := n-j-1, i
- // if direction == "R" {
- // operations = append(operations, []int{x1, y1, x4, y4})
- // operations = append(operations, []int{x4, y4, x3, y3})
- // operations = append(operations, []int{x3, y3, x2, y2})
- // } else {
- // operations = append(operations, []int{x1, y1, x2, y2})
- // operations = append(operations, []int{x2, y2, x3, y3})
- // operations = append(operations, []int{x3, y3, x4, y4})
- // }
- // }
- // }
- // return operations
- // }
- func rotateMatrix(n int, matrix [][]int64, direction string) [][]int {
- operations := [][]int{}
- for i := 0; i < n/2; i++ {
- for j := i; j < n-i-1; j++ {
- x1, y1 := i, j
- x2, y2 := j, n-i-1
- x3, y3 := n-i-1, n-j-1
- x4, y4 := n-j-1, i
- if direction == "R" {
- if matrix[x1][y1] != matrix[x4][y4] {
- operations = append(operations, []int{x1, y1, x4, y4})
- matrix[x1][y1], matrix[x4][y4] = matrix[x4][y4], matrix[x1][y1]
- }
- if matrix[x4][y4] != matrix[x3][y3] {
- operations = append(operations, []int{x4, y4, x3, y3})
- matrix[x4][y4], matrix[x3][y3] = matrix[x3][y3], matrix[x4][y4]
- }
- if matrix[x3][y3] != matrix[x2][y2] {
- operations = append(operations, []int{x3, y3, x2, y2})
- matrix[x3][y3], matrix[x2][y2] = matrix[x2][y2], matrix[x3][y3]
- }
- } else {
- if matrix[x1][y1] != matrix[x2][y2] {
- operations = append(operations, []int{x1, y1, x2, y2})
- matrix[x1][y1], matrix[x2][y2] = matrix[x2][y2], matrix[x1][y1]
- }
- if matrix[x2][y3] != matrix[x3][y3] {
- operations = append(operations, []int{x2, y2, x3, y3})
- matrix[x2][y2], matrix[x3][y3] = matrix[x3][y3], matrix[x2][y2]
- }
- if matrix[x3][y3] != matrix[x4][y4] {
- operations = append(operations, []int{x3, y3, x4, y4})
- matrix[x3][y3], matrix[x4][y4] = matrix[x4][y4], matrix[x3][y3]
- }
- }
- }
- }
- return operations
- }
- func createIntSlice(n int, slice []string) []int64 {
- arr := make([]int64, 0, n)
- for _, s := range slice {
- if s != "" && s != " " {
- num, _ := strconv.ParseInt(s, 10, 64)
- arr = append(arr, num)
- }
- }
- return arr
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement