๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 16197_๋‘ ๋™์ „

๋ฟŒ์•ผ._. 2022. 12. 26. 12:18

Gold IV

๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/16197)

< ๋‘ ๋™์ „ >

 

๋ฌธ์ œ ํ’€์ด

 

bfs๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๊ฐ ๋™์ „์„ ์ƒํ•˜์ขŒ์šฐ๋กœ ์›€์ง์ธ๋‹ค. ๊ทธ ํ›„ ๋ณด๋“œ ๋ฒ”์œ„๋ฅผ ํ™•์ธํ•˜์—ฌ ๋™์ „์ด ๋‘˜ ๋‹ค ๋ณด๋“œ ์•ˆ์ด๋ฉด queue์— ์ถ”๊ฐ€, ํ•˜๋‚˜๋งŒ ๋–จ์–ด์กŒ์œผ๋ฉด ์ข…๋ฃŒ, ๋‘˜ ๋‹ค ๋–จ์–ด์กŒ์œผ๋ฉด ๊ณ„์†ํ•ด์„œ ์ง„ํ–‰ํ•œ๋‹ค.

 

 

 - my solution (Java)

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

public class _16197_ { // ๋‘ ๋™์ „
	static int[] dx= {-1,1,0,0};
	static int[] dy= {0,0,-1,1};
	static char[][] map;
	static Queue<int[]>queue;
	static int n,m;
	static int result=-1;

	public static void main(String[] args) throws IOException {
		BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer(bf.readLine());
		
		n=Integer.parseInt(st.nextToken());
		m=Integer.parseInt(st.nextToken());
		
		map=new char[n][m];
		queue=new LinkedList<>();
		for(int i=0; i<n; i++) {
			String s=bf.readLine();
			for(int j=0; j<m; j++) {
				map[i][j]=s.charAt(j);
				if(map[i][j]=='o')queue.add(new int[] {i,j,0}); // ๋™์ „ ์œ„์น˜ ์ €์žฅ
			}
		}
		search();
		System.out.println(result);
	}

	private static void search() {
		while(!queue.isEmpty()) {
			int []coin1=queue.poll();
			int []coin2=queue.poll();
			
			if(coin1[2]+1>10) continue;
			for(int i=0; i<4; i++) {
				int count=0;
				int x1=coin1[0]+dx[i];
				int y1=coin1[1]+dy[i];
				
				int x2=coin2[0]+dx[i];
				int y2=coin2[1]+dy[i];
				
				if(x1>=0 && x1<n && y1>=0 && y1<m && map[x1][y1]=='#') { // ์ด๋™ํ•˜๋ ค๋Š” ์นธ์ด ๋ฒฝ์ด๋ฉด ์ด๋™ x
					x1=coin1[0];
					y1=coin1[1];
				}else if(x1<0 || x1>=n || y1<0 || y1>=m) { // ๋ฐ”๊นฅ
					count+=1;
				}
				
				if(x2>=0 && x2<n && y2>=0 && y2<m && map[x2][y2]=='#') {
					x2=coin2[0];
					y2=coin2[1];
				}else if(x2<0 || x2>=n || y2<0 || y2>=m) {
					count+=1;
				}
				
				if(count==0 && !(x1==coin1[0] && y1==coin1[1] && x2==coin2[0] && y2==coin2[1])) { 
					queue.add(new int[] {x1,y1,coin1[2]+1});
					queue.add(new int[] {x2,y2,coin1[2]+1});
				}else if(count==1) { // ํ•˜๋‚˜๋งŒ ๋–จ์–ด์ง
					result=coin1[2]+1;
					break;
				}
			}
			if(result>0) break;
		}	
	}
}

 

  • Main
    • n, m ์ž…๋ ฅ
    • map : ๋ณด๋“œ
    • queue : ๋™์ „์˜ [x์ขŒํ‘œ, y์ขŒํ‘œ, ํšŸ์ˆ˜]๋ฅผ ์ €์žฅ
    • search ํ•จ์ˆ˜ ํ˜ธ์ถœ
  • search
    • queue์—์„œ ๋™์ „ 2๊ฐœ์˜ ์œ„์น˜๋ฅผ ๊บผ๋‚ด์˜ด
    • ๋ฒ„ํŠผ์„ 10๋ฒˆ๋ณด๋‹ค ๋งŽ์ด ๋ˆŒ๋Ÿฌ์•ผ ํ•œ๋‹ค๋ฉด ๋‹ค์‹œ ๋™์ „ ๊บผ๋‚ด๊ธฐ๋ถ€ํ„ฐ! ๋Œ์•„๊ฐ!
    • ๋™์ „ 2๊ฐœ๋ฅผ ๊ฐ๊ฐ ์ƒํ•˜์ขŒ์šฐ๋กœ ์›€์ง์ž„ -> ๊ฐ ๋™์ „์ด ์ด๋™ํ•˜๋ ค๋Š” ์นธ์ด ๋ฒฝ์ด๋ฉด ์ด๋™ x BUT ๋ณด๋“œ ๋ฐ–์ด๋ผ๋ฉด count ์ฆ๊ฐ€  -> ๋™์ „์ด ํ•˜๋‚˜๋„ ๋ฐ–์œผ๋กœ ๋–จ์–ด์ง€์ง€ ์•Š์•˜์œผ๋ฉฐ ์›๋ž˜ ์œ„์น˜์™€ ๋‹ค๋ฅด๋‹ค๋ฉด queue์— ์ถ”๊ฐ€! / ๋™์ „์ด ํ•˜๋‚˜๋งŒ ๋–จ์–ด์กŒ๋‹ค๋ฉด ์ข…๋ฃŒ! / ๋™์ „์ด ๋‘˜ ๋‹ค ๋–จ์–ด์กŒ๋‹ค๋ฉด ๊ณ„์†ํ•ด์„œ ์ˆ˜ํ–‰

์ƒ๊ฐ๐Ÿค”

 

bfs๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉฐ ์กฐ๊ฑด๋งŒ ์ž˜ ์„ค์ •ํ•ด์ฃผ๋ฉด ๊ธˆ๋ฐฉ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 


'๐ŸŒžAlgorithm > ๐Ÿ”ฅBaekjoon' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Baekjoon] 6593_์ƒ๋ฒ” ๋นŒ๋”ฉ  (0) 2023.01.08
[Baekjoon] 13565_์นจํˆฌ  (1) 2022.12.27
[Baekjoon] 9019_DSLR  (0) 2022.12.23
[Baekjoon] 12919_A์™€ B 2  (0) 2022.12.22
[Baekjoon] 15644_๊ตฌ์Šฌ ํƒˆ์ถœ 3  (0) 2022.12.21