본문 바로가기
알고리즘/프로그래머스

[ 프로그래머스 / 구현 ] 단체사진 찍기 (KaKao)

by 뎁꼼 2020. 7. 2.

1. 문제


 

 

코딩테스트 연습 - 단체사진 찍기

단체사진 찍기 가을을 맞아 카카오프렌즈는 단체로 소풍을 떠났다. 즐거운 시간을 보내고 마지막에 단체사진을 찍기 위해 카메라 앞에 일렬로 나란히 섰다. 그런데 각자가 원하는 배치가 모두 �

programmers.co.kr

 

 

2. 소스코드


- 단순 구현문제.

- string base = "ACFJMNRT" 와 같이 string을 선언하고 next_permutation을 이용해서 모든 경우의 수를 탐색한다.

- 경우의 수마다, 문자열이 조건을 만족하는지 확인해 주면 된다.

 

소스코드

#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
 
bool isAble(string str, vector<string> &data){
    int size = data.size();
    for(int i = 0 ; i < size; ++i){
        string cmd = data[i];
        char from = cmd[0];
        char to = cmd[2];
        char oper = cmd[3];
        int oper_num = (cmd[4] - '0') + 1;
        int from_idx = str.find(from);
        int to_idx = str.find(to);

        if(oper == '=' and abs(from_idx - to_idx) != oper_num) 
            return false; 
        else if(oper == '>' and abs(from_idx - to_idx) <= oper_num) 
            return false;
        else if(oper == '<' and abs(from_idx - to_idx) >= oper_num)
            return false;
                
    }
    return true;
}

int solution(int n, vector<string> data) {
    int answer = 0;
    string base = "ACFJMNRT";
    do{
        if(isAble(base, data)) answer++;
    }
    while(next_permutation(base.begin(), base.end()));
    
    return answer;
}