본문 바로가기

이론/문제풀이

[카카오 코드페스티벌 본선 1] 단체사진 찍기 (C++)

토요일에 있을 카카오코드페스티벌을 대비해서 연습겸 풀어본건데,

아 나 떨어지겠는데? ㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋ 


자바로ㅡㄴ 못풀어요 절레절레 ,, 

팔자에도 없는 C++ 오랜만에 켰당 ,,ㅎㅎ


기본적으로는 "순열" 문제이다.

자바로 순열은 구현하려면 재귀를 이용해야하기 때문에 순열 함수가 있다는 C++로 고고씽,,



#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main(){
    vector<string> kf(8);
    int result=0;

    //A, C, F, J, M, N, R, T
    kf[0]="A";kf[1]="C";kf[2]="F";
    kf[3]="J";kf[4]="M";
    kf[5]="N";kf[6]="R";kf[7]="T";

    int total=0;;
    cin>>total;
    string arr="";
    string* command=new string[total];
    for(int i=0;i i++){
            cin >> command[i];
    }
    do{
        arr="";
        for(int i=0;i<8; i++)
            arr+=kf[i];
        bool flag=true;
        for(int i=0;i i++){
            string fm=command[i].substr(0,1);
            string em=command[i].substr(2,1);
            string condition=command[i].substr(3,1);
            int diff=atoi(command[i].substr(4,1).c_str());
           int fmindex=arr.find(fm);
            int emindex=arr.find(em);
            if(condition=="="){
                 if(abs(emindex-fmindex)-1!=diff)
                    flag=false;
           }
            else if(condition==">" ){
                if(abs(emindex-fmindex)-1<=diff)
                    flag=false;
            }
            else if(condition=="<" ){
                if(abs(emindex-fmindex)-1>=diff)
                    flag=false;
            }
        }
        if(flag) result++;
    }while(next_permutation(kf.begin(),kf.end()));
    cout<<result<<endl;
    return 0;
}

next_permutation은 주어진 벡터 또는 배열에서 조합 가능한 모든 순열을 출력해준다.

우리는 편하게 출력된 순열이 주어진 조건과 일치하는지만 확인하면 된다.

(말로는 편하게라고했지만 겁나오래걸림ㅋㅋㅋㅋㅋㅋㅋ)



'이론 > 문제풀이' 카테고리의 다른 글

[Codility] Lesson5- PassingCars  (0) 2018.08.07
[Codility] Lession 4 - FrogRiverOne  (0) 2018.08.02
[Codility] Lesson 3- PermCheck  (0) 2018.08.01
[Codility] Lesson3- TapeEquilibrium  (0) 2018.08.01
[Codility] Lesson3 - PermMissingElem  (0) 2018.07.30