on
[프로그래머스] [Level3] 여행경로 JAVA
[프로그래머스] [Level3] 여행경로 JAVA
import java.util. * ;
import java.io. * ;
class Solution {
boolean [] visit;
ArrayList < String > path = new ArrayList < > ();
PriorityQueue < String > ans = new PriorityQueue < > ( 1 );
public String [] solution( String [][] tickets) {
visit = new boolean [tickets. length ];
dfs( "ICN" , 0 ,tickets);
return ans.poll(). split ( " " );
}
private void dfs( String cur, int depth, String [][] tickets) {
path. add (cur);
if (depth = = tickets. length ){
ans. add ( String .join( " " , path));
return ;
}
for ( int i = 0 ;i < tickets. length ;i + + ){
if (tickets[i][ 0 ]. equals (cur) & & ! visit[i]) { // i번째 티켓을 쓰지 않았고 , 출발지가 같은 티켓이 있다면
visit[i] = true ;
dfs(tickets[i][ 1 ],depth + 1 ,tickets);
visit[i] = false ;
path.remove(path.size() - 1 );
}
}
}
}
from http://katastrophe.tistory.com/74 by ccl(A) rewrite - 2021-11-03 01:27:56