• Home
  • About
    • Develop2r photo

      Develop2r

      안녕하세요 IT 개발자 임기남입니다. 한걸음 한걸음 나아가는 개발자를 꿈꾸고 있습니다.

    • Learn More
    • Twitter
    • Facebook
    • Instagram
    • Github
    • Steam
  • Posts
    • All Posts
    • All Tags
  • Projects
  • Algorithm

백준 1194 달이 차오른다 가자

21 Jun 2020

Reading time ~2 minutes

  • BFS - 문제 : 백준 1194

KeyPoint

  • Point Class 에 좌표를 저장하여 Queue의 Type Parameters 로 사용
  • 자료 구조 Queue 이용 BFS 구현
  • CHAR 형 문자 a ~ f 까지 총 6개의 열쇠를 비트마스크를 이용해 관리 => 총(2^6)의 방문배열을 사용해 체크

Code Snippets


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

public class S_1194 {
	
	static class Point{
		int x,y;
		int cnt,key;
		public Point(int x, int y, int cnt, int key) {
			super();
			this.x = x;
			this.y = y;
			this.cnt = cnt;
			this.key = key;
		}
		
		
		
		
	}
	
	static int dx[] = {0,0,1,-1};
	static int dy[] = {1,-1,0,0};
	
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer stk = new StringTokenizer(br.readLine());
		
		int N = Integer.parseInt(stk.nextToken()); // 세로
		int M = Integer.parseInt(stk.nextToken()); // 가로
		
		Point sp = null;
		Point ep = null;
		
		Queue<Point> q = new LinkedList<>();
		
		char arr[][] = new char[N][M];
		boolean visited[][][] = new boolean[64][N][M];
		
		for ( int i = 0; i < N; i++ ) {
			String s = br.readLine();
			
			for ( int j = 0; j < M; j++ ) {
				arr[i][j] = s.charAt(j);
				if( arr[i][j] == '0' ) {
					sp = new Point(i,j,0,0); // 시작
				}
			}
		}
		
		q.add(sp);
		visited[0][sp.x][sp.y]=true;
		
		int result = -1;
		
		loop:while(!q.isEmpty()) {
			Point tmp = q.poll();
			for (int i = 0; i < 4; i++) {
				
				int nx = tmp.x + dx[i];
				int ny = tmp.y + dy[i];
				int key=tmp.key;
				
				if(nx < 0 || nx >=N || ny < 0 || ny >=M)
					continue;
				if(arr[nx][ny]=='#')
					continue;
				if(arr[nx][ny]=='1') {
					result = tmp.cnt+1;
					break loop;
				}
				if(visited[tmp.key][nx][ny])
					continue;
				if(arr[nx][ny]>='a' && arr[nx][ny]<='f') {
					if((key & (1 << (arr[nx][ny]-'a'))) != (1 << (arr[nx][ny]-'a')))//키를 처음 얻으면
						key += 1 << (arr[nx][ny]-'a');
					visited[key][nx][ny]=true;
					q.add(new Point(nx,ny,tmp.cnt+1,key));
				}else if(arr[nx][ny]>='A' && arr[nx][ny]<='F'){
					if((key & (1 << (arr[nx][ny]-'A'))) != (1 << (arr[nx][ny]-'A')))//열수 있는 키가 없으면
						continue;
					visited[key][nx][ny]=true;
					q.add(new Point(nx,ny,tmp.cnt+1,key));
				}
				visited[key][nx][ny]=true;
				q.add(new Point(nx,ny,tmp.cnt+1,key));
			}
		}
		
		System.out.println(result);
		
	}

}



algorithmbfsbaekjoonbitmask Share Tweet +1