[Programmers] 여행 경로

Time Lapse :58min 57sec

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <string>
#include <vector>
using namespace std;
//58:57
class INFO{
public:
string name;
int from;
int to;
INFO(string s, int f, int t) : from(f), to(t), name(s) {}
};
vector<INFO> info;
vector<vector<string>> TICKET;
vector<vector<string>> answer;
bool mysort(vector<string> v1, vector<string> v2){
if(v1[0]<v2[0])
return true;
else if(v1[0]==v2[0])
return v1[1]<v2[1];
else
return false;
}

void DFS(string from, int visit,int total, vector<bool> visited, vector<string> ans){
ans.push_back(from);
if(visit==total){
answer.push_back(ans);
return ;
}
int info_idx;
for(int i=0;i<info.size();i++){
if(info[i].name==from){
info_idx = i;
break;
}
}

for(int to=info[info_idx].from;to<=info[info_idx].to;to++){
if(!visited[to]){
visited[to]=true;
DFS(TICKET[to][1],visit+1,total,visited,ans);
visited[to]=false;
}
}

return ;
}

vector<string> solution(vector<vector<string>> tickets) {
sort(tickets.begin(),tickets.end(),mysort);
TICKET = tickets;
string name = tickets[0][0];
int from = 0;
int to = -1;
for(int i=0;i<tickets.size();i++) {
if (name != tickets[i][0]) {
INFO infomation(name,from,to);
info.push_back(infomation);
name = tickets[i][0];
from = i;
to = i-1;
}
++to;
}
INFO infomation(name,from,to);
info.push_back(infomation);
vector<bool> used(tickets.size(),false);
vector<string> ans;
DFS("ICN",0,tickets.size(),used,ans);
sort(answer.begin(),answer.end());
return answer[0];
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/Programmers/travelRoute/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.