紅與黑
題目來源:《資訊學奧賽一本通》
時間限制:1000ms 記憶體限制:64mb
題目描述
有一間長方形的房子,地上鋪了紅色、黑色兩種顏色的正方形瓷磚,
你站在其中一塊黑色的瓷磚上,只能向相鄰(上下左右四個方向)的黑色瓷磚移動,
請寫一個程式,計算你總共能夠到達多少塊黑色的瓷磚,
輸入格式
輸入包括多個資料集合,
每個資料集合的第一行是兩個整數 \(W\) 和 \(H\),分別表示 \(x\) 方向和 \(y\) 方向瓷磚的數量,
在接下來的 \(H\) 行中,每行包括 \(W\) 個字符,每個字符表示一塊瓷磚的顏色,規則如下
1)‘.’:黑色的瓷磚;
2)‘#’:紅色的瓷磚;
3)‘@’:黑色的瓷磚,并且你站在這塊瓷磚上,該字符在每個資料集合中唯一出現一次,
當在一行中讀入的是兩個零時,表示輸入結束,
輸出格式
對每個資料集合,分別輸出一行,顯示你從初始位置出發能到達的瓷磚數(記數時包括初始位置的瓷磚),
資料范圍
\(1 ≤ W,H ≤ 20\)
樣例輸入
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();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/251495.html
標籤:其他
上一篇:2021寒假每日一題《字母圖形》
下一篇:紅黑樹添加洗掉
