π λ¬Έμ
π μμ΄λμ΄
μ²μμλ ν΄μ맡과 dfs λ°©λ²μ μ΄μ©νμ¬ κ° λμμ μ°κ²°λ λ€μ μ½μ€λ₯Ό λ£κ³ dfsλ‘ νμνλ©΄μ κ° κ²½λ‘μμ κ° μ μλ λ€μλμλ₯Ό λͺ¨λ λ°©λ¬Ένλ λ°©μμΌλ‘ μ§ννμμ΅λλ€. μ΄νμ depth κ° ν°μΌμ μλ§νΌ λλ€λ©΄ λͺ¨λ ν°μΌμ μ¬μ©ν κ²μ΄λ―λ‘ κ°λ₯ κ²½λ‘μ¬μ 리ν΄νλλ‘ νμμ΅λλ€.
π νμ΄
import java.util.*;
class FindPath43164 {
public static final String ICN = "ICN";
HashMap<String, Integer> hash = new HashMap<>();
List<List<String>> graph = new LinkedList<>();
List<String> ans = new ArrayList<>();
boolean flag = false;
public String[] solution(String[][] tickets) {
graph.add(new ArrayList<>());
for (String[] ticket : tickets) {
for (int j = 0; j < 2; j++) {
if (!hash.containsKey(ticket[j])) {
hash.put(ticket[j], hash.size());
graph.add(new ArrayList<>());
}
}
int start = hash.get(ticket[0]);
graph.get(start).add(ticket[1]);
}
ans.add(ICN); //μμ
dfs(hash.get(ICN),1,tickets.length+1);
return ans.toArray(String[]::new);
}
public void dfs(int now, int depth, int goalDepth) {
if (depth == goalDepth) {
flag = true;
return;
}
if (checkVisited()) return;
if (graph.get(now).size() == 0) return;
Collections.sort(graph.get(now));
int size = graph.get(now).size();
for (int i = 0; i < size; i++) {
String nextCity = graph.get(now).remove(i);
ans.add(nextCity);
dfs(hash.get(nextCity),depth+1,goalDepth);
if (flag) return;
ans.remove(ans.lastIndexOf(nextCity));
graph.get(now).add(i, nextCity);
}
}
private boolean checkVisited() {
for (List<String> strings : graph) {
if (strings.size() != 0) return false;
}
return true;
}
public static void main(String[] args) {
FindPath43164 solution = new FindPath43164();
// System.out.println(Arrays.toString(solution.solution(new String[][]{{"ICN", "JFK"}, {"HND", "IAD"}, {"JFK", "HND"}})));
// System.out.println(Arrays.toString(solution.solution(new String[][]{{"ICN", "SFO"}, {"ICN", "ATL"}, {"SFO", "ATL"}, {"ATL", "ICN"}, {"ATL", "SFO"}})));
System.out.println(Arrays.toString(solution.solution(new String[][]{{"ICN", "BOO"}, {"ICN", "COO"}, {"COO", "DOO"}, {"DOO", "COO"}, {"BOO", "DOO"}, {"DOO", "BOO"}, {"BOO", "ICN"}, {"COO", "BOO"}}
)));
}
}
π μμ΄λμ΄
νμ§λ§ λ κ°λ¨ν νμ΄λ°©λ²μ μμκΉ κ³ λ―Όνλ€κ° κ·Έλ₯ κ° ν°μΌμ λν΄μ νμΈνλ λ°©μμΌλ‘ μ§ννμμ΅λλ€
μ²μ μμμ μΈμ²μμ μμνμ¬ μΈμ²μμ μΆλ°νκ³ μμ§ μ¬μ©νμ§ μμν°κ²μ arrayμ λ£κ³ μνλ²³ μ μ λ ¬ν©λλ€.
μ΄νμ κ°λ₯ν κ²½λ‘λ₯Ό μμμ λΆν° ansλ°°μ΄μ λ£κ³ , visitμ²λ¦¬ νμ λ€μκ²½λ‘μ λν νμμ μ§νν©λλ€(dfs)
κ·Έλ κ² ν°μΌμ κ°μμ dfsμ νμ κΉμ΄κ° κ°μΌλ©΄ λͺ¨λ νμνκ²μ΄λ―λ‘ νμμ μ’ λ£νκ³ λ¦¬ν΄νλλ‘ νμμ΅λλ€.
π νμ΄
import java.util.*;
import java.io.*;
class Solution {
public static final String ICN = "ICN";
private String[][] tickets;
private boolean[] visited;
private String[] ans ;
private boolean flag = false;
public String[] solution(String[][] tickets) {
this.tickets = tickets;
visited = new boolean[tickets.length];
ans = new String[tickets.length + 1];
ans[0] = ICN;
dfs(ICN, 0);
return ans;
}
public void dfs(String now, int depth) {
if (depth == tickets.length) {
flag = true;
}else{
List<Integer> able = new ArrayList<>();
for (int i = 0; i < tickets.length; i++) {
if (tickets[i][0].equals(now) && !visited[i]) {
able.add(i);
}
}
Collections.sort(able, (o1, o2) -> tickets[o1][1].compareTo(tickets[o2][1]));
for (int ableIndex : able) {
String next = tickets[ableIndex][1];
ans[depth + 1] = next;
visited[ableIndex] = true;
dfs(next, depth + 1);
if (flag) return; //μ΄λ―Έ μ°Ύμ
visited[ableIndex] = false;
}
}
}
}
'programmers' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[JAVA]68645 μΌκ°λ¬ν½μ΄ (0) | 2023.07.02 |
---|