Codeforces Gym 100935G Board Game DFS

Source: Internet
Author: User

Board GameTime limit:2000MS Memory Limit:65536KB 64bit IO Format:%i64d &%i64 U Gym 100935G

Description

Standard Input/output
Statements

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

Input

Your program would be tested on one or more test cases. The first line of the input is a single integer T, the number of test cases (1 ≤t ≤100). Then the test cases. Each test case had 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 was empty and you had to fill it, otherwise, Uncle Feras had 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 PR int the board in four lines every line have four numbers separated by space. If there is more than one solution print the solution that have the smallest order (see the notes below).

Sample Input

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

Hint

In the sample input there are 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 have 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

Test instructions: The place where 4*4 is given some numbers (the numbers are 1-16 inside) the rest-1 of those who want us to make sure that each row has the same sum for each column (the rows and columns are the same)

After you have entered all the numbers, you can judge the first result and return it.

#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 0x3f3f 3f3f#define Lson l,m,rt<<1#define Rson m+1,r,rt<<1|1typedef 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 (a); } return 0;}

  

Codeforces Gym 100935G Board Game DFS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.