1. ๋ฌธ์ ์ ๋ํ์ฌ ๐ฆ
https://www.acmicpc.net/problem/15683
(1) ์กฐ๊ฑด ๋ถ์ ๐
์๋จ์์ ๋ฌธ์ ์์ ํ์์ด๋ค.
๊ทธ๋ฅ ๋ฌธ์ ์กฐ๊ฑด ๊ทธ๋๋ก ์๊ฐ๋ณต์ก๋ ์ ๊ฒฝ ์์ฐ๊ณ ํ๋ฉด ๋๋ค.
'์ค๋ฅ๊ฐ ์๋ค๋ฉด' ์์ด๊ณผ ์กฐํฉ์ ์จ๋ ๋ฌธ์ ๊ฐ ํ๋ ค์ผ ํ๋ค.
2. ์ฝ๋๊ฐ ๋์ค๊ธฐ๊น์ง ๐ ๏ธ
KEY WORD
: ์์ด, ์์ ํ์
![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|
1๋ฒ | 2๋ฒ | 3๋ฒ | 4๋ฒ | 5๋ฒ |
์ถ์ฒ: ํด๋น ๋ฌธ์ ์ ์ด๋ฏธ์ง ์ฌ์ง
A. ์์ด์ ์ด๋์ ์ฐ๋๊ฐ?
์์ด์ CCTV๊ฐ ๋์ํ ์ ์๋ ๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ฅผ ์ธ๊ธฐ ์ํ์ฌ ํ์๋ก ๋์ด์ง๋ค.
์์ ๋์ค๋ ๊ฐ CCTV๋ 90๋์ฉ ํ์ ์ด ๊ฐ๋ฅํ๋ค. 1,3,4๋ฒ์ ๋์๋จ๋ถ ๋ชจ๋ ๊ฒฝ์ฐ์ ์๊ฐ ๋ค๋ฅด๊ณ , 2๋ฒ์ ์ํ, ์ข์ฐ๋ง ๋ค๋ฅด๋ฉฐ 5๋ฒ์์๋ ๋์ฌ ์ ์๋ ๊ฒฝ์ฐ์ ์๊ฐ ๋ฑ ํ๋์ด๋ค. cctv๋ 1๊ฐ ์ด์ 8๊ฐ ์ดํ์ผ ์ ์๊ธฐ ๋๋ฌธ์, CCTV๊ฐ ๋ฐ๋ผ๋ณด๊ณ ์๋ ๋ฐฉํฅ์ ๋ฐ๋ผ ๊ฒฝ์ฐ์ ์๊ฐ ๋ฌด๊ถ๋ฌด์งํ๊ฒ ๋๋ ์ ์๋ค. ์ฐ๋ฆฌ๋ ์ด๊ฑธ ๋ฐฐ์ด์ ๋ด์์ ํํํ ๊ฒ์ด๋ค.
์ฆ index = cctv์ ๋ฒํธ, value๋ 0~3๋ฒ๊น์ง ์ํ์ข์ฐ๋ฅผ ๋ํ๋ธ๋ค. (2๋ฒ์ ๊ฒฝ์ฐ 0์ด ์ํ, 1์ด ์ข์ฐ, 5๋ฒ์ ๋ฌด์กฐ๊ฑด 0๋ง ์กด์ฌ), ์ฌ๊ธฐ์ ์กฐํฉ์ด ์๋ ์ด์ ๊ฐ ๋ค์ด๋๋ค. ์๋๋ฉด ๋ฐฐ์ด๊ฐ์ ์๋ก value๊ฐ์ ๋ฐ๊พธ๋ฉด, CCTV๋ง๋ค ๋ฐ๋ผ๋ณด๋ ๋ฐฉํฅ์ด ๋ฌ๋ผ์ ธ์ ์์ ์๋ก์ด ๊ฒฝ์ฐ์ ์๊ฐ ๋๊ธฐ ๋๋ฌธ์ด๋ค.
B. ์์ ํ์์ ๋ํ์ฌ
๋ฐ๋ณต๋ฌธ์ผ๋ก ๋ํ๋ด๊ธฐ
CCTV๊ฐ ๋ฐ๋ผ๋ด์ผ ํ๋ ๋ฐฉํฅ, CCTV์ ์์น, ์ ํ์ ์๋ ค์ฃผ๋ฉด, ํด๋น ๋ฐฉํฅ์ ํด๋นํ๋ ๋ชจ๋ ์ง์ ์ ๋ฒฝ์ด๋ ๋ฒ์ด๋ ๋๊น์ง ๋ฐฉ๋ฌธ์ฒ๋ฆฌ ํด๋ฒ๋ฆฌ๋ฉด ๋๋ค.
์ดํ ์ ์ฒด ๋ฐฐ์ด ๊ฐ์์ ์์ง๋ ๊ฐ์ด 0์ธ ์ง์ (์ฆ ์ฌ๊ฐ์ง๋)์ ๊ฐ์๋ฅผ ์ธ์ ๊ทธ ๊ฐ์๊ฐ ๊ฐ์ฅ ์์ ๋๋ฅผ ์ฐพ์๋ด ์ถ๋ ฅํ๋ฉด ๋๋ค.
(1) ์๊ฐ๋ณต์ก๋ ๋ถ์ โณ
N,M <= 8, CCTV = 8
ํด๋น ๋ฌธ์ ์์ ์ต์ ์ ๊ฒฝ์ฐ๋ก 4๊ฐ์ง ๋ฐฉํฅ์ด ๋ค ๋ค๋ฅธ 1,3,4๋ฒ CCTV๊ฐ 8๊ฐ ์ฃผ์ด์ก๋ค๊ณ ์ณ๋ณด์.
- ๋ชจ๋ ๊ฒฝ์ฐ์ ์ ์ฐพ๊ธฐ $O(4^8)$
- ๊ฐ ๊ฒฝ์ฐ์ ์๋ง๋ค ๊ฐ์ ์ ๋ ฅํ 2์ฐจ์ ๋ฐฐ์ด ๋ง๋ค๊ธฐ $O(N^2)$
- ๊ฐ ๊ฒฝ์ฐ์ ์๋ง๋ค ๋ฐฉ๋ฌธ์ฒ๋ฆฌ: ์ต๋ $O(N^2)$
- ๊ฐ ๊ฒฝ์ฐ์ ์๋ง๋ค ์ฌ๊ฐ์ง๋ ์ธ๊ธฐ $O(N^2)$
ํ ๊ฐ์ ๊ฒฝ์ฐ์ ์๋ง๋ค 2์ฐจ์ ๋ฐฐ์ด ๋ง๋ค๊ธฐ + ๋ฐฉ๋ฌธ์ฒ๋ฆฌ + ์ฌ๊ฐ์ง๋ ์ธ๊ธฐ๊ฐ ๋ณ๋ ฌ์ ์ผ๋ก ์ผ์ด๋๋ค.
์ฆ ์ด๋ฅผ ์ข
ํฉํ ์ต์
์ ๊ฒฝ์ฐ์ ์๋ $O(4^8 * (3N2))$ ์ด๋ค. N๋ 8์ด๋ผ๊ณ ์น๋ฉด
$O(4^8 * (34^{2}))$ ์ด๋ค.
์ฆ 3,145,728์ ์ฐ์ฐํ์๋ฅผ ํ์๋ก ํด์, 10^{8}์ด ๋งฅ์๋ฉ์ธ ์๊ณ ๋ฆฌ์ฆ ๋ฌธ์ ์์ ๊ฐ๋ฅํ๋ค.
3. ์ฝ๋ ๐
(1) SUDO CODE ๐ฌ
+ 0. CCTV ์ขํ์ ํ์
์ ์ฅํ๋ ํด๋์ค ๋ง๋ค๊ธฐ
+ 1. ๋ชจ๋ CCTV์ ๋ํด์ ๊ทธ๊ฒ์ ๋์ ๊ฒฝ์ฐ์ ์ ์ธ๊ธฐ
- (1) ํ๋์ ๊ฒฝ์ฐ์ ์๋ง๋ค ์ ์ฒด ์
๋ ฅ์ ๋ณต์ฌํ 2์ฐจ์ ๋ฐฐ์ด ์๋ก ๋ง๋ค๊ธฐ
- (2) ํด๋น ๋ฐฐ์ด์ ๋ํด์ CCTV๊ฐ ๋ฐ๋ผ๋ณด๋ ์ง์ ์ ์ฒด ๋ฐฉ๋ฌธ ์ฒ๋ฆฌํ๊ธฐ
- (3) ๊ทธ๋ผ์๋ ๋ณด์ง ๋ชปํ ์ฌ๊ฐ์ง๋ ๊ฐ์ ์ธ๊ธฐ
- (4) ์ด์ ๊น์ง ์ค ์ฌ๊ฐ์ง๋ ์ต์๊ฐ๊ณผ ๋น๊ต ํ ์ด๋ฒ ๊ฒ์ด ๋ ์๋ค๋ฉด ๊ฐฑ์
(2) JAVA CODE โ
import java.io.*;
import java.util.*;
public class Main{
static class Coordinate{
int r;
int c;
int type;
public Coordinate(int r, int c){
this.r = r;
this.c = c;
}
public Coordinate (int r, int c, int type){
this.r = r;
this.c = c;
this.type = type;
}
@Override
public boolean equals(Object obj) {
if(obj.getClass() != this.getClass()) return false;
Coordinate o = (Coordinate) obj;
return o.r == this.r && o.c == this.c;
}
@Override
public int hashCode() {
return this.r* 10 + this.c;
}
}
static int N,M, min = Integer.MAX_VALUE;
static int [][] field;
static int [][] dir = new int[][]{{-1,0}, {1,0}, {0,-1}, {0,1}}; // ์ํ์ข์ฐ
static ArrayList<Coordinate> list = new ArrayList<>();
public static void main(String[] args) throws IOException {
init();
permutation(new int[list.size()], 0);
System.out.println(min);
}
public static void init() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
field = new int[N][M];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < M; j++) {
field[i][j] = Integer.parseInt(st.nextToken());
if(field[i][j] != 0 && field[i][j] != 6) {
list.add(new Coordinate(i,j, field[i][j]));
}
}
}
}
public static void permutation (int [] nums, int depth ){
if(depth == list.size()){
int [][] map = new int [N][M];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
map[i][j] = field[i][j];
}
}
for (int i = 0; i < list.size(); i++) {
switch (list.get(i).type){
case 1: {
cctv1(nums[i], map,list.get(i));
break;
}
case 2:{
cctv2(nums[i], map, list.get(i));
break;
}
case 3: {
cctv3(nums[i], map, list.get(i));
break;
}
case 4: {
cctv4(nums[i], map,list.get(i));
break;
}
case 5: {
cctv5(map, list.get(i));
break;
}
}
}
int cnt = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if(map[i][j] == 0) cnt++;
}
}
min = Math.min(min, cnt);
return;
}
Coordinate now = list.get(depth);
int type = field[now.r][now.c];
int max = type == 5? 1 :
type == 2? 2: 4;
for (int i = 0; i < max; i++) {
nums[depth] = i;
permutation(nums, depth+1);
}
}
static void cctv1(int d, int [][] map, Coordinate coordinate) {
int nextR = coordinate.r;
int nextC = coordinate.c;
visit(d,map, nextR, nextC,1);
}
static void cctv2(int d, int[][] map, Coordinate coordinate) {
int nextR = coordinate.r;
int nextC = coordinate.c;
if(d == 0) { // ์ข์ฐ
visit(2,map, nextR, nextC,2);
nextR = coordinate.r;;
nextC = coordinate.c;
visit(3, map, nextR, nextC,2);
}else if (d == 1) { // ์ํ
visit(0,map, nextR, nextC,2);
nextR = coordinate.r;
nextC = coordinate.c;
visit(1, map, nextR, nextC,2);
}
}
static void cctv3(int d, int[][] map, Coordinate coordinate){
int nextR = coordinate.r;
int nextC = coordinate.c;
switch (d){ // ๊ฐ๊ฐ 90๋์ฉ
case 0: {
visit(0, map, nextR, nextC,3);
nextR = coordinate.r;
nextC = coordinate.c;
visit(3, map, nextR, nextC,3);
break;
}
case 1:{
visit(0, map, nextR, nextC,3);
nextR = coordinate.r;
nextC = coordinate.c;
visit(2, map, nextR, nextC,3);
break;
}
case 2: {
visit(1, map, nextR, nextC,3);
nextR = coordinate.r;
nextC = coordinate.c;
visit(2, map, nextR, nextC,3);
break;
}
case 3:{
visit(1, map, nextR, nextC,3);
nextR = coordinate.r;
nextC = coordinate.c;
visit(3, map, nextR, nextC,3);
}
}
}
static void cctv4(int d, int [][] map, Coordinate coordinate){
int nextR = coordinate.r;
int nextC = coordinate.c;
switch (d){
case 0: { // ์ข์์ฐ
visit(0, map, nextR, nextC, 4); // ์
nextR = coordinate.r;
nextC = coordinate.c;
visit(2, map, nextR,nextC, 4);
nextR = coordinate.r;
nextC = coordinate.c;
visit(3, map, nextR,nextC, 4);
break;
}
case 1: { // ์, ํ, ์ข
visit(0, map, nextR, nextC, 4); // ์
nextR = coordinate.r;
nextC = coordinate.c;
visit(1, map, nextR,nextC, 4);
nextR = coordinate.r;
nextC = coordinate.c;
visit(2, map, nextR,nextC, 4);
break;
}
case 2: { // ์,ํ,์ฐ
visit(0, map, nextR, nextC, 4); // ์
nextR = coordinate.r;
nextC = coordinate.c;
visit(1, map, nextR,nextC, 4);
nextR = coordinate.r;
nextC = coordinate.c;
visit(3, map, nextR,nextC, 4);
break;
}
case 3: { // ํ,์ข,์ฐ
visit(1, map, nextR, nextC, 4); // ์
nextR = coordinate.r;
nextC = coordinate.c;
visit(2, map, nextR,nextC, 4);
nextR = coordinate.r;
nextC = coordinate.c;
visit(3, map, nextR,nextC, 4);
break;
}
}
}
static void cctv5(int[][] map, Coordinate coordinate){
int nextR = coordinate.r;
int nextC = coordinate.c;
visit(0, map, nextR, nextC,5);
nextR = coordinate.r;
nextC = coordinate.c;
visit(1, map, nextR, nextC,5);
nextR = coordinate.r;
nextC = coordinate.c;
visit(2, map, nextR, nextC,5);
nextR = coordinate.r;
nextC = coordinate.c;
visit(3, map, nextR, nextC,5);
}
public static void visit (int d, int [][] map, int nextR, int nextC, int type) {
while (!OOB(nextR, nextC)){
if( map[nextR][nextC] == 6) break;
map[nextR][nextC] = type;
nextR += dir[d][0];
nextC += dir[d][1];
}
}
public static boolean OOB(int r, int c){
return r < 0 || c < 0 || r >= N || c >= M;
}
}
4. ํธ๋ฌ๋ธ ์ํ or ๋ฐฐ์ด ์ ๐
A. ๊ฐ์ด 0์ด ์๋ ์ง์ ์ ์ ๋ถ CCTV๋ก ์ฐฉ๊ฐํ๋ค๊ฐ ๋ ์๋ฌ
๋ฒฝ์ ๋ํ๋ด๋ 6 ๋ํ CCTV๋ก ์๊ฐํ๊ณ ์์ด ๊ณ์ฐ์ ๋ฃ์๋ค. 6์ ์ต๋ 64๊ฐ ๋์ค๋ฏ๋ก ์ด๋ฌ๋ฉด ๋น์ฐํ ์๊ฐ ์ด๊ณผ๊ฐ ๋จ.