백준-1260. DFS와 BFS
https://www.acmicpc.net/problem/1260
DFS에는 크게 stack을 이용한 방법과 재귀함수를 이용한 방법이 있고,
BFS는 queue를 이용해서 풀이해야 한다.
DFS와 BFS의 정석을적인 방법을 이용해 풀이하면된다.
DFS는 재귀로 간단하게 해결하였다.
C++ 코드
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
vector<int> edge[1001];
bool check[1001];
void DFS(int n) { //재귀함수로 해결
if (check[n] == true) return;
check[n] = true;
cout << n << " ";
for (auto i : edge[n]) DFS(i);
}
void BFS(int n) { //큐를 이용
queue<int> q;
q.push(n);
check[n] = true;
while (q.empty() == false) {
int f = q.front();
cout << f << " ";
q.pop();
for (int i : edge[f]) {
if (check[i] == false) {
q.push(i); check[i] = true;
}
}
}
}
int main() {
int n = 0, m = 0, v = 0;
cin >> n >> m >> v;
int a, b;
while (m--) {
cin >> a >> b;
edge[a].push_back(b);
edge[b].push_back(a);
}
for (int i = 1; i <= n; i++) sort(edge[i].begin(), edge[i].end());
DFS(v);
for (int i = 1; i <= n; i++) check[i] = false;
cout << "\n";
BFS(v);
}
C# 코드
using System.Collections;
using System.Collections.Generic;
using static System.Console;
using System;
class Program
{
static ArrayList[] edge = new ArrayList[1001];
static bool[] check = new bool[1001];
static void DFS(int n)
{
if (check[n] == true) return;
check[n] = true;
Write($"{n} ");
foreach (int i in edge[n]) DFS(i);
}
static void BFS(int n)
{
Queue q = new Queue();
q.Enqueue(n);
check[n] = true;
while (q.Count != 0)
{
int f = (int)q.Dequeue();
Write($"{f} ");
foreach (int i in edge[f])
{
if (check[i] == false)
{
q.Enqueue(i);
check[i] = true;
}
}
}
}
public static void Main(string[] args)
{
for (int i = 0; i < 1001; i++) edge[i] = new ArrayList();
string[] s = ReadLine().Split(' ');
int N = int.Parse(s[0]), M = int.Parse(s[1]), v = int.Parse(s[2]);
while (M-- > 0)
{
s = ReadLine().Split(' ');
int a = int.Parse(s[0]);
int b = int.Parse(s[1]);
edge[a].Add(b);
edge[b].Add(a);
}
for (int i = 0; i <= N; i++) edge[i].Sort();
DFS(v);
for (int i = 0; i <= N; i++) check[i] = false;
Write("\n");
BFS(v);
}
}
java 코드
import java.io.*;
import java.util.*;
public class Main {
static ArrayList<Integer>[] edge = new ArrayList[1001];
static boolean[] check = new boolean[1001];
static void DFS(int n){
if(check[n] == true) return;
check[n] = true;
System.out.print(n + " ");
for(int i : edge[n]){
if(check[i] == false) DFS(i);
}
}
static void BFS(int n){
Queue<Integer> q = new LinkedList<>();
q.add(n);
check[n] = true;
while(q.isEmpty() == false){
int f = q.peek();
q.poll();
System.out.print(f + " ");
for(int i : edge[f]){
if(check[i] == false) {
q.add(i); check[i] = true;
}
}
}
}
public static void main(String[] args) {
for(int i = 0; i< 1001; i++) edge[i] = new ArrayList();
Scanner s = new Scanner(System.in);
int N = s.nextInt(); int M = s.nextInt(); int v = s.nextInt();
while(M-- > 0){
int a = s.nextInt();
int b = s.nextInt();
edge[a].add(b);
edge[b].add(a);
}
for(int i =0 ; i<= N; i++) Collections.sort(edge[i]);
DFS(v);
for(int i =0 ; i<= N; i++) check[i] = false;
System.out.println();
BFS(v);
}
}
kotlin 코드
import java.util.*
import kotlin.math.*
var edge = Array<ArrayList<Int>>(1001) {ArrayList<Int>()}
var check = BooleanArray(1001, {false})
fun DFS(n: Int){
if(check[n] == true) return
print("${n} ")
check[n] = true
for(i in edge[n]) DFS(i)
}
fun BFS(n: Int){
var q = LinkedList<Int>()
q.add(n)
check[n] = true
while(q.isEmpty() == false){
var f = q.peek()
q.poll()
print("${f} ")
for(i in edge[f]){
if(check[i] == false){
q.add(i)
check[i] = true
}
}
}
}
fun main(args: Array<String>){
val s = Scanner(System.`in`)
val N = s.nextInt()
var M = s.nextInt()
val v = s.nextInt()
while(M-- >0){
val a = s.nextInt()
val b = s.nextInt()
edge[a].add(b)
edge[b].add(a)
}
for(i in 0..N) edge[i].sort()
DFS(v)
for(i in 0..N) check[i] = false
println()
BFS(v)
}
python 코드
from collections import deque
edge = []
for i in range(1001): edge.append([])
check = [False for _ in range(1001)]
def DFS(n):
if check[n] == True: return
print(n, end=' ')
check[n] = True
for i in edge[n]:
DFS(i)
def BFS(n):
q = deque()
q.append(n)
check[n] = True
while len(q) > 0:
f = q.popleft()
print(f, end=' ')
for i in edge[f]:
if check[i] == False:
q.append(i)
check[i] = True
N,M,v = map(int, input().split())
for i in range(M):
a,b = map(int, input().split())
edge[a].append(b)
edge[b].append(a)
for i in range(N+1): edge[i].sort()
DFS(v)
print()
for i in range(N+1): check[i] = False
BFS(v)