🎯 목표 : 인접 행렬과 접점이 주어졌을때, 두 접점간의 연결 관계가 있는 지 여부를 반환하는 알고리즘 작성
public class DirectionsTest{
public static void main(String[] args) {
int[][] input = new int[][]
{
{0, 1, 0, 0, 0},
{0, 0, 0, 1, 0},
{0, 1, 0, 0, 0},
{0, 1, 1, 0, 0},
{1, 1, 1, 1, 0}
};
DataStructureQuiz2 a = new DataStructureQuiz2();
// input 인접 행렬을 받아 vertex 1 - 4에 도달할수 있는지 여부 확인
System.out.println(a.findDirections(input,1,4));
}
public boolean findDirections(int[][] matrix, int from, int to) {
int[][] compareArr = new int[matrix.length][];
for(int i=0; i< matrix.length; i++) compareArr[i]
=Arrays.copyOf(matrix[i],matrix[i].length);
if (from==to) return true;
for (int i=0; i<compareArr[from].length; i++){
if(compareArr[from][i]==1){
compareArr[from][i]=0;
if(getDirections(compareArr,i,to)) return true;
}
}
return false;
}
}
// 출력값
// false