美文网首页半栈工程师
HDU 1241 Oil Deposits 深搜(DFS)

HDU 1241 Oil Deposits 深搜(DFS)

作者: TinyDolphin | 来源:发表于2017-12-12 16:03 被阅读0次

题意解析:给你一个地图,找出所有不相连(八个方向)的 @ 组合有多少个?

深度优先遍历图 VS 广度优先遍历图

深度优先遍历图 VS 广度优先遍历图.gif
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;

public class Main {

    private static final int MAXSIZE = 105;

    private static char rooms[][] = new char[MAXSIZE][MAXSIZE];
    private static boolean pass[][] = new boolean[MAXSIZE][MAXSIZE];

    // 注意:本题需要向八个方向搜索
    public static void DFS(int indexI, int indexJ) {
        if (pass[indexI][indexJ]) {
            return;
        }
        pass[indexI][indexJ] = true;
        // 上
        if (rooms[indexI - 1][indexJ] != '*') {
            DFS(indexI - 1, indexJ);
        }
        // 下
        if (rooms[indexI + 1][indexJ] != '*') {
            DFS(indexI + 1, indexJ);
        }
        // 左
        if (rooms[indexI][indexJ - 1] != '*') {
            DFS(indexI, indexJ - 1);
        }
        // 右
        if (rooms[indexI][indexJ + 1] != '*') {
            DFS(indexI, indexJ + 1);
        }
        // 右上
        if (rooms[indexI + 1][indexJ + 1] != '*') {
            DFS(indexI + 1, indexJ + 1);
        }
        // 右下
        if (rooms[indexI + 1][indexJ - 1] != '*') {
            DFS(indexI + 1, indexJ - 1);
        }
        // 左上
        if (rooms[indexI - 1][indexJ + 1] != '*') {
            DFS(indexI - 1, indexJ + 1);
        }
        // 左下
        if (rooms[indexI - 1][indexJ - 1] != '*') {
            DFS(indexI - 1, indexJ - 1);
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        int inputN;
        int inputM;
        String inputStr;
        char temp;
        int roomsCount;
        while (in.hasNext()) {
            inputM = in.nextInt();
            inputN = in.nextInt();
            if (inputM == 0 && inputN == 0) {
                break;
            }
            // 初始化,因为没墙包着,所以四周需要加墙
            for (int index = 0; index < MAXSIZE; index++) {
                Arrays.fill(rooms[index], '*');
                Arrays.fill(pass[index], false);
            }
            // 将输入的字符串转换为字符数组存储起来
            for (int indexI = 1; indexI <= inputM; indexI++) {
                inputStr = in.next();
                for (int indexJ = 1; indexJ <= inputN; indexJ++) {
                    temp = inputStr.charAt(indexJ - 1);
                    rooms[indexI][indexJ] = temp;
                }
            }
            // 初始化油田的数量
            roomsCount = 0;
            for (int indexI = 1; indexI <= inputM; indexI++) {
                for (int indexJ = 1; indexJ <= inputN; indexJ++) {
                    if (!pass[indexI][indexJ] && rooms[indexI][indexJ] == '@') {
                        DFS(indexI, indexJ);    // 一个顶点起,进行深度优先遍历
                        roomsCount++;           // 四面八方都走完,无路可走时,+1
                    }
                }
            }
            out.println(roomsCount);
        }
        out.flush();
    }
}

相关文章

网友评论

    本文标题:HDU 1241 Oil Deposits 深搜(DFS)

    本文链接:https://www.haomeiwen.com/subject/pvdnixtx.html