Notice
Recent Posts
Recent Comments
Link
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
Tags
more
Archives
Today
Total
관리 메뉴

코드와이

[BAEKJOON] 16985. Maaaaaaaaaze 본문

acmicpc

[BAEKJOON] 16985. Maaaaaaaaaze

코드와이 2021. 4. 21. 13:53

 

BFS 순열

문제링크

www.acmicpc.net/problem/16985

 

16985번: Maaaaaaaaaze

첫째 줄부터 25줄에 걸쳐 판이 주어진다. 각 판은 5줄에 걸쳐 주어지며 각 줄에는 5개의 숫자가 빈칸을 사이에 두고 주어진다. 0은 참가자가 들어갈 수 없는 칸, 1은 참가자가 들어갈 수 있는 칸을

www.acmicpc.net

 

package acmicpc.Gold3;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.StringTokenizer;

public class maaaaze {

	static List<int[][]> list;
	static int[] numbers;
	static int ans;
	
	static class Point{
		int x;
		int y;
		int z;
		int cnt;
		
		public Point(int x, int y, int z, int cnt) {
			super();
			this.x = x;
			this.y = y;
			this.z = z;
			this.cnt = cnt;
		}
	}

	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		
		list = new ArrayList<>();
		
		int[][][] arr = new int[5][5][5];
		
		for(int i = 0 ; i < 5 ; i++) {
			for(int j = 0 ; j < 5 ; j++) {
				st = new StringTokenizer(br.readLine());
				for(int k = 0 ; k < 5 ; k++) {
					arr[i][j][k] = Integer.parseInt(st.nextToken());
				}
			}
		}
		
		for(int i = 0 ; i < 5 ; i++) {
			list.add(rotate(arr[i]));
			for(int j = 0 ; j < 3 ; j++) {
				list.add(rotate(list.get(i*4 + j)));
			}
		}
		numbers = new int[5];
		
		ans = 987654321;
		perm(0);
		System.out.println(ans == 987654321 ? -1 : ans);
	}
	
	static int[][][] map;
	public static void perm(int cnt) {
		if(cnt == 5) {
			shuffle(0);
			return;
		}
		
		for(int i = 0 ; i < 4 ; i++) {
			numbers[cnt] = i;
			perm(cnt+1);
		}
	}
	
	static int[] input = new int[5];
	static boolean[] isSelected = new boolean[5];
	public static void shuffle(int cnt) {
		if(cnt == 5) {
			map = new int[5][5][5];
			for(int i = 0 ; i < 5 ; i++) {
				map[input[i]] = list.get(4 * i + numbers[i]);
			}
			if(map[0][0][0] == 0 || map[4][4][4] == 0) return;
			
			go();
			return;
		}
		
		for(int i = 0 ; i < 5 ; i++) {
			if(isSelected[i]) continue;
			isSelected[i] = true;
			input[cnt] = i;
			shuffle(cnt+1);
			isSelected[i] = false;
		}
	}
	
	static int[] dr = {-1, 1, 0, 0, 0, 0};
	static int[] dc = {0, 0, 1, -1, 0, 0};
	static int[] dz = {0, 0, 0, 0, 1, -1};
	public static void go() {
		Queue<Point> queue = new LinkedList<>();
		queue.offer(new Point(0,0,0,0));
		
		boolean[][][] visited = new boolean[5][5][5];
		visited[0][0][0] = true;

		while(!queue.isEmpty()) {
			Point q = queue.poll();
			
			if(q.x == 4 && q.y == 4 && q.z == 4) {
				ans = Math.min(ans, q.cnt);
				return;
			}
			for(int d = 0 ; d < 6 ; d++) {
				int nx = q.x + dr[d];
				int ny = q.y + dc[d];
				int nz = q.z + dz[d];
				
				if(nx < 0 || ny < 0 || nz < 0 || nx > 4 || ny > 4 || nz > 4 || map[nx][ny][nz] == 0 || visited[nx][ny][nz]) continue;
				visited[nx][ny][nz] = true;
				queue.offer(new Point(nx,ny,nz,q.cnt+1));
			}
		}
	}
	
	public static int[][] rotate(int[][] arr){
		int[][] tmp = new int[5][5];
		
		for(int i = 0 ; i < 5 ; i++) {
			for(int j = 0 ; j < 5 ; j++) {
				tmp[j][5-i-1] = arr[i][j];
			}
		}
		return tmp;
	}
}

'acmicpc' 카테고리의 다른 글

[BAEKJOON] 11404. 플로이드  (0) 2021.04.22
[BAEKJOON] 15683. 감시  (0) 2021.04.21
[BAEKJOON] 7579. 앱  (0) 2021.04.19
[BAEKJOON] 1516. 게임 개발  (0) 2021.04.19
[BAEKJOON] 2056. 작업  (0) 2021.04.19