본문 바로가기

problem solving

[프로그래머스]단체사진 찍기

우선 모든 가능한 경우의 수를 구하기 위해 DFS를 사용했고 store 배열에 모든 경우의 수를 담았다.

그리고 나서 두 개의 큐(fQ, sQ)를 선언해 data 배열의 조건을 하나씩 확인하면서 빈 큐에 푸쉬했다.

 반복하는 data의 인덱스마다 저장되어있는 큐에서 빈큐로 (fQ에서 sQ로 또는  sQ에서 fQ로)데이터를 필터링해 이동시킨다.

모든 data의 조건을 검사한 후 남아있는 경우의 수를 answer에 넣어준다.

#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <cmath>
 
using namespace std;
 
vector<string> store;
string kakaoCharacter;
bool check[8];
 
void DFS(string str) {
    if(str.length() == 8) {
        store.push_back(str);
        return;
    }
    
    for(int idx = 0; idx < 8; idx++) {
        if(check[idx]) { continue; }
        check[idx] = true;
        DFS(str + kakaoCharacter[idx]);
        check[idx] = false;
    }
}
 
 
// 전역 변수를 정의할 경우 함수 내에 초기화 코드를 꼭 작성해주세요.
int solution(int n, vector<string> data) {
    int answer = 0;
    
    kakaoCharacter = "ACFJMNRT";
    fill(check, check + 8false);
    
    store.clear();
    
    DFS("");
    
    queue<string> fQ;
    for(int idx = 0; idx < store.size(); idx++) {
        fQ.push(store[idx]);
    }
    queue<string> sQ;
    int pos1, pos2;
    for (int idx = 0; idx < n; idx++) {
        char f1 = data[idx][0];
        char f2 = data[idx][2];
        char comp = data[idx][3];
        int arr = data[idx][4- '0';
        
        if (idx % 2 == 0) {
            while (!fQ.empty()) {
                pos1 = pos2 = 100;
                string str = fQ.front();
                fQ.pop();
                
                for(int i = 0; i < 8; i++) {
                    if(str[i] == f1 || str[i] == f2) {
                        pos1 = (pos1 == 100 ? i : pos1);
                        pos2 = (pos1 != i ? i : pos2);
                    }
                }
                if(comp == '=' && abs(pos2 - pos1) - 1 == arr) {
                    sQ.push(str);
                }
                else if(comp == '<' && abs(pos2 - pos1) - 1 < arr) {
                    sQ.push(str);
                }
                else if(comp == '>' && abs(pos2 - pos1) - 1 > arr) {
                    sQ.push(str);
                }
            }   
        } else {
            while (!sQ.empty()) {
                pos1 = pos2 = 100;
                string str = sQ.front();
                sQ.pop();
                
                for(int i = 0; i < 8; i++) {
                    if(str[i] == f1 || str[i] == f2) {
                        pos1 = (pos1 == 100 ? i : pos1);
                        pos2 = (pos1 != i ? i : pos2);
                    }
                }
                if(comp == '=' && abs(pos2 - pos1) - 1 == arr) {
                    fQ.push(str);
                }
                else if(comp == '<' && abs(pos2 - pos1) - 1 < arr) {
                    fQ.push(str);
                }
                else if(comp == '>' && abs(pos2 - pos1) - 1 > arr) {
                    fQ.push(str);
                }
            }   
        }
    }
    
    answer = (fQ.size() > sQ.size() ? fQ.size() : sQ.size());
    
    return answer;
}
cs

'problem solving' 카테고리의 다른 글

[프로그래머스]방금그곡  (0) 2020.07.11
[ 프로그래머스]다음 큰 숫자  (0) 2020.03.28
[SWEA]2814. 최장 경로  (0) 2020.03.15
[BOJ]1874번: 스택 수열  (0) 2020.03.14
[LeetCode]#2 Add Two Numbers  (0) 2020.03.14