ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฉ๐ป๐ป ๋ฌธ์
โ๐ป ํ์ด
๐จ Go
// https://www.acmicpc.net/problem/3085
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
var n int
fmt.Fscanln(reader, &n)
var bonbons [][]string
for i := 0; i < n; i++ {
input, _ := reader.ReadString('\n')
inputs := strings.Split(strings.ReplaceAll(input, "\n", ""), "")
bonbons = append(bonbons, inputs)
}
var maxCount int
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
if j != n-1 {
bonbons = swap(i, j, i, j+1, bonbons)
maxCount = checkMax(maxCount, bonbons)
bonbons = swap(i, j, i, j+1, bonbons)
}
if i != n-1 {
bonbons = swap(i, j, i+1, j, bonbons)
maxCount = checkMax(maxCount, bonbons)
bonbons = swap(i, j, i+1, j, bonbons)
}
}
}
fmt.Fprintln(writer, maxCount)
}
func swap(i, j, k, l int, bonbons [][]string) [][]string {
bonbons[i][j], bonbons[k][l] = bonbons[k][l], bonbons[i][j]
return bonbons
}
func checkMax(maxCount int, bonbons [][]string) int {
max := 0
prev := ""
for i := 0; i < len(bonbons); i++ {
count := 0
for j := 0; j < len(bonbons); j++ {
cur := bonbons[i][j]
if prev == cur {
count++
if max < count {
max = count
}
} else {
count = 1
}
prev = cur
}
}
prev = ""
for i := 0; i < len(bonbons); i++ {
count := 0
for j := 0; j < len(bonbons); j++ {
cur := bonbons[j][i]
if prev == cur {
count++
if max < count {
max = count
}
} else {
count = 1
}
prev = cur
}
}
if max < maxCount {
max = maxCount
}
return max
}
๐จ Python3
# https://www.acmicpc.net/problem/3085
import sys
def swap(i, j, k, l, bonbons):
bonbons[i][j], bonbons[k][l] = bonbons[k][l], bonbons[i][j]
def check_max(max_count, bonbons):
prev = ""
for i in range(len(bonbons)):
count = 0
for j in range(len(bonbons)):
cur = bonbons[i][j]
if prev == cur:
count += 1
max_count = max(count, max_count)
else:
count = 1
prev = cur
prev = ""
for i in range(len(bonbons)):
count = 0
for j in range(len(bonbons)):
cur = bonbons[j][i]
if prev == cur:
count += 1
max_count = max(count, max_count)
else:
count = 1
prev = cur
return max_count
if __name__ == "__main__":
n = int(sys.stdin.readline())
bonbons = []
for i in range(n):
inputs = list(sys.stdin.readline().rstrip())
bonbons.append(inputs)
max_count = 0
for i in range(n):
for j in range(n):
if j != n-1:
swap(i, j, i, j+1, bonbons)
max_count = check_max(max_count, bonbons)
swap(i, j, i, j+1, bonbons)
if i != n-1:
swap(i, j, i+1, j, bonbons)
max_count = check_max(max_count, bonbons)
swap(i, j, i+1, j, bonbons)
print(max_count)
728x90
'dev > algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ / 6187๋ฒ / Going to the Movies [Go][Python3] (0) | 2021.02.22 |
---|---|
BOJ / 3048๋ฒ / ๊ฐ๋ฏธ [Go][Python3] (0) | 2021.02.21 |
BOJ / 1049๋ฒ / ๊ธฐํ์ค [Go][Python3] (0) | 2021.02.19 |
BOJ / 15815๋ฒ / ์ฒ์ฌ ์ํ์ ์ฑํ [Go][Python3] (0) | 2021.02.18 |
BOJ / 1802๋ฒ / ์ข ์ด ์ ๊ธฐ [Go][Python3] (0) | 2021.02.17 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
TAG
- go
- ํ
- Macbook pro 2012 mid 13
- ๋ถํ ์ ๋ณต
- ์๊ฐ๊ต์ฒด
- Algorithm
- dfs
- MongoDB
- ๋ธ๋ฃจํธํฌ์ค
- ๋ฐฑ์ค
- java
- ๋ชฝ๊ณ ๋๋น
- ๋งฅ๋ถ ์ ๊ทธ๋ ์ด๋
- ์๋ฐ
- ๋งฅ๋ถ
- BFS
- ballet
- python3
- Golang
- AWS
- ํด์๋งต
- ์ด๋ถํ์
- ์คํ
- ํ๋ก์ด๋์์ฌ
- baekjoon
- dp
- ๋ฐ๋
- BOJ
- ๋งฅ๋ถํ๋ก
- ์๊ณ ๋ฆฌ์ฆ
- Total
- Today
- Yesterday