1285번: 동전 뒤집기
첫째 줄에 20이하의 자연수 N이 주어진다. 둘째 줄부터 N줄에 걸쳐 N개씩 동전들의 초기 상태가 주어진다. 각 줄에는 한 행에 놓인 N개의 동전의 상태가 왼쪽부터 차례대로 주어지는데, 앞면이 위
www.acmicpc.net
- 풀이
열만 backtracking하면서 메소드를 돌려주고 메소드 안에서 행도 돌려주면서 T의 최소 개수를 찾아준다.
- 코드
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.List;
import java.util.PriorityQueue;
public class Main {
static int N, answer;
static char[][] array;
public static void main(String[] args) throws Exception {
SetData();
System.out.println(answer);
}
// 데이터
private static void SetData() throws Exception {
InputReader in = new InputReader(System.in);
N = in.nextInt();
answer = Integer.MAX_VALUE;
array = new char[N][N];
for(int i = 0; i < N ; i ++){
array[i] = in.nextLine().toCharArray();
}
solve(0);
}
public static void solve(int row){
if(row == N){
int count = 0;
for(char[] i : array){
int temp = 0;
for(char j : i){
if(j == 'T'){
temp ++;
}
}
count = count + Math.min(temp,N-temp);
}
answer = Math.min(count,answer);
return;
}
solve(row+1);
for(int i = 0 ; i < N; i ++){
array[i][row] = array[i][row] == 'H'?'T':'H';
}
solve(row+1);
}
}
class Delivery implements Comparable<Delivery> {
int from;
int to;
int weight;
public Delivery(int from, int to, int weight) {
this.from = from;
this.to = to;
this.weight = weight;
}
@Override
public int compareTo(Delivery o) {
if (this.to < o.to) {
return -1;
} else if (this.to == o.to) {
if (this.from < o.from) {
return -1;
}
}
return 1;
}
}
class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
public InputReader(InputStream st) {
this.stream = st;
}
public int read() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
'algorithm' 카테고리의 다른 글
[BOJ/JAVA] 백준 3190번 : 뱀 (0) | 2021.04.15 |
---|---|
[BOJ/JAVA] 백준 12100번 : 2048(Easy) (0) | 2021.04.15 |
[BOJ/JAVA] 백준 8980번 : 택배 (0) | 2021.04.03 |
[BOJ/JAVA] 백준 20183번 : 골목 대장 호석 (0) | 2021.04.02 |
[BOJ/JAVA] 백준 20164번 : 홀수 홀릭 호석 (0) | 2021.04.01 |