강의로 돌아가기
선종연

이게 왜 채점 1,2에서 segmentation fault가 뜨는지 모르겠네요.

'''c++

include

include

include

include

using namespace std;

vector dfs(string cur, map> routes, int sum, int n, vector make) {

if (sum == n) {
    return make;
}

if (routes[cur].empty()) {
    return vector<string>{};
}

vector<string> answer;
for (auto it = routes[cur].begin(); it != routes[cur].end(); ++it) {
    string next = *it;
    routes[cur].erase(it);
    make.push_back(next);
    answer = dfs(next, routes, sum + 1, n, make);
    routes[cur].insert(next);
    make.pop_back();

    if (answer.size() > 0) {
        return answer;
    }
}

}

vector solution(vector> tickets) {
vector answer;
int n = tickets.size();
map> routes;

for (auto x:tickets) {
    string start = x[0];
    string end = x[1];
    routes[start].insert(end);
}

string cur = "ICN";
answer = dfs(cur, routes, 0, n, vector<string>{cur});

return answer;

}

'''

작성중인 코드―solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <string>
#include <vector>
#include <map>
#include <set>

using namespace std;

vector<string> dfs(string cur, map<string, multiset<string>> routes, int sum, int n, vector<string> make) {

    if (sum == n) {
        return make;
    }


    vector<string> answer;
    for (auto it = routes[cur].begin(); it != routes[cur].end(); ++it) {
        string next = *it;
        routes[cur].erase(it);
        make.push_back(next);
        answer = dfs(next, routes, sum + 1, n, make);
        routes[cur].insert(next);
        make.pop_back();

        if (answer.size() > 0) {
            return answer;
        }
    }
}

vector<string> solution(vector<vector<string>> tickets) {
    vector<string> answer;
    int n = tickets.size();
    map<string, multiset<string>> routes;

    for (auto x:tickets) {
        string start = x[0];
        string end = x[1];
        routes[start].insert(end);
    }

    string cur = "ICN";
    answer = dfs(cur, routes, 0, n, vector<string>{cur});

    return answer;
}
0 개의 답변
답변 쓰기
이 입력폼은 마크다운 문법을 지원합니다. 마크다운 가이드 를 참고하세요.