본문 바로가기
프로그래머스

여행경로 C++

by paysmile 2019. 9. 29.
#include <string>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>

using namespace std;
const int MAX = 100000001;
vector<string> temp;
int visited[MAX];

bool dfs(string str,int count, vector<vector<string>> t) {
	if (count == t.size())
		return true;

	for (int i = 0; i < t.size(); i++) {
		if (t[i][0] == str && visited[i] == -1) {
			temp.push_back(t[i][1]);
			visited[i] = 1;
			if (dfs(t[i][1], count + 1, t))
				return true;
			visited[i] = -1;
			temp.pop_back();
		}
	}
	return false;
}

vector<string> solution(vector<vector<string>> tickets) {
	vector<string> answer;

	sort(tickets.begin(), tickets.end());
	memset(visited, -1, sizeof(visited));

	for (int i = 0; i < tickets.size(); i++) {
		if (tickets[i][0] == "ICN") {
			visited[i] = 1;
			temp.clear();
			temp.push_back(tickets[i][0]);
			temp.push_back(tickets[i][1]);
			if (dfs(tickets[i][1],1,tickets)) {
				answer = temp;
				return answer;
			}
			visited[i] = -1;
		}
	}
	return answer;
}

'프로그래머스' 카테고리의 다른 글

저울 C++  (0) 2019.09.30
이중우선순위큐 C++  (0) 2019.09.29
입국심사 C++  (0) 2019.09.28
디스크 컨트롤러  (0) 2019.09.28
섬 연결하기  (0) 2019.09.25