Java 中的搜索算法

例题:HDU - 1312 Red and Black

题意 : 在地图中,'@' 是起点, '#' 是障碍物, '.'是可走的格子,问,从起点出发, 一共有多少个格子可以走。

这是经典的搜索入门题目。利用深度优先算法和宽度优先算法都可以解决。

下面用Java语言来分别进行两种搜索算法的实现。

DFS算法:

import java.util.*;
import java.math.*;
import java.io.*;

public class Main{
    static int[][] dir = {{0, 0, 1, -1} , {1, -1, 0, 0}};
    static int n = 0, m = 0, u = 0, v = 0, num = 0;
    static char[][] mp = new char[20][20];
    static boolean[][] vis = new boolean[20][20];
    static void dfs(int x, int y){
        if(x < 0 || x >= n || y < 0 || y >= m) return;
        if(mp[x][y] == '#' || vis[x][y] == true) return;
        num ++; vis[x][y] = true;
        for(int i = 0; i < 4; i++){
            int dx = x + dir[0][i];
            int dy = y + dir[1][i];
            dfs(dx, dy);
        }
        return;
    }
    public static void main(String[] args){
        Scanner cin = new Scanner(System.in);
        while(cin.hasNext()){
            m = cin.nextInt();
            n = cin.nextInt();
            num = 0;
            for(int i = 0; i < 20; i++){
                for(int j = 0; j < 20; j++){
                    mp[i][j] = '\0';
                    vis[i][j] = false;
                }
            }
            if(m == 0 && n == 0) return;
            for(int i = 0; i < n; i++){
                String s = cin.next();
                for(int j = 0; j < m; j++){
                    mp[i][j] = s.charAt(j);
                    if(mp[i][j] == '@'){
                        u = i; v = j;
                    }
                }
            }
            dfs(u, v);
            System.out.println(num);
        }
    }
}

 

BFS算法:

import java.io.*;
import java.math.*;
import java.util.*;

class node{
    int x, y;
}
public class Main {
    static int n, m, num, sx, sy;
    static char[][] mp = new char[20][20];
    static boolean[][] vis = new boolean[20][20];
    static int[][] dir = {{1, -1, 0, 0}, {0, 0, 1, -1}};
    static void BFS(){
        Queue Q = new LinkedList();
        node u = new node();
        u.x = sx; u.y = sy;
        Q.offer(u);
        vis[sx][sy] = true;
        while (Q.size() > 0){
            node top = Q.poll();
            num ++;
            for(int i = 0; i < 4; i++){
                int dx = top.x + dir[0][i];
                int dy = top.y + dir[1][i];
                if(dx >= 0 && dx < n && dy >= 0 && dy < m && mp[dx][dy] == '.' && !vis[dx][dy]){
                    vis[dx][dy] = true;
                    node v = new node();
                    v.x = dx;
                    v.y = dy;
                    Q.offer(v);
                }
            }
        }
    }
    public static void main(String[] argv){
        Scanner cin = new Scanner(System.in);
        while(cin.hasNext()){
            num = 0; sx = 0; sy = 0;
            for(int i = 0; i < 20; i++){
                for(int j = 0; j < 20; j++){
                    mp[i][j] = '\0';
                    vis[i][j] = false;
                }
            }
            m = cin.nextInt();
            n = cin.nextInt();
            if(m == 0 && n == 0) return;
            String s;
            for(int i = 0; i < n; i++){
                s = cin.next();
                for(int j = 0; j < m; j++){
                    mp[i][j] = s.charAt(j);
                    if(mp[i][j] == '@'){
                        sx = i; sy = j;
                    }
                }
            }
            BFS();
            System.out.println(num);
        }
    }
}

 

你可能感兴趣的:(Java,学习)