개발자 톡

연습문제 톡 [HSAT 4회 정기 코딩 인증평가 기출] 통근버스 출발 순서 검증하기

통근버스 출발순서 아이디어

등록일
2022-10-17 20:50:18
조회수
690
작성자
bungae104

모든 버스의 경우의 수에서 순열, 조합으로 뽑아서 문제를 풀었습니다.


//통근 버스 출발 순서 결정하기
/*
3
3 1 2
출력예제1
0
*/
#include 
#include 
#include 
using namespace std;

//globals
vector graph;
bool visited[5001];
int N;
int results;

//methods
void Input(){
    
    cin>>N;
    
    for(int i=0; i>tmp;
        graph.push_back(tmp);
    }
    //
}

bool check_bus(vector& tmp_graph){
    if(tmp_graph[0] tmp_graph[2]) return false;
    return true;
}

void prt_combi() {
    vector tmp_graph;
    for (int i = 0; i < N; i++) {
        if (visited[i]) {
           tmp_graph.push_back(graph[i]);
        }
    }
    if(!check_bus(tmp_graph)) results++;
}

void dfs_combi(int idx, int cnt) {
    if (cnt == 3) {
        prt_combi();
        return;
    }
    for (int i = idx; i < N; i++) {
        if (visited[i] == true) continue;
        visited[i] = true;
        dfs_combi(i, cnt + 1);
        visited[i] = false;
    }
}

int Combination(){

    int cnt=0;
    vector visited(N,false);
    
    for(int i=0; i<3; i++){
        visited[i]=true;
    }
    sort(visited.begin(), visited.end());
    do{
        vector tmp_graph;
        for(int i=0; i



c++의 next_permutation라이브러리를 사용하여 진행했지만, n이 3에서 5000이하라 시간초과가 발생했습니다.


따라서, DFS를 이용하여 조합을 구현하여 진행했는데도 시간초과가 발생했습니다.


혹시 더 좋은 아이디어 있을까요?


#[hsat_4회_정기_코딩_인증평가_기출]_통근버스_출발_순서_검증하기
#c++

이 카테고리의 톡 더보기