[Java] SWEA / 규영이와 인영이의 카드게임 / 6808번

정현명·2022년 2월 14일
0

SW Expert Academy

목록 보기
8/16
post-thumbnail

[Java] SWEA / 규영이와 인영이의 카드게임 / 6808번

문제

규영이와 인영이의 카드게임 문제 링크

문제의 저작권은 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


접근 방식

  1. 규영이의 카드 번호를 입력받고 나머지를 인영이의 카드 번호로 저장한다
  2. 인영이의 9개 카드로 나올 수 있는 순열을 구현한다
  3. 규영이와 인영이의 카드로 배틀하여 규영이의 승리와 패배 수를 카운팅한다
  • 순열을 구현할 때 배열 순서를 저장하지 않고 각자의 점수 합을 계속 더해 가기


코드

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);
			}
			
		}
	}

}
profile
꾸준함, 책임감

0개의 댓글