공부기록
임시 본문
더보기
import java.util.*;
class Solution {
class Pair{
int row;
int col;
public Pair(int row, int col){
this.row = row;
this.col = col;
}
@Override
public boolean equals(Object o){
Pair pair = (Pair)o;
if(this.row == pair.row && this.col == pair.col)
return true;
return false;
}
@Override
public String toString(){
return ("Node{" +
"row : " + Integer.toString(this.row) +
", col : " + Integer.toString(this.col) +
"}");
}
}
class Node{
Pair p1;
Pair p2;
int depth;
Node(Pair p1, Pair p2, int depth){
this.p1 = p1;
this.p2 = p2;
this.depth = depth;
}
}
private final int mv[][] = {
{-1,0},
{-1,1},
{0,1},
{1,1},
{1,0},
{1,-1},
{0,-1},
{-1,-1},
};
private boolean canUse(int N, Pair p){
if(N <= p.row || p.row < 0 || N <= p.col || p.col <0)
return false;
else
return true;
}
private boolean isCandidate(int[][] board, int his[][], Pair point, int n_d){
int N = board.length;
if(this.canUse(N, point) == false){
return false;
}else if(board[point.row][point.col] == 1){
return false;
}else if(his[point.row][point.col] < n_d){
return false;
}
return true;
}
public int solution(int[][] board) {
int answer = 0;
int N = board.length;
int his[][] = new int[N][N];
Queue<Node> q = new LinkedList<>();
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
his[i][j] = 2000000000;
}
}
q.add(new Node(new Pair(0,0), new Pair(0,1), 0));
his[0][0] = 0;
his[0][1] = 0;
Pair dst = new Pair(N-1, N-1);
while(q.size() != 0){
Node curNode = q.poll();
Pair p1 = curNode.p1;
Pair p2 = curNode.p2;
int c_d = curNode.depth;
if(p1.equals(dst) || p2.equals(dst)){
answer = c_d;
break;
}
//그대로 이동
for(int i=0; i<8; i+=2){
Pair n1 = new Pair(p1.row+mv[i][0], p1.col+mv[i][1]);
Pair n2 = new Pair(p2.row+mv[i][0], p2.col+mv[i][1]);
int n_d = c_d+1;
ArrayList<Pair> checkList = new ArrayList<>();
//변동이 있는 아이들을 구하자.
if(n1.equals(p1) == false && n1.equals(p2) == false){
checkList.add(n1);
}
if(n2.equals(p1) == false && n2.equals(p2) == false){
checkList.add(n2);
}
//변동이 있는 아이들만 체크
boolean canGo = true;
for(Pair point : checkList){
canGo = this.isCandidate(board, his, point, n_d);
if(canGo == false)
break;
}
if(checkList.size() == 0)
canGo = false;
if(canGo){
//his는 변동이 있는 아이만
for(Pair point : checkList){
his[point.row][point.col] = n_d;
}
q.add(new Node(n1, n2, n_d));
}
}
//회전
int tmp_r = p2.row-p1.row;
int tmp_d = p2.col-p1.col;
//p1기준, p2기준
ArrayList<Pair> vecList = new ArrayList(
Arrays.asList(
new Pair(tmp_r, tmp_d),
new Pair(-1*tmp_r, -1*tmp_d)
)
);
ArrayList<Pair> standList = new ArrayList(Arrays.asList(p1, p2));
for(int k=0; k<2; k++){
Pair curStand = standList.get(k);
int d_r = vecList.get(k).row;
int d_c = vecList.get(k).col;
int n_d = c_d+1;
int curDir;
for(curDir=0; curDir<8; curDir+=2){
if(this.mv[curDir][0] == d_r && this.mv[curDir][1] == d_c){
//시계 반시계
int mult[] = {1, -1};
for(int i=0; i<2; i++){
Pair diag = new Pair(curStand.row+this.mv[(curDir+8+1*mult[i])%8][0], curStand.col+this.mv[(curDir+8+1*mult[i])%8][1]);
Pair n1 = new Pair(curStand.row+this.mv[(curDir+8+2*mult[i])%8][0], curStand.col+this.mv[(curDir+8+2*mult[i])%8][1]);
//대각선 점에 대해서
//1. 범위를 만족하는지
//2. 벽을 지나지 않는지
//회전된 점에대해서
//모든 조건 검사
if(this.canUse(N, diag) == false){
}else if(board[diag.row][diag.col] == 1){
}else if(this.isCandidate(board, his, n1, n_d)){
his[n1.row][n1.col] = n_d;
q.add(new Node(curStand, n1, n_d));
}
}
break;
}
}
}
}
return answer;
}
}