ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

[๋ฌธ์ œ]

[ํ’€์ด]

์—ด์‡ ๊ฐ€ ํŠ€์–ด๋‚˜์˜จ ๋ถ€๋ถ„๊ณผ, ์ž ๊ธˆ์žฅ์น˜์˜ ํŒŒ์ธ๋ถ€๋ถ„ ์ขŒํ‘œ๋งŒ ๊ฐ๊ฐ ์ €์žฅํ•œ ํ›„, ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์—ด์‡ ์˜ ์œ„์น˜๋ฅผ ์˜ฎ๊ฒจ๊ฐ€๋ฉฐ ํƒ์ƒ‰ํ•˜์˜€์ง€๋งŒ ์‹œ๊ฐ„์ดˆ๊ณผ๋กœ ์‹คํŒจํ•˜์˜€์Šต๋‹ˆ๋‹ค.

์œ„ : x, y - 1
์˜ค๋ฅธ์ชฝ : x + 1, y
์™ผ์ชฝ : x - 1, y
์•„๋ž˜ : x, y + 1
90*ํšŒ์ „ : y,x

 

๋”ฐ๋ผ์„œ ํ•ด๋‹น ๋ธ”๋กœ๊ทธ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ํ’€์ด๋ฅผ ์ง„ํ–‰ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

https://jellyinghead.tistory.com/28

 

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ž๋ฌผ์‡ ์™€ ์—ด์‡  (์ž๋ฐ”)

https://programmers.co.kr/learn/courses/30/lessons/60059 ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์ž๋ฌผ์‡ ์™€ ์—ด์‡  [[0, 0, 0], [1, 0, 0], [0, 1, 1]] [[1, 1, 1], [1, 1, 0], [1, 0, 1]] true programmers.co.kr ์•ฝ 7%์˜ ์ •๋‹ต๋ฅ ์ธ..

jellyinghead.tistory.com

์ž๋ฌผ์‡ ์˜ ํฌ๊ธฐ๋ฅผ n์—์„œ n+2*(m-1)๋กœ ํ™•์žฅํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด extended[][] ์„ ๋งŒ๋“ค์–ด ์—ด์‡ ๋ฅผ ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์œ„์น˜์— ์˜ฌ๋ ค์„œ ์ฐพ์•„๋ณด๋ฉด ๋˜๋Š” ๊ฑฐ์˜€์Šต๋‹ˆ๋‹ค. 

ํ™•์žฅ๋œ ์ž๋ฌผ์‡  ๋ฐฐ์—ด

- moveKey()๋ฅผ ํ†ตํ•ด ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์—ด์‡ ๋ฅผ ๊ฝ‚์•„๋ณด๋ฉฐ ํƒ์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.

- unLock()์„ ํ†ตํ•ด ์ž๋ฌผ์‡ ์— ํ‚ค๋ฅผ ์ •ํ™•ํžˆ ๊ฝ‚์„์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ, ๋งŒ์•ฝ 1์„ ๊ฐ€์ง„ ๋ชจ๋“  ํ‚ค์˜ ๊ฐ’์ด ์‹ค์ œ ์ž๋ฌผ์‡ ์˜ ์ขŒํ‘œ์ธ

(m-1 ~ N-m, m-1~N-m)์— ์œ„์น˜ํ•œ๋‹ค๋ฉด ์ž๋ฌผ์‡ ์— ๊ฝ‚ํžŒ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผํ•˜๊ณ  cnt++๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

- ๋งŒ์•ฝ, ์ž๋ฌผ์‡ ์˜ ํŒŒ์ธ ๊ฐœ์ˆ˜ hole == cnt ๋ผ๋ฉด ์ž๋ฌผ์‡ ๋Š” ํ’€๋ ธ๋‹ค๊ณ  ๊ฐ„์ฃผํ•ฉ๋‹ˆ๋‹ค.

 

 

[์ฝ”๋“œ]

import java.util.*;
class Solution {
    public static int n;
    public static int m;
    public static int N;
    public static int extened[][]; // ํ™•์žฅ๋œ lock
    public static int hole = 0;
    public boolean solution(int[][] key, int[][] lock) {
        boolean answer = true;
        n = lock.length;
        m = key.length;
        N = n + 2*(m-1);
        extened = new int[N][N];
        extend(lock);
        answer = moveKey(key);
        return answer;
    }
    public static void extend(int[][] lock) {
        for(int i = 0; i < n; i++) {
			for(int j = 0; j < n; j++) {
                if(lock[i][j] == 0) {
                    hole++;
                }
				extened[m-1+i][m-1+j] = lock[i][j];   
            }
		}
		// for(int i = 0; i < N; i++) {
		// 	for(int j = 0; j < N; j++) {
		// System.out.print(extened[i][j] + " ");
		// }
		// System.out.println();
		// }
		// System.out.println(hole);
    }
    public static boolean moveKey(int[][] key) {
		for(int i = 0; i <= N-m; i++) {
			for(int j = 0; j <= N-m; j++) {
                //์ž๋ฌผ์‡ ์— ํ‚ค๋ฅผ ๊ฝ‚์Œ
                if(unLock(key,i,j)){
                    return true;
                }
			}
		}
        for(int k=0;k<3;k++) {
            key = rotate(key);
            for(int i = 0; i <= N-m; i++) {
                for(int j = 0; j <= N-m; j++) {
                    //์ž๋ฌผ์‡ ์— ํ‚ค๋ฅผ ๊ฝ‚์Œ
                    if(unLock(key,i,j)){
                        return true;
                    }
                }
            }
        }
        return false;
    }
    public static boolean unLock(int[][] key, int i, int j) {
        int cnt = 0;
        for(int x = 0; x < m; x++) {
			for(int y = 0; y < m; y++) {
				if(extened[i+x][j+y] == 1 && key[x][y] == 1) {
                    return false;
                }
				else if(extened[i+x][j+y] == 0 && key[x][y] == 1) {
                    if(m-1 <= x+i && x+i <= N-m && m-1 <= y+j && y+j <= N-m) {
                        cnt++;
                    }
                }
			}
		}
        if(cnt == hole) {
            return true;
        }
        return false;
    }
    public static int[][] rotate(int key[][]) {
		int len = key.length;
		int convert[][] = new int[len][len];

		for(int i = 0; i < len; i++) {
			for(int j = 0; j < len; j++) {
				convert[j][len-i-1] = key[i][j];
			}
		}

		return convert;
	}
}