ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

GDUT-21级排位赛第一场 - G. Board Game(暴力搜索)

2022-03-28 17:31:53  阅读:141  来源: 互联网

标签:GDUT 21 int 排位赛 number four ++ board test


题意

Feras bought to his nephew Saleem a new game to help him learning calculating. The game consists of a board with 4 rows and 4 columns with 16 cubes. Every cube has a number from 1 to 16. Let's define the power of a column as the sum of its elements. In the same way, the power of a row is the sum of its elements. Saleem should arrange the cubes in the board such that the power of all columns and all rows are equal. To make the game easier, the nice uncle, Feras, will help him arranging 7 cubes, and Saleem should arrange the rest of the cubes.

输入格式

Your program will be tested on one or more test cases. The first line of the input will be a single integer T, the number of test cases (1 ≤  T ≤  100). Then the test cases. Each test case has four lines containing four integers. The j-th number in the i-th line describes the cell (i,j) of the board. If the number is -1 then the cell is empty and you have to fill it, otherwise, uncle Feras has already filled this cell.

输出格式

For each test case print a line in the following format: "Case c:" where c is the test case number starting from 1 then print the board in four lines every line has four numbers separated by space. If there is more than one solution print the solution that has the smallest order (See the notes below).

样例1

Input Output
1
-1 -1 -1 -1
-1 -1 -1 -1
-1 5 13 12
3 8 9 14
Case 1:
11 6 10 7
16 15 2 1
4 5 13 12
3 8 9 14

思路

深搜。

每次搜到填完的时候判断一下符不符合条件,具体做法也很简单,就是开个数组t[8]分别记录4行和4列的数的和是否相等。

代码

#include <bits/stdc++.h>
using namespace std;
const int N = 9;
int a[N][N];
int vis[20];

bool is_ok() {
    int t[8] = {0};
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            t[i] += a[i][j];
            t[j+4] += a[i][j];
        }
    }
    for (int i = 1; i < 8; i++) {
        if (t[i] != t[i-1]) return 0;
    }
    return 1;
}

bool dfs(int step) {
    if (step == 16) {
        if (is_ok()) return 1;
        return 0;
    } else {
        if (a[step/4][step%4] == -1) {
            for (int i = 1; i <= 16; i++) {
                if (!vis[i]) {
                    vis[i] = 1;
                    a[step/4][step%4] = i;
                    if (dfs(step+1)) return 1;
                    vis[i] = 0;
                    a[step/4][step%4] = -1;
                }
            }
        } else if (dfs(step+1)) return 1;
        return 0;
    }
}

int main() {
    int t;
    cin >> t;
    int kase = 1;
    while (t--) {
        memset(vis,0,sizeof vis);
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                cin >> a[i][j];
                if (a[i][j] != -1) vis[a[i][j]] = 1;
            }
        }
        dfs(0);
        printf("Case %d:\n", kase++);
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                if (j == 0) printf("%d", a[i][j]);
                else printf(" %d", a[i][j]);
            }
            puts("");
        }
    }
}

标签:GDUT,21,int,排位赛,number,four,++,board,test
来源: https://www.cnblogs.com/zenghaifan/p/16068161.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有