문제
문제의 저작권은 SW Expert Academy에 있습니다
입력
4
1 3 5 7 9 11 13 15 17
18 16 14 12 10 8 6 4 2
13 17 9 5 18 7 11 1 15
1 6 7 9 12 13 15 17 18
출력
#1 112097 250783
#2 250783 112097
#3 336560 26320
#4 346656 16224
접근 방식
코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Solution_6808_정현명 {
static int GueYeung[];
static int InYeung[];
static int winCnt, loseCnt;
static boolean isSelected[];
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int T = Integer.parseInt(br.readLine());
for(int tc=1;tc<=T;tc++) {
StringTokenizer st = new StringTokenizer(br.readLine());
GueYeung = new int[9];
InYeung = new int[9];
winCnt = 0;
loseCnt = 0;
isSelected = new boolean[19];
for(int i=0;i<9;i++) {
GueYeung[i] = Integer.parseInt(st.nextToken());
}
int InYeungIdx = 0;
for(int i=1;i<=18;i++) {
if(isSelected[i] == false) {
InYeung[InYeungIdx] = i;
}
}
permutation(0,0,0,0);
sb.append("#"+tc+" "+winCnt+" "+loseCnt+"\n");
}
System.out.println(sb);
}
public static void permutation(int cnt, int flag, int scoreI, int scoreG) {
if(cnt == 9) {
if(scoreI > scoreG) loseCnt++;
else if (scoreI < scoreG) winCnt++;
return;
}
for(int i=0;i<9;i++) {
if((flag & 1<<i) != 0) continue;
int sum = InYeung[i] + GueYeung[cnt];
if(InYeung[i] > GueYeung[cnt]) {
permutation(cnt+1,flag | 1<<i, scoreI+sum, scoreG);
}
else if(InYeung[i] < GueYeung[cnt]) {
permutation(cnt+1,flag | 1<<i, scoreI, scoreG+sum);
}
else {
permutation(cnt+1,flag | 1<<i, scoreI, scoreG);
}
}
}
}