main.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package main
  2. import "fmt"
  3. // MAXN ...
  4. const MAXN int = 100
  5. // N ...
  6. var N int
  7. var row, col []bool
  8. var diag1, diag2 []bool
  9. // < 0 means orignal solution, > 0 means new solution
  10. var crossSol, plusSol [][]int = make([][]int, MAXN), make([][]int, MAXN)
  11. func placeCrosses() {
  12. for i := 0; i < N; i++ {
  13. if row[i] {
  14. continue
  15. }
  16. for j := 0; j < N; j++ {
  17. if col[j] {
  18. continue
  19. }
  20. crossSol[i][j] = 1
  21. col[j] = true
  22. break // Important
  23. }
  24. }
  25. }
  26. func placePluses() (points int) { // It's important to figure out the state while placing pluses.
  27. ds := make([]int, 0)
  28. for i := 0; i < N-1; i++ {
  29. ds = append(ds, i) // Start alternately from both ends
  30. ds = append(ds, 2*N-i-2)
  31. }
  32. ds = append(ds, N-1)
  33. for _, d := range ds {
  34. if diag1[d] {
  35. points++
  36. continue
  37. }
  38. var i, j int
  39. if d < N {
  40. i, j = d, 0
  41. } else {
  42. i, j = N-1, d-N+1
  43. }
  44. for ; 0 <= i && j < N; i, j = i-1, j+1 {
  45. if diag2[i-j+N-1] {
  46. continue
  47. }
  48. plusSol[i][j] = 1
  49. diag2[i-j+N-1] = true
  50. points++
  51. break
  52. }
  53. }
  54. return
  55. }
  56. func main() {
  57. var T int
  58. fmt.Scan(&T)
  59. for tc := 1; tc <= T; tc++ {
  60. var M int
  61. fmt.Scan(&N, &M)
  62. row = make([]bool, MAXN) // Init all state
  63. col = make([]bool, MAXN)
  64. diag1 = make([]bool, 2*MAXN-1)
  65. diag2 = make([]bool, 2*MAXN-1)
  66. for i := 0; i < MAXN; i++ {
  67. crossSol[i] = make([]int, MAXN)
  68. plusSol[i] = make([]int, MAXN)
  69. }
  70. for i := 0; i < M; i++ {
  71. var ch string
  72. var r, c int
  73. fmt.Scan(&ch, &r, &c)
  74. r, c = r-1, c-1
  75. if ch != "+" { // Is not bishop
  76. crossSol[r][c] = -1
  77. row[r], col[c] = true, true
  78. }
  79. if ch != "x" { // Is not rook
  80. plusSol[r][c] = -1
  81. diag1[r+c], diag2[r-c+N-1] = true, true
  82. }
  83. }
  84. placeCrosses()
  85. plusPoints := placePluses()
  86. pieces := 0
  87. for i := 0; i < N; i++ {
  88. for j := 0; j < N; j++ {
  89. if 0 < crossSol[i][j] || 0 < plusSol[i][j] {
  90. pieces++
  91. }
  92. }
  93. }
  94. fmt.Printf("Case #%d: %d %d\n", tc, N+plusPoints, pieces)
  95. for i := 0; i < N; i++ {
  96. for j := 0; j < N; j++ {
  97. if 0 < crossSol[i][j] || 0 < plusSol[i][j] {
  98. var ch byte
  99. if plusSol[i][j] != 0 && crossSol[i][j] != 0 { // != 0 means original sol add new sol
  100. ch = 'o'
  101. } else if plusSol[i][j] != 0 {
  102. ch = '+'
  103. } else {
  104. ch = 'x'
  105. }
  106. fmt.Printf("%c %d %d\n", ch, i+1, j+1)
  107. }
  108. }
  109. }
  110. }
  111. }