ACM: Gym 100935G Board Game - DFS暴力搜索
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
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
1
-1 -1 -1 -1
-1 -1 -1 -1
-1 5 13 12
3 8 9 14
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:\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暴力搜索的更多相关文章
- hdu 1427 速算24点 dfs暴力搜索
速算24点 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem De ...
- [HDU 1427]速度计算24点(DFS暴力搜索)
主题连接: pid=1427">http://acm.hdu.edu.cn/showproblem.php?pid=1427 思路:简单的DFS.dfs(sum,next,p)表 ...
- UVALive 5107 dfs暴力搜索
题目链接:A hard Aoshu Problem DES:给三个字符串,包含的字符是A-E范围内的.长度都不超过8.每个字符可以而且只可以匹配一个数字.两个字符不能匹配相同的数字.前两个式子之间可以 ...
- UVALive 5844 dfs暴力搜索
题目链接:UVAive 5844 Leet DES:大意是给出两个字符串.第一个字符串里的字符可以由1-k个字符代替.问这两个字符串是不是相等.因为1<=k<=3.而且第一个字符串长度小于 ...
- 洛谷P1019——单词接龙(DFS暴力搜索)
https://www.luogu.org/problem/show?pid=1019#sub 题目描述 单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母, ...
- poj 3050 Hopscotch DFS+暴力搜索+set容器
Hopscotch Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2774 Accepted: 1940 Description ...
- ACM 暴力搜索题 题目整理
UVa 129 Krypton Factor 注意输出格式,比较坑爹. 每次要进行处理去掉容易的串,统计困难串的个数. #include<iostream> #include<vec ...
- ACM: FZU 2107 Hua Rong Dao - DFS - 暴力
FZU 2107 Hua Rong Dao Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I6 ...
- 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. ...
随机推荐
- HYSBZ 2440 完全平方数(莫比乌斯反演)
链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2440 若i为质数,n为i*i的倍数,则称n为含平方因子数. 求1~n的无平方因子数. F(x) ...
- wordpress源码解析-目录结构-文件调用关系(1)
学习开源代码,是一种很快的提升自己的学习方法.Wordpress作为一个开源的博客系统,非常优秀,应用广泛,使用起来简单方便,具有丰富的主题和插件,可以按照自己的需求来任意的进行修改.所以就从word ...
- hdu 2897 巴什博弈变形 ***
大意:一堆石子共有n个,A,B两人轮流从中取,每次取的石子数必须在[p,q]区间内,若剩下的石子数少于p个,当前取者必须全部取完.最后取石子的人输.给出n,p,q,问先取者是否有必胜策略? Bash博 ...
- [Spring] 事务级别定义
记录下来,以后备用 //事务传播属性 @Transactional(propagation=Propagation.REQUIRED)//如果有事务,那么加入事务,没有的话新创建一个 @Transac ...
- php随机生成验证码
我们经常需要服务器向前端发送验证码,验证码需要随机产生,下面的用简单的代码实现了这一过程: <?php $pool='0123456789abcdefghijklmnopqrstuvwxyzAB ...
- Matlab tips and tricks
matlab tips and tricks and ... page overview: I created this page as a vectorization helper but it g ...
- ML 06、感知机
机器学习算法 原理.实现与实践 —— 感知机 感知机(perceptron)是二分类的线性分类模型,输入为特征向量,输出为实例的类别,取值+1和-1.感知机学习旨在求出将训练数据进行线性划分的分离超 ...
- 【maven + hibernate(注解) +spring +springMVC】 使用maven搭建项目
研究,百度,查资料+好友帮助,使用MyEcplise2015工具,通过maven搭建hibernate+springMVC+spring的项目,数据库采用MySql5.5 不过使用的版本会在项目搭建过 ...
- bat 炸弹升级
转自:http://digi.163.com/15/0320/06/AL4LP0QD0016192R.html 第1页:什么是批处理炸弹? 最近网上流传一个叫做<大哥别杀我>视频纷纷遭到网 ...
- thinkphp数据表操作恐怖事件。
1当使用thinkphp的where(array())时,如果里面的字段在数据库是没有的,则默认这个条件为1,这时就可能出现大批修改记录问题.如修改所有用户的密码.特别要注意的是,这里的表字段是区分大 ...