CodeForces Gym 100935G Board Game DFS

 Board Game

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Gym 100935G

Description

standard input/output
Statements

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.

Input

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.

Output

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).

Sample Input

Input

1-1 -1 -1 -1-1 -1 -1 -1-1 5 13 123 8 9 14

Output

Case 1:11 6 10 716 15 2 14 5 13 123 8 9 14

Hint

in the sample input there is more than one solution:

Solution1:

16 15 2 1

11 6 10 7

4 5 13 12

3 8 9 14

Solution2:

11 6 10 7

16 15 2 1

4 5 13 12

3 8 9 14

but we select solution2 because it has the smallest order when we write the rows in one line.

Solution1: 16 15 2 1 11 6 10 7 4 5 13 12 3 8 9 14

Solution2: 11 6 10 7 16 15 2 1 4 5 13 12 3 8 9 14

题意:4*4的地方里面  给定了一些数字  (数字都是1—16里面的) 剩下-1那些要我们来确定  每一行 每一列的总和都要相同 (行和列的和都要相同)

把所有的数都输入进去之后判断一下  得到第一个结果之后就可以return了

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <iomanip>
#include <math.h>
#include <map>
using namespace std;
#define FIN     freopen("input.txt","r",stdin);
#define FOUT    freopen("output.txt","w",stdout);
#define INF     0x3f3f3f3f
#define lson    l,m,rt<<1
#define rson    m+1,r,rt<<1|1
typedef long long LL;

const int MX=1e5+5;

int Map[10][10];
int vis[20];
int num[20];
int flag;

bool ok(){
   int sum=Map[1][1]+Map[1][2]+Map[1][3]+Map[1][4];
   for(int i=1;i<=4;i++){
       if(sum!=Map[i][1]+Map[i][2]+Map[i][3]+Map[i][4])  return false;
       if(sum!=Map[1][i]+Map[2][i]+Map[3][i]+Map[4][i])  return false;
   }
   return true;

}

void dfs(int x,int y){
   if(flag)  return ;
   if(x>4){
       if(ok()){
           flag=1;
           for(int i=1;i<=4;i++)
               for(int j=1;j<=4;j++)
                  printf("%d%c",Map[i][j],j==4?‘\n‘:‘ ‘);
       }
       return ;
   }
   int xx=x,yy=y;
   yy++;
   if(yy>4){
       xx++;
       yy=1;
   }
   if(Map[x][y]!=-1)  dfs(xx,yy);
   else
      for(int i=1;i<=9;i++){
          if(vis[i])  continue;
          Map[x][y]=num[i];
          vis[i]=1;
          dfs(xx,yy);
          if(flag)  return ;
          vis[i]=0;
          Map[x][y]=-1;
      }

}

int main()
{
    //FIN
    int T;
    scanf("%d",&T);
    for(int c=1;c<=T;c++){
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=16;i++)  num[i]=i;

        for(int i=1;i<=4;i++)
            for(int j=1;j<=4;j++){
                scanf("%d",&Map[i][j]);
                if(Map[i][j]!=-1){
                    int k=Map[i][j];
                    num[k]=INF;
                }
            }

         sort(num+1,num+1+16);
         flag=0;
         printf("Case %d:\n",c);
         dfs(1,1);

    }
    return 0;
}

  

时间: 2024-10-10 13:37:18

CodeForces Gym 100935G Board Game DFS的相关文章

ACM: Gym 100935G Board Game - DFS暴力搜索

Board Game Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Gym 100935G Description standard input/outputStatements Feras bought to his nephew Saleem a new game to help him learning calculating. The game consists of a boar

Codeforces Gym - 101147J Whistle&#39;s New Car

Discription Statements Whistle has bought a new car, which has an infinite fuel tank capacity. He discovered an irregular country since it has n cities and there are exactly n?-?1roads between them, of course, all cities are connected. He is so much

Codeforces Gym 101174 A Within Arm&#39;s Reach 贪心 手臂

#include<iostream> #include<stdio.h> #include <string.h> #include <algorithm> #include <vector> #include <math.h> using namespace std; #define LL long long const int maxn=25; double a[maxn],l[maxn],r[maxn]; double ex,ey

Codeforces gym Hello 2015 Div1 B and Div2 D

Codeforces gym 100571 problem D Problem 给一个有向图G<V,E>和源点S,边的属性有长度L和颜色C,即E=<L,C>.进行Q次询问,每次给定一个点X,输出S到X的最短路的长度(不存在则输出 -1).但要求S到X的路径中相邻两条边颜色不一样. Limits Time Limit(ms): 1000 Memory Limit(MB): 256 |V|, |E|: [1, 10^5] X, S: [1, |V| ] L: [1, 10^9] |C|

Codeforces gym Hello 2015 Div1 E

Codeforces gym 100570 problem E (一种处理动态最长回文子串问题的方法) Problem 给一个长度为N的字符串S,字符集是'a'-'z'.进行Q次操作,操作分三种.一,修改位置X的字符为C:二,查询以P位置为中心的最长回文子串的长度,并输出:三,查询以P与P+1的中间位置为中心的最长回文子串的长度,并输出. More 第二种操作子串长度为奇数,一定存在:第三种操作子串长度为偶数,若不存在,输出 -1. Limits Time Limit(ms): 4000(1s足

Codeforces gym Hello 2015 Div1 C and Div2 E

Codeforces gym 100570 problem C Codeforces gym 100571 problem E Problem 给一个N行M列的矩阵Ma,进行Q次(Q<=10)查询,每次给定一个K,问有多少子矩阵,满足最大值max - 最小值min <=K. Limits Time Limit(ms): 8000 Memory Limit(MB): 512 N, M: [1, 400] Q: [1, 10] Ma(i, j), K: [1, 10^9] Solution (Th

【模拟】ECNA 2015 I What&#39;s on the Grille? (Codeforces GYM 100825)

题目链接: http://codeforces.com/gym/100825 题目大意: 栅栏密码.给定N(N<=10),密钥为一个N*N的矩阵,'.'代表空格可以看到,'X'代表被遮挡,还有密文字符串S,长度为N*N 每次将这个矩阵顺时针旋转90°,把矩阵中空格对应的位置按照从上到下从左到右的顺序依次填充上密文字符,求最终这个密文字符能否填满N*N的矩阵,能按顺序输出得到的答案,不能输出"invalid grille" 题目思路: [模拟] 直接模拟即可.旋转的坐标公式很好推.

Codeforces gym Hello 2015 Div2 B

Codeforces gym 100571 problem B Problem 设函数F(x),F(1)与F(2)已知,且当 i>=3,F(i)=a*F(i-2)+b*F(i-1).再给一个长度为N的数列A,进行Q次如下操作:每次给一个区间[L, R],对于每个k(L=<k<=R),将A[k]=A[k]+F[k-L+1].最后输出数列A(mod 10^9+7). Limits Time Limit(ms): 1000 Memory Limit(MB): 256 N, Q: [1, 10^

CodeForces - 383C Propagating tree(dfs + 线段树)

题目大意: 给出一棵树,树上每个节点都有权值,然后有两个操作. 1 x val 在结点x上加上一个值val,x的儿子加上 -val,x的儿子的儿子加上 - (-val),以此类推. 2 x 问x节点的值. 思路分析: 每个节点上加值都是给自己的儿子节点加,而且这个是颗树. 比如样例上的,如果你给node 1加一个值,那么五个节点都加. 再给node 2加个值,2的儿子节点也加了,之前给1加的值也要加到2号节点的儿子. 所以你会发现节点的儿子会存在一个从属的关系. 这样的话,我们可以把所有节点从新