프로그래머스 코딩테스트 연습문제 (하노이의 탑)
import java.util.Arrays;
class Dfs {
int n;
int x = 0;
int[][] ans = {};
void setN(int m){
this.n = m;
}
void setAns(){
ans = new int[(int)Math.pow(2,n)-1][2];
}
void Hanoi(int m, int s, int t, int v) {
if (m == 1) {
ans[x++] = new int[]{s,t};
}
else {
Hanoi(m - 1, s, v, t);
ans[x++] = new int[]{s,t};
Hanoi(m - 1, v, t, s);
}
}
void getPrint(){
System.out.println(Arrays.deepToString(ans));
}
int[][] getAns(){
return ans;
}
}
class Solution {
public int[][] solution(int n) {
int[][] answer = {};
Dfs df = new Dfs();
df.setN(n);
df.setAns();
df.Hanoi(n, 1, 3, 2);
df.getPrint();
answer = df.getAns();
return answer;
}
}
2시간 동안 고민했는데 모르겠어서 찾아봄...ㅜㅜ
댓글