기둥과 보 설치

기둥과 보 설치

import java.util. * ;

class Solution {

private static boolean [][] arr0, arr1;

private static int cnt = 0 ;

public static boolean CheckFrame( int n, int [] frame, int i, int j, int type){

// 기둥일 경우

if (type = = 0 ) {

// 바닥, 기둥의 위, 보의 끝 부분일 경우만 수행

if (i = = 0 | | i > 0 & & arr0[i - 1 ][j] | | arr1[i][j] | | j > 0 & & arr1[i][j - 1 ])

return true ;

} else { // 보일 경우

// 기둥의 위, 보의 양쪽 끝 부분이 연결된 경우만 수행

if (i > 0 & & arr0[i - 1 ][j] | | i > 0 & & j < n & & arr0[i - 1 ][j + 1 ] | | j > 0 & & j < n & & arr1[i][j - 1 ] & & arr1[i][j + 1 ])

return true ;

}

return false ;

}

public static boolean CheckRemove( int n, int [] frame, int i, int j, int type){

// 범위 밖일 경우

if (i > n | | i < 0 | | j > n | | j < 0 )

return true ;

// 비어있는 경우

if (type = = 0 ){

if ( ! arr0[i][j])

return true ;

} else {

if ( ! arr1[i][j])

return true ;

}

if (CheckFrame(n, frame, i, j, type))

return true ;

else

return false ;

}

public static void setFrame( int n, int [] frame){

int i = frame[ 1 ]; // 행

int j = frame[ 0 ]; // 열

int type = frame[ 2 ];

int action = frame[ 3 ];

String pos = Integer. toString (i) + Integer. toString (j);

// 범위 밖일 경우

if (i > n | | i < 0 | | j > n | | j < 0 )

return ;

// 프레임 설치

if (action = = 1 ){

if (CheckFrame(n, frame, i, j, type)){

if (type = = 0 )

arr0[i][j] = true ;

else

arr1[i][j] = true ;

cnt + + ;

}

} else { // 프레임 삭제

if (type = = 0 )

arr0[i][j] = false ;

else

arr1[i][j] = false ;

cnt - - ;

boolean flag = true ;

for ( int l = 0 ;l < = n;l + + ){

for ( int m = 0 ;m < = n;m + + ){

// 기둥과 보 규칙 검사

if ( ! CheckRemove(n, frame, l, m, 0 ) | | ! CheckRemove(n, frame, l, m, 1 )){

flag = false ;

break ;

}

}

if ( ! flag){

if (type = = 0 )

arr0[i][j] = true ;

else

arr1[i][j] = true ;

cnt + + ;

break ;

}

}

}

}

public int [][] solution( int n, int [][] build_frame) {

int [][] answer = {};

arr0 = new boolean [n + 1 ][n + 1 ];

arr1 = new boolean [n + 1 ][n + 1 ];

for ( int i = 0 ;i < build_frame. length ;i + + ){

setFrame(n, build_frame[i]);

}

answer = new int [cnt][ 3 ];

int idx = 0 ;

for ( int i = 0 ;i < = n;i + + ){

for ( int j = 0 ;j < = n;j + + ){

if (arr0[i][j]){

answer[idx][ 0 ] = j;

answer[idx][ 1 ] = i;

answer[idx][ 2 ] = 0 ;

idx + + ;

}

if (arr1[i][j]){

answer[idx][ 0 ] = j;

answer[idx][ 1 ] = i;

answer[idx][ 2 ] = 1 ;

idx + + ;

}

}

}

Arrays.sort(answer, new Comparator < int [] > (){

@Override

public int compare( int [] o1, int [] o2) {

int a = o1[ 0 ] - o2[ 0 ];

if (a = = 0 )

a = o1[ 1 ] - o2[ 1 ];

if (a = = 0 )

return o1[ 2 ] - o2[ 2 ];

return a;

}

});

return answer;

}

}

from http://zzunsik.tistory.com/202 by ccl(A) rewrite - 2021-09-19 12:01:51