Maze_BFS

Queue, KEEP ROLLING

There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolling up, down, left or right, but it won't stop rolling until hitting a wall. When the ball stops, it could choose the next direction.

Given the ball's start position, the destination and the maze, determine whether the ball could stop at the destination.

The maze is represented by a binary 2D array. 1 means the wall and 0 means the empty space. You may assume that the borders of the maze are all walls. The start and destination coordinates are represented by row and column indexes.

๋ฌธ์ œ ์ ‘๊ทผ ๋ฐฉ๋ฒ•

์ž๋ฃŒ๊ตฌ์กฐ : Queue

์•Œ๊ณ ๋ฆฌ์ฆ˜ : Queue template + KEEP ROLLING PROCESS 1. ๊ธฐ๋ณธ์ ์œผ๋กœ Queue Template์„ ์ ์šฉํ•œ๋‹ค. 2. ๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ ์กฐ๊ฑด์ธ ๋ฒฝ์„ ๋งŒ๋‚˜๊ธฐ ์ „๊นŒ์ง€ Keep Rolling process๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค. Maze ์œ ํšจํ•œ ๋ฒ”์œ„๋‚ด์—์„œ ๊ณ„์† ๊ทธ ๋ฐฉํ–ฅ์œผ๋กœ ์ง์ง„ํ•œ๋‹ค.

while(nx >= 0 && nx < m && ny >= 0 && ny < n && maze[nx][ny] != 1) {//์กฐ๊ฑด์ด ์ฐธ์ผ ๋™์•ˆ KEEP ROLLING! ์ด ๋ฌธ์ œ์˜ ํ•ต์‹ฌ!
   nx += dir[0];
   ny += dir[1];
}

์ด ๋ฌธ์ œ์˜ ํ•ต์‹ฌ ๋‹ค๋ฅธ ๊ทธ๋ž˜ํ”„ ๋ฌธ์ œ๋“ค๊ณผ ๋‹ฌ๋ฆฌ ์ด ๋ฌธ์ œ์—์„œ๋Š” ํƒ์ƒ‰ํ•  ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ์ •ํ•  ๋•Œ ๋ฐ”๋กœ ์˜†์˜ ์™ผ์ชฝ ๋…ธ๋“œ ๋˜๋Š” ๋ฐ”๋กœ ์˜†์˜ ์˜ค๋ฅธ์ชฝ ๋…ธ๋“œ๊ฐ€ ์•„๋‹ˆ๋ผ ๋ฒฝ('1')์„ ๋งŒ๋‚  ๋•Œ๊นŒ์ง€ ๊ทธ ๋ฐฉํ–ฅ์œผ๋กœ ์ญ‰ ์ง„ํ–‰ํ•ด์„œ ๋ฒฝ('1')์„ ๋งŒ๋‚˜๊ธฐ ์ง์ „์˜ ์œ„์น˜๊ฐ€ ๊ทธ ๋‹ค์Œ ๋…ธ๋“œ๊ฐ€ ๋œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์ด ํŠน์ˆ˜ํ•œ process๋ฅผ ๊ตฌํ˜„ํ•ด์„œ ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ๊ฒฐ์ •ํ•˜๊ณ , ๊ณ„์† traverseํ•œ๋‹ค.

DFS ํ’€์ด๋ฒ•๊ณผ BFS ํ’€์ด๋ฒ• ๋น„๊ต 1. DFS : DFS๋Š” ๊นŠ์ด ์žˆ๊ฒŒ ์ญ‰ ํŒŒ๊ณ ๋“ ๋‹ค. ์žฌ๊ท€ํ˜ธ์ถœ ๋˜๋Š” ์Šคํƒ์œผ๋กœ ๊ตฌํ˜„ ๊ฐ€๋Šฅํ•˜๋‹ค. 2. BFS : BFS๋Š” level๋ณ„๋กœ ํƒ์ƒ‰์„ ์ง„ํ–‰ํ•œ๋‹ค.

์•Œ๊ณ ๋ฆฌ์ฆ˜์„ Java๋กœ ๊ตฌํ˜„

