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
    -1 5 13 12
    3 8 9 14
Output
  1. 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

  1. /*/
  2. 这个题目和以前做过的一个DFS数独的题目很像,还要更简单;
  3.  
  4. 直接DFS暴力+ 枚举就行了
  5.  
  6. AC代码:
  7. /*/
  1. #include"algorithm"
  2. #include"iostream"
  3. #include"cstring"
  4. #include"cstdlib"
  5. #include"cstdio"
  6. #include"string"
  7. #include"vector"
  8. #include"queue"
  9. #include"cmath"
  10. using namespace std;
  11. typedef long long LL ;
  12. #define memset(x,y) memset(x,y,sizeof(x))
  13. #define memcpy(x,y) memcpy(x,y,sizeof(x))
  14. const int MX=5;
  15.  
  16. int mps[MX][MX];
  17. int mp[MX][MX];
  18. bool num[20],flag;
  19.  
  20. void init() {
  21. memset(mp,0);
  22. memset(num,0);
  23. memset(mps,0);
  24. flag=0;
  25. }
  26.  
  27. bool check() {
  28. int sum=0;
  29. for(int i=0; i<4; i++) {
  30. sum+=mp[0][i];
  31. for(int j=0; j<4; j++) {
  32. if(mp[i][j]==-1)return 0;
  33. }
  34. }
  35. for(int i=0; i<4; i++) {
  36. if(sum!=mp[0][i]+mp[1][i]+mp[3][i]+mp[2][i])return 0;
  37. if(sum!=mp[i][0]+mp[i][1]+mp[i][2]+mp[i][3])return 0;
  38. }
  39. return 1;
  40. }
  41.  
  42. void DFS(int x,int y) {
  43. if(flag)return ;
  44. if(mps[x][y]==-1) {
  45. for(int i=1; i<=16; i++) {
  46. if(num[i]||flag)continue;
  47. mp[x][y]=i;
  48. num[i]=1;
  49. if(y<3) DFS(x,y+1);
  50. else if(x<3) DFS(x+1,0);
  51. else {
  52. if(check())flag=1;
  53. num[i]=0;
  54. return;
  55. }
  56. num[i]=0;
  57. }
  58. } else {
  59. if(y<3) DFS(x,y+1);
  60. else if(x<3) DFS(x+1,0);
  61. else {
  62. if(check()) flag=1;
  63. return ;
  64. }
  65. }
  66. }
  67.  
  68. int main() {
  69. int T;
  70. scanf("%d",&T);
  71. for(int time=1; time<=T; time++) {
  72. init();
  73. for(int i=0; i<4; i++) {
  74. for(int j=0; j<4; j++) {
  75. scanf("%d",&mps[i][j]);
  76. if(mps[i][j]!=-1) {
  77. num[mps[i][j]]=1;
  78. }
  79. }
  80. }
  81. memcpy(mp,mps);
  82. DFS(0,0);
  83. printf("Case %d:\n",time);
  84. for(int i=0; i<4; i++) {
  85. for(int j=0; j<4; j++) {
  86. printf("%d%c",mp[i][j],j==3?'\n':' ');
  87. }
  88. }
  89. }
  90. return 0;
  91. }

  

ACM: Gym 100935G Board Game - DFS暴力搜索的更多相关文章

  1. hdu 1427 速算24点 dfs暴力搜索

    速算24点 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem De ...

  2. [HDU 1427]速度计算24点(DFS暴力搜索)

    主题连接:  pid=1427">http://acm.hdu.edu.cn/showproblem.php?pid=1427 思路:简单的DFS.dfs(sum,next,p)表 ...

  3. UVALive 5107 dfs暴力搜索

    题目链接:A hard Aoshu Problem DES:给三个字符串,包含的字符是A-E范围内的.长度都不超过8.每个字符可以而且只可以匹配一个数字.两个字符不能匹配相同的数字.前两个式子之间可以 ...

  4. UVALive 5844 dfs暴力搜索

    题目链接:UVAive 5844 Leet DES:大意是给出两个字符串.第一个字符串里的字符可以由1-k个字符代替.问这两个字符串是不是相等.因为1<=k<=3.而且第一个字符串长度小于 ...

  5. 洛谷P1019——单词接龙(DFS暴力搜索)

    https://www.luogu.org/problem/show?pid=1019#sub 题目描述 单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母, ...

  6. poj 3050 Hopscotch DFS+暴力搜索+set容器

    Hopscotch Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2774 Accepted: 1940 Description ...

  7. ACM 暴力搜索题 题目整理

    UVa 129 Krypton Factor 注意输出格式,比较坑爹. 每次要进行处理去掉容易的串,统计困难串的个数. #include<iostream> #include<vec ...

  8. ACM: FZU 2107 Hua Rong Dao - DFS - 暴力

    FZU 2107 Hua Rong Dao Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

  9. Gym 100650H Two Ends DFS+记忆化搜索

    Problem H: Two EndsIn the two-player game “Two Ends”, an even number of cards is laid out in a row. ...

随机推荐

  1. JavaScript - 事件流

    事件流 事件冒泡就是事件沿DOM树向上传播,在没一级节点上都会发生,直至传播到document对象. 事件捕获正好相反,但是老版本的浏览器不支持,因此很少有人使用事件捕获. 事件处理程序 HTMl 事 ...

  2. hdu 4055 递推

    转自:http://blog.csdn.net/shiqi_614/article/details/7983298 题意:由数字1到n组成的所有排列中,问满足题目所给的n-1个字符的排列有多少个,如果 ...

  3. AIX RAC ORA-27504 ORA-27300 ORA-27301 ORA-27302 ORA-27303

    操作系统:AIX6.1  数据库:Oracle10.2.0.5 RAC 2个节点.其中一个节点正常,另外一个节点的instance的状态是offline的,手工去启动的时候报错: ORA: IPC e ...

  4. [unity3d插件]2dtoolkit系列一 创建精灵

    从今天开始要做一个2d游戏,由于之前都是做cocos2dx的,然后接触了一段时间的unity3d,都是做3D方面的东西,得知要做2d游戏还是有点开心的,或许因为不想丢失之前的2d游戏的一些思想,然后接 ...

  5. Asp.Net MVC中DropDownListFor的用法

    在Asp.Net MVC中可以用DropDownListFor的方式来让用户选择已定列表中的一个数值.用法不复杂,这里简单做一个记录. 首先我们要定义一个 Model ,用户在 DropDownLis ...

  6. C#复习、面向对象阶段开始

    C#复习:在控制台程序中使用结构体.集合,完成下列要求项目要求:一.连续输入5个学生的信息,每个学生都有以下4个内容:1.序号 - 根据输入的顺序自动生成,不需要手动填写,如输入第一个学生的序号是1, ...

  7. 【maven 报错】maven项目执行maven install时报错Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode)

    在使用maven新建的web项目中,执行 执行如上的这两个操作,报错: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-co ...

  8. mysql性能监控相关

    目录 一,获取mysql用户下的进程总数 二,主机性能状态 三,CPU使用率 四,磁盘IO量 五,swap进出量[内存] 六,数据库性能状态 七.querylog 八.mysqladmin的exten ...

  9. python特殊函数 __len__(self):

    __len__ 如果一个类表现得像一个list,要获取有多少个元素,就得用 len() 函数. 要让 len() 函数工作正常,类必须提供一个特殊方法__len__(),它返回元素的个数. 例如,我们 ...

  10. C语言连接SQLSERVER数据库

    第一步:配置ODBC.在配置ODBC时有用户DSN.系统DSN.和文件DSN三种方法,为了稳妥起见,采用系统DSN. DSN的名字叫LocalServer,帐号:sa,密码123456 第二步:打开V ...