1.

#include <string>
#include <vector>
#include <math.h>
#include <string.h>
#include <iostream>
using namespace std;
 
vector<char> people;
int visited[8];
int answer;
 
void permutation(string result, vector<string> data, int cnt)
{
    if(cnt == 8)
    {        
        for(int i = 0; i < data.size(); i++)
        {
            int p1Idx, p2Idx;
            
            // result에서, 조건에 있는 두사람의 인덱스를 찾음
            for(int j = 0; j < 8; j++)
            {
                if(result[j] == data[i][0])
                {
                    p1Idx = j;
                }
                else if(result[j] == data[i][2])
                {
                    p2Idx = j;
                }
            }
            
            if(data[i][3== '>')
            {
                if(!(abs(p1Idx-p2Idx)-1 > data[i][4]-'0')) 
                {
                    return;
                }
            }
            else if(data[i][3== '=')
            {
                if(!(abs(p1Idx-p2Idx)-1 == data[i][4]-'0')) 
                {
                    return;
                }
            }
            else if(data[i][3== '<')
            {
                if(!(abs(p1Idx-p2Idx)-1 < data[i][4]-'0'))   
                {
                    return;
                }
            }
        }
        
        answer++;
        
        return;
    }
    
    for(int i = 0; i < 8; i++)
    {
        if(visited[i] == 0)
        {        
            visited[i] = 1;
            permutation(result + people[i], data, cnt+1);
            visited[i] = 0;
        } 
    }
}
 
int solution(int n, vector<string> data) 
{
    // 전역변수 초기화
    people.push_back('A');
    people.push_back('C');
    people.push_back('F');
    people.push_back('J');
    people.push_back('M');
    people.push_back('N');
    people.push_back('R');
    people.push_back('T');
    memset(visited, 0sizeof(visited));
    answer = 0;
    
    permutation("", data, 0);
 
    return answer;
}
cs

 

2.

#include <string>
#include <vector>
#include <math.h>
#include <string.h>
#include <iostream>
using namespace std;
 
typedef struct node
{
    char p1;
    char p2;
    char command;
    int diff;
}node;
 
vector<char> people;
int visited[8];
int answer;
 
void permutation(string result, vector<node> condition, int cnt)
{
    if(cnt == 8)
    {        
        for(int i = 0; i < condition.size(); i++)
        {
            int p1Idx, p2Idx;
            
            // result에서, 조건에 있는 두사람의 인덱스를 찾음
            for(int j = 0; j < result.size(); j++)
            {
                if(result[j] == condition[i].p1)
                {
                    p1Idx = j;
                }
                else if(result[j] == condition[i].p2)
                {
                    p2Idx = j;
                }
            }
            
            if(condition[i].command == '>')
            {
                if(!(abs(p1Idx-p2Idx)-1 > condition[i].diff))   
                {
                    return;
                }
            }
            else if(condition[i].command == '=')
            {
                if(!(abs(p1Idx-p2Idx)-1 == condition[i].diff))
                {
                    return;
                }
            }
            else if(condition[i].command == '<')
            {
                if(!(abs(p1Idx-p2Idx)-1 < condition[i].diff))   
                {
                    return;
                }
            }
        }
        
        answer++;
        
        return;
    }
    
    for(int i = 0; i < 8; i++)
    {
        if(visited[i] == 0)
        {    
            string temp;
            temp += people[i];
            
            visited[i] = 1;
            permutation(result + temp, condition, cnt+1);
            visited[i] = 0;
        } 
    }
}
 
// 전역 변수를 정의할 경우 함수 내에 초기화 코드를 꼭 작성해주세요.
int solution(int n, vector<string> data) 
{
    // 구조체를 사용할 경우 -> node 포함한 선언은 solution 안에 한 후 초기화해야함
    vector<node> condition;
    
    // 전역변수 초기화
    people.push_back('A');
    people.push_back('C');
    people.push_back('F');
    people.push_back('J');
    people.push_back('M');
    people.push_back('N');
    people.push_back('R');
    people.push_back('T');
    memset(visited, 0sizeof(visited));
    answer = 0;
    
    for(int i = 0; i < data.size(); i++)
    {
        condition.push_back({data[i][0], data[i][2], data[i][3], data[i][4]-'0'});   
    }
    
    permutation("", condition, 0);
 
    return answer;
}
cs

+ Recent posts