본문 바로가기
카테고리 없음

dfs bfs

by why제곱 2023. 4. 17.
package Silver2;

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 Main_1260DFS와BFS {
	
	static int N;
	static int M;
	static int[][] adj;
	static boolean[] visited;
	public static void main(String[] args) throws IOException {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		
		st = new StringTokenizer(br.readLine());
		
		N = Integer.parseInt(st.nextToken());
		M = Integer.parseInt(st.nextToken());
		int V = Integer.parseInt(st.nextToken());
		adj = new int[N+1][N+1];
		visited = new boolean[N+1];
	
		
		for(int m=0; m<M; m++) {
			st = new StringTokenizer(br.readLine());
			int start = Integer.parseInt(st.nextToken());
			int end = Integer.parseInt(st.nextToken());
			
			adj[start][end] = adj[end][start] = 1;
		}
		
		DFS(V);
		System.out.println();
		BFS(V);
	}

	static void DFS(int v) {
		visited[v] = true;
		System.out.print(v+" ");
		
		for(int i=1; i<=N; i++) {
			if(!visited[i] && adj[v][i]==1) {
				DFS(i);
			}
		}
		
	}
	
	static void BFS(int v) {
		Queue<Integer> queue = new LinkedList<>();
		visited = new boolean[N+1];
		queue.add(v);
		visited[v] = true;
		
		while(!queue.isEmpty()) {
			int w = queue.poll();
			System.out.print(w+" ");
			for(int i=1; i<=N; i++) {
				if(!visited[i] && adj[w][i]==1) {
					queue.add(i);
					visited[i] = true;
				}
			}
		}
	}
}
package Silver2;

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

public class Main_4963섬의개수bfs {
	static int w;
	static int h;
	static int[][] land;
	static boolean[][] visited;
	static int cnt;

	static int[] dr = { -1, 1, 0, 0, -1, 1, -1, 1 };// 상 하 좌 우 좌상 좌하 우상 우하
	static int[] dc = { 0, 0, -1, 1, -1, -1, 1, 1 };

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;

		while (true) {
			cnt = 0;
			st = new StringTokenizer(br.readLine());
			w = Integer.parseInt(st.nextToken());
			h = Integer.parseInt(st.nextToken());
			if (w == 0 && h == 0)
				break;
			land = new int[h][w];
			visited = new boolean[h][w];
			for (int r = 0; r < h; r++) {
				st = new StringTokenizer(br.readLine());
				for (int c = 0; c < w; c++) {
					land[r][c] = Integer.parseInt(st.nextToken());
				}
			}



			for (int r = 0; r < h; r++) {
				for (int c = 0; c < w; c++) {
					if (!visited[r][c] && land[r][c] == 1) {
							cnt++;
							BFS(r, c);
						}
					}
				}

			System.out.println(cnt);
			}

		}
	static void BFS(int r, int c) {
		Queue<Integer[]> queue = new LinkedList<>();
		queue.add(new Integer[] {r,c});
		visited[r][c] = true;
		
		while(!queue.isEmpty()) {
			Integer[] out = queue.poll();
			for (int i = 0; i < 8; i++) {
				int du = out[0]+ dr[i];
				int dv = out[1] + dc[i];
				if (du >= 0 && du< h && dv >= 0 && dv <w) {
					if (!visited[du][dv] && land[du][dv]== 1) {
						visited[du][dv] = true;
						queue.add(new Integer[] {du, dv});
					}
				}

			}
		}
	} 
}
package Silver2;

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

public class Main_4963섬의개수dfs {
	static int w;
	static int h;
	static int[][] land;
	static boolean[][] visited;
	static int cnt;

	static int[] dr = { -1, 1, 0, 0, -1, 1, -1, 1 };// 상 하 좌 우 좌상 좌하 우상 우하
	static int[] dc = { 0, 0, -1, 1, -1, -1, 1, 1 };

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;

		while (true) {
			cnt = 0;
			st = new StringTokenizer(br.readLine());
			w = Integer.parseInt(st.nextToken());
			h = Integer.parseInt(st.nextToken());
			if (w == 0 && h == 0)
				break;
			land = new int[h][w];
			visited = new boolean[h][w];
			for (int r = 0; r < h; r++) {
				st = new StringTokenizer(br.readLine());
				for (int c = 0; c < w; c++) {
					land[r][c] = Integer.parseInt(st.nextToken());
				}
			}



			for (int r = 0; r < h; r++) {
				for (int c = 0; c < w; c++) {
					if (!visited[r][c] && land[r][c] == 1) {
						DFS(r, c);
							cnt++;
						}
					}
				}

			System.out.println(cnt);
			}

		}
	

	static void DFS(int r, int c) {
		visited[r][c] = true;
		
		for (int i = 0; i < 8; i++) {
			int du = r+ dr[i];
			int dv = c + dc[i];
			if (du >= 0 && du< h && dv >= 0 && dv< w) {
				if (!visited[du][dv] && land[du][dv]== 1)	DFS(du, dv);
			}

		}

	}


}