Board Game
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
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; }