ν°μ€ν 리 λ·°
728x90
λ°μν
λ¬Έμ
λ¬Έμ νμ΄
μΈμ ν μ¬νμ swap νκ³ , κ°λ‘ μΈλ‘μμ μ°μλ μ¬νμ κ°μλ₯Ό 체ν¬νλ©΄ ν΄λΉ λ¬Έμ λ₯Ό ν μ μλ€. λ€μκ³Ό κ°μ κ²½μ°μ μλ₯Ό νλ¨νμ¬ λ¬Έμ λ₯Ό μ κ·Όνλ©΄ μ½κ² ν΄κ²°ν μ μλ€. λν swap λλ κ²½μ° λͺ¨λ λ²μλ₯Ό νμνλ κ²μ΄ μλ, swap λλ μμλ§ νμνλ€λ©΄ μλλ₯Ό λμΌ μ μμ κ² κ°λ€.
- λκ°μ μ μΈμ νκ²μΌλ‘ νλ¨νμ§ μλλ€.
- μ¦, κ°λ‘ μΈλ‘μ λν swapλ§ μ²λ¦¬νκ³ λΉκ΅νλ©΄ λλ€.
- Nμ ν¬κΈ°κ° 50μ΄λ―λ‘ λΈλ£¨νΈ ν¬μ€λ‘ μΆ©λΆν νμ κ°λ₯ν ν¬κΈ°μ΄λ€.
- μΈμ ν μ¬νμ λ°°μ΄μ μμ°¨μ μΌλ‘ swap νκ³ νμνλ κ²μ λ°λ³΅νμ¬ λ΅μ μ°Ύμ μ μλ€.
μ½λ
Python μ½λ
from sys import stdin
def check_length():
h_max, v_max = 1, 1
for i in range(n):
horizontal, vertical = 1, 1
for j in range(n - 1):
# κ°λ‘ νμΈ
if candy[i][j] == candy[i][j + 1]:
horizontal += 1
else:
h_max = \
horizontal if h_max < horizontal else h_max
horizontal = 1
# μΈλ‘ νμΈ
if candy[j][i] == candy[j + 1][i]:
vertical += 1
else:
v_max = \
vertical if v_max < vertical else v_max
vertical = 1
h_max =\
horizontal if h_max < horizontal else h_max
v_max = \
vertical if v_max < vertical else v_max
return h_max if h_max > v_max else v_max
if __name__ == "__main__":
n = int(stdin.readline())
candy = [list(stdin.readline().rstrip()) for _ in range(n)]
answer = 0
for i in range(n):
for j in range(n - 1):
# κ°λ‘ swap
candy[i][j], candy[i][j + 1] = candy[i][j + 1], candy[i][j]
answer = max(answer, check_length())
ret = check_length()
answer = ret if answer < ret else answer
candy[i][j], candy[i][j + 1] = candy[i][j + 1], candy[i][j]
# μΈλ‘ swap
candy[j][i], candy[j + 1][i] = candy[j + 1][i], candy[j][i]
ret = check_length()
answer = ret if answer < ret else answer
candy[j][i], candy[j + 1][i] = candy[j + 1][i], candy[j][i]
print(answer)
Java μ½λ
import java.util.Scanner;
public class Main {
static int check_length(char[][] candy) {
int n = candy.length;
int h_max = 1;
int v_max = 1;
int ret = 0;
for (int i = 0; i < n; i++) {
int horizontal = 1;
int vertical = 1;
for (int j = 0; j < n - 1; j++) {
if (candy[i][j] == candy[i][j + 1]) {
horizontal += 1;
} else {
if (h_max < horizontal) {
h_max = horizontal;
}
horizontal = 1;
}
}
for (int j = 0; j < n - 1; j++) {
if (candy[j][i] == candy[j + 1][i]) {
vertical += 1;
} else {
if (v_max < vertical) {
v_max = vertical;
}
vertical = 1;
}
}
if (h_max < horizontal) {
h_max = horizontal;
}
if (v_max < vertical) {
v_max = vertical;
}
}
ret = v_max;
if (h_max > v_max)
ret = h_max;
return ret;
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
char[][] candy = new char[n][n];
int answer = 0;
for (int i = 0; i < n; i++) {
candy[i] = s.next().toCharArray();
}
char temp;
int ret;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1; j++) {
temp = candy[i][j];
candy[i][j] = candy[i][j + 1];
candy[i][j + 1] = temp;
ret = check_length(candy);
if (answer < ret) {
answer = ret;
}
temp = candy[i][j];
candy[i][j] = candy[i][j + 1];
candy[i][j + 1] = temp;
temp = candy[j][i];
candy[j][i] = candy[j + 1][i];
candy[j + 1][i] = temp;
ret = check_length(candy);
if (answer < ret) {
answer = ret;
}
temp = candy[j][i];
candy[j][i] = candy[j + 1][i];
candy[j + 1][i] = temp;
}
}
System.out.println(answer);
}
}
μμ κ°μ λ‘μ§μ λ€λ₯Έ μΈμ΄λ₯Ό μ¬μ©νλ€λ©΄ μκ° λ΄μ ν΅κ³Όν μ μμ§λ§, νμ΄μ¬μ ν΅κ³Όν μ μμΌλ©° PyPy3λ₯Ό μ¬μ©νμ¬μΌ νλ€.
728x90
λ°μν
'π¨βπ» μ½λ©ν μ€νΈ > λ°±μ€' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
λ°±μ€: 15650 Nκ³Ό M (2) (0) | 2020.07.14 |
---|---|
λ°±μ€: 15649 Nκ³Ό M (1) (0) | 2020.07.14 |
λ°±μ€: 6064 μΉ΄μ λ¬λ ₯ (2) | 2020.07.12 |
λ°±μ€: 1476 λ μ§ κ³μ° (0) | 2020.07.11 |
λ°±μ€: 1748 μ μ΄μ΄ μ°κΈ° 1 (0) | 2020.07.10 |
λκΈ
κΈ λ³΄κ΄ν¨
μ΅κ·Όμ μ¬λΌμ¨ κΈ
μ΅κ·Όμ λ¬λ¦° λκΈ