๋ฌธ์
์ฝ๋
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <iostream>
using namespace std;
struct Info{
string city;
int index;
};
unordered_map<string, vector<Info>>graph;
bool flag = true;
vector<string>answer;
int visited[10001];
void dfs(string now, int move, vector<string>memory, int len){
if(move == len&&flag){
flag = false;
answer = memory;
return;
}
for(int i=0; i<graph[now].size(); i++){
if(visited[graph[now][i].index]) continue;
visited[graph[now][i].index] = 1;
memory.push_back(graph[now][i].city);
dfs(graph[now][i].city, move+1, memory, len);
memory.pop_back();
visited[graph[now][i].index] = 0;
}
}
vector<string> solution(vector<vector<string>> tickets) {
sort(tickets.begin(), tickets.end());
for(int i=0; i<tickets.size(); i++){
string a = tickets[i][0];
string b = tickets[i][1];
graph[a].push_back({b,i});
}
dfs("ICN", 0, {"ICN"}, tickets.size());
return answer;
}
ํ์ด
๋ค์๊ณผ ๊ฐ์ ์ ๋ ฅ์ ์กฐ์ฌํด์ผ ํ๋๋ฐ {{"ICN","AAA"},{"ICN","AAA"},{"ICN","AAA"},{"AAA","ICN"},{"AAA","ICN"}} ์ ๊ฐ์ด ์ค๋ณต๋ ํฐ์ผ์ด ์์ ๋ ์ฒ๋ฆฌ๋ฅผ ์ํด์ฃผ์ง ์์ผ๋ฉด ์ฑ์ ๋ฐ์ดํฐ 1๋ฒ์์ ๊ณ์ ํ๋ฆฌ๊ฒ ๋ฉ๋๋ค.
๊ท์ฐฎ์์ ์ํ๋๊ฑธ ํ ๋ฒ์ ์น ํ์ด๋ฒ๋ฆฌ๋ ์ ์์ํ๋ค์
'Algorithm ๐ง๐ปโ๐ป > ํ๋ก๊ทธ๋๋จธ์ค(Programmers)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[SQL] ํ๋ก๊ทธ๋๋จธ์ค - SQL ๊ณ ๋์ Kit (0) | 2022.05.28 |
---|---|
[c++] ํ๋ก๊ทธ๋๋จธ์ค - ๊ฐ์ฅ ๋จผ ๋ ธ๋(Level 3) (0) | 2022.05.05 |
[c++] ํ๋ก๊ทธ๋๋จธ์ค - [3์ฐจ] ํ์ผ๋ช ์ ๋ ฌ(Level 2) (0) | 2022.05.04 |
[c++] ํ๋ก๊ทธ๋๋จธ์ค - ์ ํ์ ์๊ฐ ์ด๋(Level 2) (0) | 2022.05.01 |
[c++] ํ๋ก๊ทธ๋๋จธ์ค - ์ด์ง ๋ณํ ๋ฐ๋ณตํ๊ธฐ(Level 2) (0) | 2022.04.26 |
[c++] ํ๋ก๊ทธ๋๋จธ์ค - ๊ตฌ๋ช ๋ณดํธ(Level 2) (0) | 2022.04.25 |
๋๊ธ