L


私信TA

用户名:1302759659

访问量:2151

签 名:

等  级
排  名 854
经  验 3476
参赛次数 0
文章发表 13
年  龄 17
在职情况 学生
学  校 广西英华国际职业学院
专  业 软件技术

  自我简介:

TA的其他文章

解题思路: 直接DFS爆搜得了 判断好边界 注意回溯

参考代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;

public class Main {
    static int n, m, x, y;
    static int dx[] = {1, 1, -2, -1, -2, -1, 2, 2};
    static int dy[] = {2, -2, 1, 2, -1, -2, 1, -1};
    static boolean f[][];

    public static void main(String[] args) throws IOException {
        int t = sc.nextInt();
        while (t-- > 0) {
            n = sc.nextInt();
            m = sc.nextInt();
            f = new boolean[15][15];
            x = sc.nextInt();
            y = sc.nextInt();
            max = 0;
            f[x][y] = true;
            dfs(x, y, 1);
            System.out.println(max);
        }
    }
    static int max = 0;
    static void dfs(int x, int y, int sum) {
        if (sum == n * m) {
            max++;
            return;
        }
        for (int i = 0; i < 8; i++) {
            int xx = x + dx[i];
            int yy = y + dy[i];
            if (xx < 0 || xx >= n || yy < 0 || yy >= m || f[xx][yy]) {
                continue;
            }
            f[xx][yy] = true;
            dfs(xx, yy, sum + 1);
            f[xx][yy] = false;
        }

    }
    //快读模板
    static class sc {
        private static StreamTokenizer input = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

        public static int nextInt() throws IOException {
            input.nextToken();
            return (int) input.nval;
        }

        public static String next() throws IOException {
            input.nextToken();
            return input.sval;
        }

        public static double nextDouble() throws IOException {
            input.nextToken();
            return input.nval;
        }
    }
}


 

0.0分

1 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区