Advertisement
Egmell

4

Apr 18th, 2024
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 3.02 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "bufio"
  5.     "fmt"
  6.     "os"
  7.     "strconv"
  8.     "strings"
  9. )
  10.  
  11. func main() {
  12.     scanner := bufio.NewScanner(os.Stdin)
  13.     buf := make([]byte, 1e4)
  14.     scanner.Buffer(buf, 1e4)
  15.  
  16.     scanner.Scan()
  17.     sizeAndDirection := strings.Fields(scanner.Text())
  18.  
  19.     n, _ := strconv.Atoi(sizeAndDirection[0])
  20.     direction := sizeAndDirection[1]
  21.  
  22.     matrix := make([][]int64, n)
  23.     for i := 0; i < n; i++ {
  24.         scanner.Scan()
  25.         matrix[i] = createIntSlice(n, strings.Fields(scanner.Text()))
  26.     }
  27.     operations := rotateMatrix(n, matrix, direction)
  28.     //operations := rotateMatrix(n, direction)
  29.  
  30.     fmt.Println(len(operations))
  31.     for _, op := range operations {
  32.         fmt.Println(op[0], op[1], op[2], op[3])
  33.     }
  34. }
  35.  
  36. // func rotateMatrix(n int, direction string) [][]int {
  37. //  operations := [][]int{}
  38.  
  39. //  for i := 0; i < n/2; i++ {
  40. //      for j := i; j < n-i-1; j++ {
  41. //          x1, y1 := i, j
  42. //          x2, y2 := j, n-i-1
  43. //          x3, y3 := n-i-1, n-j-1
  44. //          x4, y4 := n-j-1, i
  45.  
  46. //          if direction == "R" {
  47. //              operations = append(operations, []int{x1, y1, x4, y4})
  48. //              operations = append(operations, []int{x4, y4, x3, y3})
  49. //              operations = append(operations, []int{x3, y3, x2, y2})
  50. //          } else {
  51. //              operations = append(operations, []int{x1, y1, x2, y2})
  52. //              operations = append(operations, []int{x2, y2, x3, y3})
  53. //              operations = append(operations, []int{x3, y3, x4, y4})
  54. //          }
  55. //      }
  56. //  }
  57. //  return operations
  58. // }
  59.  
  60. func rotateMatrix(n int, matrix [][]int64, direction string) [][]int {
  61.     operations := [][]int{}
  62.  
  63.     for i := 0; i < n/2; i++ {
  64.         for j := i; j < n-i-1; j++ {
  65.             x1, y1 := i, j
  66.             x2, y2 := j, n-i-1
  67.             x3, y3 := n-i-1, n-j-1
  68.             x4, y4 := n-j-1, i
  69.  
  70.             if direction == "R" {
  71.                 if matrix[x1][y1] != matrix[x4][y4] {
  72.                     operations = append(operations, []int{x1, y1, x4, y4})
  73.                     matrix[x1][y1], matrix[x4][y4] = matrix[x4][y4], matrix[x1][y1]
  74.                 }
  75.                 if matrix[x4][y4] != matrix[x3][y3] {
  76.                     operations = append(operations, []int{x4, y4, x3, y3})
  77.                     matrix[x4][y4], matrix[x3][y3] = matrix[x3][y3], matrix[x4][y4]
  78.                 }
  79.                 if matrix[x3][y3] != matrix[x2][y2] {
  80.                     operations = append(operations, []int{x3, y3, x2, y2})
  81.                     matrix[x3][y3], matrix[x2][y2] = matrix[x2][y2], matrix[x3][y3]
  82.                 }
  83.             } else {
  84.                 if matrix[x1][y1] != matrix[x2][y2] {
  85.                     operations = append(operations, []int{x1, y1, x2, y2})
  86.                     matrix[x1][y1], matrix[x2][y2] = matrix[x2][y2], matrix[x1][y1]
  87.                 }
  88.                 if matrix[x2][y3] != matrix[x3][y3] {
  89.                     operations = append(operations, []int{x2, y2, x3, y3})
  90.                     matrix[x2][y2], matrix[x3][y3] = matrix[x3][y3], matrix[x2][y2]
  91.                 }
  92.                 if matrix[x3][y3] != matrix[x4][y4] {
  93.                     operations = append(operations, []int{x3, y3, x4, y4})
  94.                     matrix[x3][y3], matrix[x4][y4] = matrix[x4][y4], matrix[x3][y3]
  95.                 }
  96.             }
  97.         }
  98.     }
  99.     return operations
  100. }
  101.  
  102. func createIntSlice(n int, slice []string) []int64 {
  103.     arr := make([]int64, 0, n)
  104.     for _, s := range slice {
  105.         if s != "" && s != " " {
  106.             num, _ := strconv.ParseInt(s, 10, 64)
  107.             arr = append(arr, num)
  108.         }
  109.     }
  110.     return arr
  111. }
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement