zoukankan      html  css  js  c++  java
  • ACM: Gym 100935G Board Game

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

    /*/
    这个题目和以前做过的一个DFS数独的题目很像,还要更简单;
    
    直接DFS暴力+ 枚举就行了
    
    AC代码:
    /*/
    #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(x,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);
    		DFS(0,0);
    		printf("Case %d:
    ",time);
    		for(int i=0; i<4; i++) {
    			for(int j=0; j<4; j++) {
    				printf("%d%c",mp[i][j],j==3?'
    ':' ');
    			}
    		}
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    死锁
    面试题: JVM的四大引用
    面试题:对象怎么定位
    面试题: Spring框架的好处
    VTK 图形基本操作进阶_表面重建技术(等值面提取)
    VTK 图形基本操作进阶_表面重建技术(三角剖分)
    VTK 图形基本操作进阶_多分辨率策略(模型细化的三种方法)
    VTK 图形基本操作进阶_多分辨率策略(模型抽取的三种方法)
    VTK 图形基本操作进阶_连通区域分析
    VTK 图形基本操作进阶_网格模型的特征边 与 封闭性检测
  • 原文地址:https://www.cnblogs.com/HDMaxfun/p/5759261.html
Copyright © 2011-2022 走看看