Acm:gym 100935G Board game-dfs Violence search

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

/* /This topic is similar to the topic of a DFS sudoku that has been done before, and more simple; Direct DFS violence + enumeration on the line AC code:/ */

#include "algorithm" #include "iostream" #include "CString" #include "cstdlib" #include "Cstdio" #include "string" # Include "vector" #include "queue" #include "Cmath" using namespace std;typedef long long LL; #define MEMSET (x, y) memset (x, Y, sizeof (x)) #define memcpy (y) memcpy (x,y,sizeof (x)) const int Mx=5;int mps[mx][mx];int Mp[mx][mx];bool num[20],flag; void Init () {memset (mp,0); memset (num,0); memset (mps,0); flag=0;} BOOL Check () {int sum=0;for (int i=0; i<4; i++) {sum+=mp[0][i];for (int j=0; j<4; J + +) {if (mp[i][j]==-1) return 0;}} for (int i=0; i<4; i++) {if (Sum!=mp[0][i]+mp[1][i]+mp[3][i]+mp[2][i]) return 0;if (sum!=mp[i][0]+mp[i][1]+mp[i][2]+ MP[I][3]) return 0;} return 1;} void DFS (int x,int y) {if (flag) return, if (mps[x][y]==-1) {for (int i=1; i<=16; i++) {if (num[i]| | Flag) continue;mp[x][y]=i;num[i]=1;if (y<3) DFS (x,y+1), else if (x<3) DFS (x+1,0), else {if (check ()) flag=1;num[i]= 0;return;} num[i]=0;}} else {if (y<3) DFS (x,y+1); else if (x<3) DFS (x+1,0); else {if (check ()) Flag=1;return;}}} int main () {int t;scanf ("%d", &t), for (Int. time=1; time<=t; time++) {init (); for (int i=0; i<4; i++) {for (int j=0; j<4; j + +) {scanf ("%d", &mps[i][j]); if (mps[i][j]!=-1) {num[mps[i][j]]=1;}}} memcpy (Mp,mps);D FS (0,0);p rintf ("Case%d:\n", time), for (Int. i=0; i<4; i++) {for (int j=0; j<4; J + +) {printf ("%d%c", Mp[i][j],j==3? ' \ n ': ');}}} return 0;}

  

Acm:gym 100935G Board game-dfs Violence search

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.