「洛谷」P1406 方格填数

P1406 方格填数

https://www.luogu.com.cn/problem/P1406

题目描述

给一个n 的方格矩阵,还有 n*n个整数,让你将这些整数填入矩阵,使得每行每列每个对角线上整数的和都相等。下面给出几个例子:

在这里插入图片描述

输入描述

第一行一个整数n.(1<=n<=4)

第二行n*n个整数 ai (-108<=ai<=108)

输出描述

第一行一个整数s 代表每行每列每个对角线的和值

接下来输出一个n*n的矩阵,表示填数方案。

数据保证有解,可能存在多种方案,输出字典序最小的(将每行顺次相连之后,字典序最小)

样例

#1
3
1 2 3 4 5 6 7 8 9
15
2 7 6
9 5 1
4 3 8

提示

解析

  1. 能算出来每行、每列、每条对角线的总和为矩阵中所有元素之和除于 n n n
  2. 因为题目要求字典序最小,因此需要对输入 n × n n\times n n×n 个元素进行从小到大排序。
  3. 类似于八皇后那样,不同的是这里是每个格子都需要填充,当填充到每行或每列或每条对角线时,就要及时的进行判断当前的总和是否合法,不合法就忽略这个值,选择下一个。
  4. 因为答案不唯一,又由于答案要字典序最小,因此找到第一个符合答案要求的结果就直接 System.exit(0) 退出。

AC Code

import java.io.*;
import java.math.*;
import java.util.*;

public class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StreamTokenizer st = new StreamTokenizer(br);
    static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));

    static int[][] board = new int[5][5];
    static int[] num = new int[20];
    static boolean[] vis = new boolean[20];
    static int n, cnt, a;

    public static void main(String[] args) throws Exception {
        n = nextInt();
        cnt = n * n;
        for(int i = 1; i <= cnt; i++) {
            num[i] = nextInt();
            a += num[i];
        }
        a /= n;
        Arrays.sort(num, 1, cnt + 1);
        dfs(1, 0);
    }

    public static void dfs(int x, int y) {
        y++;
        if(y == n + 1) {
            x++;
            y = 1;
        }
        if(x == n + 1) {
            out.println(a);
            print();
            System.exit(0);
            return;
        }
        for(int i = 1; i <= cnt; i++) {
            if(vis[i]) continue;
            board[x][y] = num[i];
            if(y == n) { // 行
                int t = 0;
                for(int k = 1; k <= n; k++) t += board[x][k];
                if(t != a) continue;
            }
            if(x == n) { // 列
                int t = 0;
                for(int k = 1; k <= n; k++) t += board[k][y];
                if(t != a) continue;
            }
            if(x == n && y == 1) { // 对角线(右上角到左下角)
                int t = 0;
                for(int k = 1; k <= n; k++) t += board[k][n - k + 1];
                if(t != a) continue;
            }
            if(x == n && y == n) { // 对角线(左上角到右下角)
                int t = 0;
                for(int k = 1; k <= n; k++) t += board[k][k];
                if(t != a) continue;
            }
            vis[i] = true;
            dfs(x, y);
            vis[i] = false;
        }
    }

    public static void print() {
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                out.print(board[i][j] + " ");
            }
            out.println();
        }
        out.flush();
    }

    public static int nextInt() throws Exception {
        st.nextToken();
        return (int) st.nval;
    }

    public static long nextLong() throws Exception {
        st.nextToken();
        return (long) st.nval;
    }

    public static String nextStr() throws Exception {
        st.nextToken();
        return st.sval;
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值