The Mystery of the Final Path of the 7th Blue Bridge Cup Java Programming Undergraduate Group B in 2016 (Programming Question)

2016 7th Blue Bridge Cup Java Programming Undergraduate Group B Final Individual Problem Solution Summary:

https://blog.csdn.net/daixinliangwyx/article/details/90169154

 

Question 4

Title: Path Mystery

Test address: https://www.docpp.com/oj/problem1834.html

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 each in the west and north walls of the castle) The

same square is only allowed to pass once. But you don't have to go through 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.


Solution: Because the range of n is very small, it is a simple DFS search.

Code:

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;

public class Main {
    public static InputReader in = new InputReader(new BufferedInputStream(System.in));
    public static PrintWriter out = new PrintWriter(System.out);
    public static int n, flag;
    public static int[][] vis = new int[25][25];
    public static int[] ans = new int[410];
    public static int[] a = new int[25];
    public static int[] b = new int[25];
    public static int[] ta = new int[25];
    public static int[] tb = new int[25];
    public static int[][] dir = {
   
   {0, 1}, {0, -1}, {1, 0}, {-1, 0}};

    public static void main(String[] args) {
        n = in.nextInt();
        for (int i = 0; i < n; i++)
            a[i] = in.nextInt();
        for (int i = 0; i < n; i++)
            b[i] = in.nextInt();
        flag = 0;
        ans[0] = 0;
        ta[0]++;
        tb[0]++;
        vis[0][0] = 1;
        dfs(0, 0, 1);
        out.close();
    }

    static void dfs(int x, int y, int k) {
        if (flag == 1) return;
        if (x == n-1 && y == n-1) {
            int f = 0;
            for (int i = 0; i < n; i++) {
                if (ta[i] != a[i] || tb[i] != b[i]) {
                    f = 1;
                    break;
                }
            }
            if (f == 0) {
                for (int i = 0; i < k; i++) {
                    out.print(ans[i] + " ");
                    out.flush();
                }
                flag = 1;
            }
            return;
        }
        for (int i = 0; i < 4; i++) {
            int nextx = x + dir[i][0];
            int nexty = y + dir[i][1];
            if (nextx >= 0 && nextx < n && nexty >= 0 && nexty < n && vis[nextx][nexty] == 0 && ta[nexty] < a[nexty] && tb[nextx] < b[nextx]) {
                vis[nextx][nexty] = 1;
                ta[nexty]++;
                tb[nextx]++;
                ans[k] = nextx * n + nexty;
                dfs(nextx, nexty, k+1);
                ta[nexty]--;
                tb[nextx]--;
                vis[nextx][nexty] = 0;
            }
        }
    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public String nextLine() {
            String str = null;
            try {
                str = reader.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public Double nextDouble() {
            return Double.parseDouble(next());
        }

        public BigInteger nextBigInteger() {
            return new BigInteger(next());
        }

        public BigDecimal nextBigDecimal() {
            return new BigDecimal(next());
        }

    }
}

 

Guess you like

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