路径之谜
小明冒充X星球的骑士,进入了一个奇怪的城堡。
城堡里边什么都没有,只有方形石头铺成的地面。
假设城堡地面是 n x n 个方格。【如图1.png】所示。
按习俗,骑士要从西北角走到东南角。
可以横向或纵向移动,但不能斜着走,也不能跳跃。
每走到一个新方格,就要向正北方和正西方各射一箭。
(城堡的西墙和北墙内各有 n 个靶子)
同一个方格只允许经过一次。但不必做完所有的方格。
如果只给出靶子上箭的数目,你能推断出骑士的行走路线吗?
有时是可以的,比如图的例子。
本题的要求就是已知箭靶数字,求骑士的行走路径(测试数据保证路径唯一)
输入:
第一行一个整数N(0<N<20),表示地面有 N x N 个方格
第二行N个整数,空格分开,表示北边的箭靶上的数字(自西向东)
第三行N个整数,空格分开,表示西边的箭靶上的数字(自北向南)
输出:
一行若干个整数,表示骑士路径。
为了方便表示,我们约定每个小格子用一个数字代表,从西北角开始编号: 0,1,2,3....
比如,图1.png中的方块编号为:
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
示例:
用户输入:
4
2 4 3 4
4 3 3 3
程序应该输出:
0 4 5 1 2 3 7 11 10 9 13 14 15
资源约定:
峰值内存消耗 < 256M
CPU消耗 < 1000ms
import java.util.Scanner;
public class 路径之谜 {
static int dy[]={0,0,1,-1};
static int dx[] ={1,-1,0,0};
static Scanner sc = new Scanner(System.in);
static int N =sc.nextInt();
//对应的方块的块编号应该就是N*x+y
static boolean vd[][] =new boolean[N][N];//判断有无重复
static int st[] = new int [N*N];//是否要选取这一点当做路径点
static boolean flag = false;
public static void main(String[] args) {
//对应的地图
int map[][] = new int [N][N];
//北边
int north[] = new int[N];
for (int i = 0; i < N; i++) {
north[i] = sc.nextInt();
}
//西边
int west[] = new int[N];
for (int i = 0; i < N; i++) {
west[i] = sc.nextInt();
}
north[0]--;
west[0]--;
vd[0][0] = true;
System.out.printf("0 ");//起点
dfs(0,0,0,north,west);
for (int i = 0; i <N*N ; i++) {
if(st[i]!=0){
System.out.printf("%d ",st[i]);
}
}
}
static void dfs(int x,int y,int k,int north[],int west[] ){
if(north[y]<0||west[x]<0||flag) return;
st[k] = x*N+y;
if(x==N-1&&y==N-1&&check(north,west)){
flag =true;
return;
}
for (int i = 0; i < 4; i++) {
int newx = x+dx[i];
int newy = y+dy[i];
if(newx>=0&&newx<N&&newy>=0&&newy<N&&!vd[newx][newy]){
vd[newx][newy] = true;
north[newy]--;
west[newx]--;
dfs(newx,newy,k+1,north,west);
north[newy]++;
west[newx]++;
vd[newx][newy] =false;//回溯
}
}
}
//判断是不是用完了所有的箭靶
static boolean check(int north[],int west[]){
for (int i = 0; i < N; i++) {
if(north[i]!=0||west[i]!=0){
return false;
}
}
return true;
}
}