2021寒假每日一題《紅與黑》

紅與黑

題目來源:《信息學奧賽一本通》
時間限制:1000ms 內存限制:64mbjava

題目描述

有一間長方形的房子,地上鋪了紅色、黑色兩種顏色的正方形瓷磚。
你站在其中一塊黑色的瓷磚上,只能向相鄰(上下左右四個方向)的黑色瓷磚移動。
請寫一個程序,計算你總共可以到達多少塊黑色的瓷磚。shell

輸入格式

輸入包括多個數據集合。
每一個數據集合的第一行是兩個整數 \(W\) 和 \(H\),分別表示 \(x\) 方向和 \(y\) 方向瓷磚的數量。
在接下來的 \(H\) 行中,每行包括 \(W\) 個字符。每一個字符表示一塊瓷磚的顏色,規則以下
1)‘.’:黑色的瓷磚;
2)‘#’:紅色的瓷磚;
3)‘@’:黑色的瓷磚,而且你站在這塊瓷磚上。該字符在每一個數據集合中惟一出現一次。
當在一行中讀入的是兩個零時,表示輸入結束。ide

輸出格式

對每一個數據集合,分別輸出一行,顯示你從初始位置出發能到達的瓷磚數(記數時包括初始位置的瓷磚)。this

數據範圍

\(1 ≤ W,H ≤ 20\)code

樣例輸入

6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
0 0

樣例輸出

45

解題思路1:BFS(廣度優先搜索)

先將初始座標加入隊列。
而後,遍歷當前格子的上下左右四個格子,若是能找到'.',則將他的座標加入隊列。
而後依次作下去,每走到一個新的格子,計數+1
直到隊列爲空,也就完成了全部遍歷。
計數的值就是題解。遞歸

在Java中,LinkedList類實現了Queue接口,所以咱們能夠把LinkedList當成Queue來用。
其中,add()和remove()方法在失敗的時候會拋出異常,而offer()和poll()不會,因此這裏使用offer()和poll()。接口

解題代碼1-Java

import java.util.*;

class pair {
    int x, y;

    public pair(int sx, int sy) {
        this.x = sx;
        this.y = sy;
    }
}

public class Main {
    public static int N = 30;
    public static int h, w;
    public static char[][] g = new char[N][N];
    public static int[] dx = {-1, 0, 1, 0};
    public static int[] dy = {0, 1, 0, -1};

    static int bfs(int sx, int sy) {
        pair p = new pair(sx, sy);
        Queue<pair> q = new LinkedList<>();
        q.offer(p);
        g[sx][sy] = '#';
        int res = 0;
        while (!q.isEmpty()) {
            pair t = q.poll();
            res++;
            for (int i = 0; i < 4; i++) {
                int x = t.x + dx[i];
                int y = t.y + dy[i];
                if (x < 0 || x >= h || y < 0 || y >= w || g[x][y] != '.') {
                    continue;
                }
                g[x][y] = '#';
                q.offer(new pair(x, y));
            }
        }
        return res;
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        while (true) {
            String[] ts = input.nextLine().split(" ");
            w = Integer.parseInt(ts[0]);
            h = Integer.parseInt(ts[1]);
            if (w == 0 || h == 0) {
                break;
            }
            int x = 0, y = 0;
            for (int i = 0; i < h; i++) {
                String str = input.nextLine();
                for (int j = 0; j < w; j++) {
                    g[i][j] = str.charAt(j);
                    if (g[i][j] == '@') {
                        x = i;
                        y = j;
                    }
                }
            }
            System.out.println(bfs(x, y));
        }
        input.close();
    }
}

解題思路2:DFS(深度優先搜索)

深度優先搜索,因爲有不少層遞歸,有可能爆棧。
雖然DFS代碼更加簡單,但仍是建議使用BFS解決此題。隊列

遍歷當前座標的上下左右四個格子,若是是'.',則當即遍歷找到的'.'的格子的上下左右,如此進行遞歸。
每層遞歸返回找到的'.'的數量。
最後獲得的數量即爲題解。內存

解題代碼2-Java

import java.util.*;

public class Main {
    public static int N = 30;
    public static int h, w;
    public static char[][] g = new char[N][N];
    public static int[] dx = {-1, 0, 1, 0};
    public static int[] dy = {0, 1, 0, -1};

    static int dfs(int sx, int sy) {
        int res = 1;
        g[sx][sy] = '#';
        for (int i = 0; i < 4; i++) {
            int x = sx + dx[i];
            int y = sy + dy[i];
            if (x >= 0 && x < h && y >= 0 && y < w && g[x][y] == '.') {
                res += dfs(x, y);
            }
        }
        return res;
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        while (true) {
            String[] ts = input.nextLine().split(" ");
            w = Integer.parseInt(ts[0]);
            h = Integer.parseInt(ts[1]);
            if (w == 0 || h == 0) {
                break;
            }
            int x = 0, y = 0;
            for (int i = 0; i < h; i++) {
                String str = input.nextLine();
                for (int j = 0; j < w; j++) {
                    g[i][j] = str.charAt(j);
                    if (g[i][j] == '@') {
                        x = i;
                        y = j;
                    }
                }
            }
            System.out.println(dfs(x, y));
        }
        input.close();
    }
}
相關文章
相關標籤/搜索