1. 程式人生 > >搜素演算法(基礎)--DFS/BFS演算法(JAVA)

搜素演算法(基礎)--DFS/BFS演算法(JAVA)

為了便於理解這裡的資料是一個無向圖,要求輸出遍歷順序

下面只給出用例和演算法,之後可以根據後面的三個題目進行深入學習
Input:
5 5
1 2
1 3
1 5
2 4
3 5
Output:
1 2 4 3 5

DFS

import java.util.Scanner;

public class DFS {
    static int[][] e = new int[100][100];
    static int[] book = new int[100];
    static int n, m;
    static int sum = 0;
    static Scanner input = new
Scanner(System.in); public static void main(String[] args) { n = input.nextInt(); m = input.nextInt(); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (i == j) { e[i][j] = 0; } else { e[i][j] = 99999999
; } } } for (int i = 1; i <= m; i++) { int a = input.nextInt(); int b = input.nextInt(); e[a][b] = 1; e[b][a] = 1; } book[1] = 1; dfs(1); } public static void dfs(int cur) { System.out
.print(cur + " "); sum++; if (sum == n) { return; } for (int i = 1; i <= n; i++) { if (e[cur][i] == 1 && book[i] == 0) { book[i] = 1; dfs(i); } } return; } }

BFS


import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class BFS {
    static int[][] e = new int[100][100];
    static int[] book = new int[100];
    static int n, m;
    static Queue<Integer> queue = new LinkedList<>();
    static Scanner input = new Scanner(System.in);
    public static void main(String[] args) {
        int a, b;
        n = input.nextInt();
        m = input.nextInt();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (i == j) {
                    e[i][j] = 0;
                } else {
                    e[i][j] = 99999999;
                }
            }
        }
        for (int i = 1; i <= m; i++) {
            a = input.nextInt();
            b = input.nextInt();
            e[a][b] = 1;
            e[b][a] = 1;
        }
        queue.offer(1);
        book[1] = 1;
        bfs();

    }
    public static void bfs() {
        while (!queue.isEmpty()) {
            int cur = queue.peek();
            for (int i = 1; i <= n; i++) {
                if (e[cur][i] == 1 && book[i] == 0) {
                    book[i] = 1;
                    queue.offer(i);
                }
            }
            System.out.print(queue.remove() + " ");
        }
        return;
    }
}

下面是一個全排列的簡單題目,也用到了dfs,屬於偏簡單的一道題目,用來給後面的題目做鋪墊

問題描述:假設有編號為1、2、3的三張卡片和編號1、2、3的三個盒子,現需將這三張撲克牌分別放到三個盒子裡面,並且每個盒子只能放一張撲克牌,請問一共有幾種不同的方法?
Output:
123 132 213 231 312 321

import java.util.Scanner;
/**
 * DFS演算法基本模型:
 * void dfs(int step) {
 *     判斷邊界
 *     嘗試每一種可能 for {
 *         繼續下一步def(step + 1)
 *     }
 *     返回
 * }
 * */

public class DFS {
    static int n;
    static int[] book = new int[10];
    static int[] a = new  int[10];
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        n = input.nextInt();
        dfs(1);
    }

    public static void dfs(int step) {
        if (step == n + 1) {
            /**
             * 列印,並返回
             * */
            for (int i = 1; i <= n; i++) {
                System.out.print(a[i] + " ");
            }
            System.out.println();
            return;
        }
        for (int i = 0; i < n; i++) {
            /**
             * 標記陣列book,用來檢測盒子中是否已經放入
             * */
            if (book[i] == 0) {
                /**
                 * 將i號撲克牌放入到第step個盒子中
                 * */
                a[step] = i;
                book[i] = 1;
                /**
                 * 處理step+1個小盒子,注意要記得將剛才嘗試的撲克牌收回
                 * */
                dfs(step + 1);
                book[i] = 0;
            }
        }
        return;
    }

}