package graph;
import java.util.*;
public class Maze_BFS {
	//์ด ๋ฌธ์ œ์˜ ํ•ต์‹ฌ์€ ๋ฌด์—‡์ธ๊ฐ€.
	//DFS ํ’€์ด๋ฒ•๊ณผ BFS ํ’€์ด๋ฒ•์˜ ๋น„๊ต, ์ฐจ์ด์ .
	public static void main(String[] args) {
		int[][] maze ={
				  {0,0,1,0,0},
				  {0,0,0,0,0},
				  {0,0,0,1,0},
				  {1,1,0,1,1},
				  {0,0,0,0,0}
				};
		int[] start = {0,4};
		int[] dest = {4,4};
		int[] dest2 = {3,2};
		Maze_BFS a = new Maze_BFS();
		System.out.println(a.hasPath(maze,start,dest));
		
		System.out.println("Start(0,4) and Destination(4,4) : "+a.hasPath(maze,start,dest));
		System.out.println("Start(0,4) and Destination(3,2) : "+a.hasPath(maze,start,dest2));
	}
	int[][] dirs = {{-1,0},{1,0},{0,-1},{0,1}};//์ƒ ํ•˜ ์ขŒ ์šฐ 
	int m,n;
	public boolean hasPath(int[][] maze, int[] start, int[] dest) {//hasPath ์—†์–ด๋„ ๋  ๊ฒƒ ๊ฐ™์€๋ฐ ๋ญ”๊ฐ€ visited ๊ฐ€ ๊ฑธ๋ฆฐ๋‹ค.
		//์—๋Ÿฌ ์ œ์™ธ 
		if(maze == null || maze.length == 0) return false;
		//0.๋‹ด์„ ๊ทธ๋ฆ‡ ์ƒ์„ฑ 
		m = maze.length;
		n = maze[0].length;
		boolean[][] visited = new boolean[m][n];//์ค‘๋ณต์„ ์ฒดํฌํ•˜๋Š” ๋ฐฐ์—ด. set์œผ๋กœ ํ•˜๊ฑฐ๋‚˜ ์›๋ž˜ ์ขŒํ‘œ๋ฅผ distintiveํ•œ ๋‹ค๋ฅธ ์ˆซ์ž๊ฐ’์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋ฉด ์ถ”๊ฐ€ ์ €์žฅ๊ณต๊ฐ„ ๋ถˆํ•„์š”.
		System.out.println("===================================");
		return bfs(maze,start,dest,visited);
	}
	public boolean bfs(int[][] maze, int[] start, int[] dest, boolean[][] visited) {
		//์•„๋‹Œ ๊ฒฝ์šฐ๋ฅผ ์กฐ๊ฑด์œผ๋กœํ•˜์—ฌ ์˜ˆ์™ธ์ฒ˜๋ฆฌ.
		if(start[0] < 0 || start[0] >= m || start[1] < 0 || start[1] >= n || visited[start[0]][start[1]])
			return false;
		
		//1.๋‹ด์„ ๊ทธ๋ฆ‡ ์ƒ์„ฑ 
		Queue<int[]> queue = new LinkedList<>();
		queue.offer(start);
		visited[start[0]][start[1]] = true;
		
		//ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•œ๋‹ค. (queue recursive loop)
		while(!queue.isEmpty()) {
			//ํ์—์„œ ๋…ธ๋“œํ•˜๋‚˜ ๋ฝ‘๋Š”๋‹ค.
			int curr[] = queue.poll();
			//์‚ฌ๋ฐฉ์œผ๋กœ ๋Œ๋ฆฐ๋‹ค.
			for(int[] dir:dirs) {
				int nx = curr[0];//queue์—์„œ ๊บผ๋‚ธ ์ขŒํ‘œ๋Š” for๋ฌธ ์•ˆ์— ๋“ค์–ด์™€์•ผํ•œ๋‹ค. 
				int ny = curr[1];
				if(nx == dest[0] && ny == dest[1]) return true;//queue์—์„œ ๋‚˜์˜จ ๊ฒƒ์ด ๋ชฉ์ ์ง€์ธ์ง€ ํ™•์ธ!(์ฒ˜์Œ์—” ๊ทธ๋Ÿด๋ฆฌ ์—†์ง€๋งŒ!)
				while(nx >= 0 && nx < m && ny >= 0 && ny < n && maze[nx][ny] != 1) {//์กฐ๊ฑด์ด ์ฐธ์ผ ๋™์•ˆ KEEP ROLLING! ์ด ๋ฌธ์ œ์˜ ํ•ต์‹ฌ!
					nx += dir[0];
					ny += dir[1];
				}
				nx -= dir[0];
				ny -= dir[1];
				if(visited[nx][ny]) continue;//๋ฐฉ๋ฌธํ–ˆ๋˜ ๋…ธ๋“œ๋ผ๋ฉด ์•„๋ž˜ ๋™์ž‘ ์ œ์™ธ. 
				queue.offer(new int[] {nx,ny});//ํ์— ์ƒˆ๋กœ์šด ์ขŒํ‘œ๋ฅผ ๋„ฃ๊ณ  recursively proceed.
				visited[nx][ny] = true;
			}
		}
		return false;
	}
	

}

Last updated

Was this helpful?