一、題目描述
馬在中國象棋以日字形規則移動。請編寫一段程序,給定 n*m 大小的棋盤,以及馬的初始位置 (x,y),要求不能重複經過棋盤上的同一個點,計算馬可以有多少途徑遍歷棋盤上的所有點。
輸入
- 第一行爲整數T(T < 10),表示測試數據組數。
- 每一組測試數據包含一行,爲四個整數,分別爲棋盤的大小以及初始位置座標n,m,x,y。(0<=x<=n-1,0<=y<=m-1, m < 10, n < 10)
輸出
- 每組測試數據包含一行,爲一個整數,表示馬能遍歷棋盤的途徑總數,0爲無法遍歷一次。
樣例輸入
1
5 4 0 0
樣例輸出
32
二、題解
方法一:dfs + 回溯
- 在 dfs 中加入 step,每次進入 dfs 時先判斷當前 step 是否等於 R × C。
- 是,則 count++
- 否則,繼續枚舉 8 個方向。
- 注意狀態回溯。
import java.util.*;
import java.math.*;
import java.io.*;
public class Main{
static int count, R, C;
static boolean[][] vis = new boolean[10][10];
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(new BufferedInputStream(System.in));
BufferedWriter w = new BufferedWriter(new OutputStreamWriter(System.out));
int T = sc.nextInt();
while (T-- > 0) {
R = sc.nextInt();
C = sc.nextInt();
int x = sc.nextInt();
int y = sc.nextInt();
vis[x][y] = true;
dfs(x, y, 1);
System.out.println(count);
count = 0;
}
}
private static int[][] dir = {{1,2},{-1,2},{1,-2},{-1,-2},{2,1},{2,-1},{-2,1},{-2,-1}};
static void dfs(int x, int y, int step) {
if (step == R * C) {
count++;
return;
}
for (int k = 0; k < 8; k++) {
int tx = x + dir[k][0];
int ty = y + dir[k][1];
if (!inArea(tx, ty) || vis[tx][ty])
continue;
vis[tx][ty] = true;
dfs(tx, ty, step+1);
vis[tx][ty] = false;
}
}
static boolean inArea(int x, int y) {
return x >= 0 && x < R && y >= 0 && y < C;
}
}
複雜度分析
- 時間複雜度:,
- 空間複雜度:,