게하! 개하! (게임도 하고 개발도 하고)

[프로그래머스] 여행경로 - java 본문

알고리즘/[2021] 프로그래머스

[프로그래머스] 여행경로 - java

Sorrel 2021. 6. 1. 01:48
import java.util.ArrayList;
import java.util.Collections;
class Solution {
    ArrayList<String> answers = new ArrayList<String>();
    void dfs(String begin, String[][]tickets, int count, String answer, boolean[]visited){
        if(count==tickets.length){
            answers.add(answer);
            return;
        }
        
        for(int i = 0; i<tickets.length; i++){
            if(!visited[i]&&begin.equals(tickets[i][0])){
                visited[i]=true;
                dfs(tickets[i][1], tickets, count+1, answer+" "+tickets[i][1], visited);
                visited[i]=false;
            }
        }
    }
    public String[] solution(String[][] tickets) {
        
        boolean[] visited = new boolean[tickets.length];
        dfs("ICN", tickets, 0, "ICN", visited);
        Collections.sort(answers);
        String[] answer = new String[tickets.length+1];
        answer = answers.get(0).split(" ");
        return answer;
    }
}
0 Comments
댓글쓰기 폼