코딩스터디

6808. 규영이와 인영이의 카드게임

애플앤마블 2019. 5. 13. 00:10
반응형
SMALL

Card by Andrejs Kirma from the Noun Project

이 문제는 swexpertacademy D3 P.6808임을 알립니다. 하단 URL참조

 

인영이와 규영이의 카드를 입력받고 recursion을 이용해 순열을 비교하면 된다. 이 때 순열의 조건으로 경우를 줄이지 않으면 runtime이 초과될 수 있으니 조심한다.

#include<iostream>
using namespace std;
int iy[9];
int ky[9];
bool checkArr[9];
int win;

void permutation(int idx, int ky_score, int iy_score){
	if(idx==9){
        if(ky_score>iy_score) {
        	win++;
            return;
        }
        return;
    }
    for(int i=0; i<9; i++){
        if(checkArr[i]==false){
        	checkArr[i]=true;
            if(ky[idx]>iy[i]) permutation(idx+1, ky_score+(ky[idx]+iy[i]), iy_score);
            else permutation(idx+1, ky_score, iy_score+(ky[idx]+iy[i]));
            checkArr[i]=false;
        }
    }
}

int main(){
	int T;
    cin>>T;
    for(int t=1; t<=T; t++){
    	cout<<'#'<<t<<' ';
        win=0;
        int card[19]={0};
        for(int i=0; i<9; i++){
        	cin>>ky[i];
            card[ky[i]]=1;
        }
        int k=0;
        for(int i=1; i<=18; i++){
        	if(card[i]==0) {iy[k++]=i;} 
        }
        permutation(0,0,0);
        cout<<win<<' '<<362880-win<<'\n';
    }
    return 0;
}

 

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWgv9va6HnkDFAW0&categoryId=AWgv9va6HnkDFAW0&categoryType=CODE

반응형
LIST