새발블로그

암시적 그래프 bfs 템플릿 본문

Problem Solving/Refer

암시적 그래프 bfs 템플릿

EUG 2025. 7. 4. 17:32
int[][] grid = {
    {1, 1, 1, 1},
    {0, 1, 0, 1},
    {0, 1, 0, 1},
    {1, 0, 1, 1}
};
import java.util.*;
                    
public class ImplicitGraphBFS {
		static boolean[][] visited;
		static int[][] grid;
		static int[] dr = {0, 1, 1, 1, 0, -1, -1, -1};
		static int[] dc = {1, 1, 0, -1, -1, -1, 0, 1};
		static int rowLength;
		static int colLength;
		
		public static boolean isValid(int r, int c) {
		    return 0 <= r && r < rowLength && 0 <= c && c < colLength;
		}
		
		public static void main(String[] args) {
		    grid = new int[][] {
				    {1, 1, 1, 1},
				    {0, 1, 0, 1},
				    {0, 1, 0, 1},
				    {1, 0, 1, 1}
				};
		    rowLength = grid.length;
		    colLength = grid[0].length;
		    visited = new boolean[rowLength][colLength];
		    bfs(0, 0);
		}
		
		public static void bfs(int r, int c) {
		    Queue<int[]> queue = new LinkedList<>();
		    queue.offer(new int[]{r, c});
		    visited[r][c] = true;
		
				// queue가 비어있을 때까지 반복
		    while (!queue.isEmpty()) {
				    // 현재 노드 방문
		        int[] cur = queue.poll();
		        int curRow = cur[0];
		        int curCol = cur[1];
		
						// 다음 노드 방문
		        for (int i = 0; i < 8; i++) {
		            int nextRow = curRow + dr[i];
		            int nextCol = curCol + dc[i];
		            if (isValid(nextRow, nextCol)) {
		                if (!visited[nextRow][nextCol]) {
		                    queue.offer(new int[]{nextRow, nextCol});
												visited[nextRow][nextCol] = true;
		                }
		            }
		        }
		    }
		}
}

'Problem Solving > Refer' 카테고리의 다른 글

DFS와 BFS를 활용한 네트워크(연결된 그룹) 개수 구하기  (0) 2025.07.04
암시적 그래프 dfs 템플릿  (0) 2025.07.04
디버깅 Tip  (0) 2025.07.04
연결된 영역 개수 찾는 유형  (0) 2025.07.04
BFS 템플릿  (0) 2025.07.04