路径之谜
小明冒充X星球的骑士,进入了一个奇怪的城堡。
城堡里边什么都没有,只有方形石头铺成的地面。
假设城堡地面是 n x n 个方格。【如图1.png】所示。
按习俗,骑士要从西北角走到东南角。
可以横向或纵向移动,但不能斜着走,也不能跳跃。
每走到一个新方格,就要向正北方和正西方各射一箭。
(城堡的西墙和北墙内各有 n 个靶子)
同一个方格只允许经过一次。但不必做完所有的方格。
如果只给出靶子上箭的数目,你能推断出骑士的行走路线吗?
有时是可以的,比如图1.png中的例子。
本题的要求就是已知箭靶数字,求骑士的行走路径(测试数据保证路径唯一)
输入:
第一行一个整数N(0
package lq.lq2016;
import java.util.Scanner;
/*
*
*
*/
/*20 20
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 20
20 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
4 4
1 1 1 4
4 1 1 1
*/
public class RoadMistery {
static int N, tot;
static int maxn = 25;
static int[] top = new int[maxn];
static int[] left = new int[maxn];
static int[] top2 = new int[maxn];
static int[] left2 = new int[maxn];
static int[] dx = { -1, 1, 0, 0 };
static int[] dy = { 0, 0, -1, 1 };
static int[] ans = new int[400];
static int vis[][] = new int[25][25];
static void dfs(int x, int y, int ind) {// 当前位于(x,y)点,搜索第ind个点
if (ind == tot - 1 && x == N - 1 && y == N - 1) {
boolean flag = true;
for (int i = 0; i < N; i++)
if (top2[i] != top[i]) {
return;
}
for (int i = 0; i < N; i++)
if (left2[i] != left[i]) {
return;
}
System.out.print(0 + " ");
for (int i = 0; i < tot - 1; i++) {
System.out.print(ans[i] + " ");
}
return;
}
for (int i = 0; i < 4; i++) {
int tx = x + dx[i], ty = y + dy[i];
if (tx >= 0 && tx < N && ty >= 0 && ty < N && vis[tx][ty] == 0) {
top2[ty]++;
left2[tx]++;
if (top2[ty] > top[ty] || left2[tx] > left[tx]) {// 两个条件不可分开continue,会造成
// vis[tx][ty]=0;
top2[ty]--;
left2[tx]--;
continue;
}
vis[tx][ty] = 1;
ans[ind] = tx * N + ty;
dfs(tx, ty, ind + 1);
vis[tx][ty] = 0;
// ans[ind]=tx*N+ty;
top2[ty]--;
left2[tx]--;
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
for (int i = 0; i < N; i++) {
top[i] = sc.nextInt();
tot += top[i];
}
for (int i = 0; i < N; i++)
left[i] = sc.nextInt();
top2[0] = 1;
left2[0] = 1;
vis[0][0] = 1;
dfs(0, 0, 0);
}
}
在一个由方形石头铺成的城堡中,一名冒充的骑士必须从西北角走到东南角,每到一个新方格,就向正北方和正西方各射一箭。已知箭靶上的箭数,如何确定骑士的行走路线?本文介绍了一个通过编程求解此问题的方法。

被折叠的 条评论
为什么被折叠?



