The mystery of the path of the 7th Blue Bridge Cup National Championship

Topic description

Xiao Ming pretends to be a knight from Planet X and enters a strange castle.
There is nothing in the castle, only the ground paved with square stones.

Suppose the castle floor is nxn squares. [As shown in Figure 1.png].

According to custom, the knight should walk from the northwest corner to the southeast corner.
Can move horizontally or vertically, but cannot walk diagonally or jump.
Every time you reach a new square, you have to shoot one arrow to the north and one to the west.
(There are n targets in each of the west and north walls of the castle)


The same square is only allowed to pass once. But you don't have to do all the squares.

Can you infer the knight's path given only the number of arrows on the target?

Sometimes it's OK, like the example in Figure 1.png.

The requirement of this question is to know the number of the arrow target and find the walking path of the knight (the test data ensures that the path is unique)

Input:
The first line is an integer N (0<N<20), indicating that there are N x N squares on the ground.
The second line is N integers, separated by spaces, indicating the number on the arrow target on the north side (from west to east)
The third Line N integers, separated by spaces, indicating the numbers on the target on the west side (from north to south)

Output:
a line of integers representing the knight's path.

For the convenience of representation, we agree that each small grid is represented by a number, numbered from the northwest corner: 0, 1, 2, 3....
For example, the number of the squares in Figure 1.png is:

0  1  2  3
4  5  6  7
8  9  10 11
12 13 14 15


Example:
User input:
4
2 4 3 4
4 3 3 3

The program should output:
0 4 5 1 2 3 7 11 10 9 13 14 15

Resource convention:
peak memory consumption < 256M
CPU consumption < 1000ms


Please output strictly according to the requirements, and do not superficially print superfluous content like: "Please enter...".

All code is placed in the same source file, after debugging, copy and submit the source code.
Note: Do not use the package statement. Do not use features of jdk1.7 and above.
Note: The name of the main class must be: Main, otherwise it will be processed as invalid code.

// 常规的深搜+剪枝
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
	// 下一步的方向
	static int[][] dir = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };
	// 结果
	static ArrayList<Integer> list = new ArrayList<Integer>();

	static int N;
	// 记录北边的箭靶
	static int[] north;
	// 记录西边的箭靶
	static int[] west;
	// 记录走过的方格
	static byte[] visit;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		N = sc.nextInt();
		north = new int[N];
		west = new int[N];

		for (int i = 0; i < N; i++) {
			north[i] = sc.nextInt();
		}
		for (int i = 0; i < N; i++) {
			west[i] = sc.nextInt();
		}

		// visit[i]为0,表示未被走过;visit[i]为1,表示已经走过。
		visit = new byte[N * N];

		dfs(0, 0);

		sc.close();
	}

	static void dfs(int x, int y) {
		int index = x * N + y;
		visit[index] = 1;
		north[y]--;
		west[x]--;
		list.add(index);

		if (index == N * N - 1) {
			for (int i = 0; i < N; i++) {
				if (north[i] > 0 || west[i] > 0)
					return;
			}
			for (int i = 0; i < list.size(); i++) {
				System.out.print(list.get(i) + " ");
			}
			return;
		}

		for (int i = 0; i < dir.length; i++) {
			int nextx = x + dir[i][0];
			int nexty = y + dir[i][1];
			if (nextx < 0 || nextx >= N || nexty < 0 || nexty >= N)
				continue;
			if (north[nexty] <= 0 || west[nextx] <= 0)
				continue;
			dfs(nextx, nexty);
			list.remove(list.size() - 1);
			north[nexty]++;
			west[nextx]++;
			visit[nextx * N + y] = 0;
		}
	}
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326070759&siteId=291194637