Problem 洛谷P1274-魔术数字游戏

Accept: 118    Submit: 243
Time Limit: 1000 mSec    Memory Limit : 128MB

Problem Description

填数字方格的游戏有很多种变化,如下图所示的4×4方格中,我们要选择从数字1到16来填满这十六个格子(Aij,其中i=1..4,j=1..4)。为了让游戏更有挑战性,我们要求下列六项中的每一项所指定的四个格子,其数字累加的和必须为34:

四个角落上的数字,即A11+A14+A41+A44=34。

每个角落上的2×2方格中的数字,例如左上角:A11+A12+A21+A22=34。

最中间的2×2方格中的数字,即A22+A23+A32+A33=34。

每条水平线上四个格子中的数字,即Ai1+Ai2+Ai3+Ai4=34,其中i=1..4。

每条垂直线上四个格子中的数字,即A1j+A2j+A3j+A4j=34,其中j=1..4。

两条对角线上四个格子中的数字,例如左上角到右下角:A11+A22+A33+A44=34。

右上角到左下角:A14+A23+A32+A41=34

 Input

输入文件会指定把数字1先固定在某一格内。输入的文件只有一行包含两个正数据I和J,表示第1行和第J列的格子放数字1。剩下的十五个格子,请按照前述六项条件用数字2到16来填满。

 Output

把全部的正确解答用每4行一组写到输出文件,每行四个数,相邻两数之间用一个空格隔开。两组答案之间,要以一个空白行相间,并且依序排好。排序的方式,是先从第一行的数字开始比较,每一行数字,由最左边的数字开始比,数字较小的解答必须先输出到文件中。

 Sample Input

1 1

Sample output

1 4 13 16
14 15 2 3
8 5 12 9
11 10 7 6
1 4 13 16
14 15 2 3
12 9 8 5
7 6 11 10
题目链接:https://www.luogu.org/problemnew/show/P1274
题解:无脑爆搜就好,剪枝的话就按照题目说的剪,代码比较冗长
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cstdlib>
  5 using namespace std;
  6
  7 const int maxn = 6;
  8
  9 int gra[maxn][maxn];
 10 bool used[maxn*3];
 11
 12 void output(){
 13     for(int i = 1;i <= 4;i++){
 14         printf("%d",gra[i][1]);
 15         for(int j = 2;j <= 4;j++){
 16             printf(" %d",gra[i][j]);
 17         }
 18         printf("\n");
 19     }
 20     printf("\n");
 21 }
 22
 23 void dfs(int x,int y){
 24     if(x > 4){
 25         output();
 26         return;
 27     }
 28     if(gra[x][y] == 1){
 29         if(x==2 && y==2){
 30             if(gra[1][1]+gra[1][2]+gra[2][1]+gra[2][2] != 34) return;
 31         }
 32         if(x==2 && y==4){
 33             if(gra[1][3]+gra[1][4]+gra[2][3]+gra[2][4] != 34) return;
 34         }
 35         if(x==4 && y==2){
 36             if(gra[3][1]+gra[3][2]+gra[4][1]+gra[4][2] != 34) return;
 37         }
 38         if(x==4 && y==4){
 39             if(gra[3][3]+gra[3][4]+gra[4][3]+gra[4][4] != 34) return;
 40             if(gra[1][1]+gra[2][2]+gra[3][3]+gra[4][4] != 34) return;
 41             if(gra[1][1]+gra[1][4]+gra[4][1]+gra[4][4] != 34) return;
 42         }
 43         if(x==4 && y==1){
 44             if(gra[1][4]+gra[4][1]+gra[2][3]+gra[3][2] != 34) return;
 45         }
 46         if(x==3 && y==3){
 47             if(gra[2][2]+gra[2][3]+gra[3][2]+gra[3][3] != 34) return;
 48         }
 49         if(x == 4){
 50             if(gra[1][y]+gra[2][y]+gra[3][y]+gra[4][y] != 34) return;
 51         }
 52         if(y == 4){
 53             if(gra[x][1]+gra[x][2]+gra[x][3]+gra[x][4] != 34) return;
 54         }
 55         if(y == 4) dfs(x+1,1);
 56         else dfs(x,y+1);
 57     }
 58     else{
 59         for(int i = 2;i <= 16;i++){
 60             if(!used[i]){
 61                 //output();
 62                 gra[x][y] = i;
 63                 if(x==2 && y==2){
 64                     if(gra[1][1]+gra[1][2]+gra[2][1]+gra[2][2] != 34){
 65                         gra[x][y] = 0;
 66                         continue;
 67                     }
 68                 }
 69                 if(x==2 && y==4){
 70                     if(gra[1][3]+gra[1][4]+gra[2][3]+gra[2][4] != 34){
 71                         gra[x][y] = 0;
 72                         continue;
 73                     }
 74                 }
 75                 if(x==4 && y==2){
 76                     if(gra[3][1]+gra[3][2]+gra[4][1]+gra[4][2] != 34){
 77                         continue;
 78                         gra[x][y] = 0;
 79                     }
 80                 }
 81                 if(x==4 && y==4){
 82                     if(gra[3][3]+gra[3][4]+gra[4][3]+gra[4][4] != 34){
 83                         gra[x][y] = 0;
 84                         continue;
 85                     }
 86                     if(gra[1][1]+gra[2][2]+gra[3][3]+gra[4][4] != 34){
 87                         gra[x][y] = 0;
 88                         continue;
 89                     }
 90                     if(gra[1][1]+gra[1][4]+gra[4][1]+gra[4][4] != 34){
 91                         gra[x][y] = 0;
 92                         continue;
 93                     }
 94                 }
 95                 if(x==4 && y==1){
 96                     if(gra[1][4]+gra[4][1]+gra[2][3]+gra[3][2] != 34){
 97                         gra[x][y] = 0;
 98                         continue;
 99                     }
100                 }
101                 if(x == 3 && y==3){
102                     if(gra[2][2]+gra[2][3]+gra[3][2]+gra[3][3] != 34){
103                         gra[x][y] = 0;
104                         continue;
105                     }
106                 }
107                 if(x == 4){
108                     if(gra[1][y]+gra[2][y]+gra[3][y]+gra[4][y] != 34){
109                         gra[x][y] = 0;
110                         continue;
111                     }
112                 }
113                 if(y == 4){
114                     if(gra[x][1]+gra[x][2]+gra[x][3]+gra[x][4] != 34){
115                         gra[x][y] = 0;
116                         continue;
117                     }
118                 }
119                 used[i] = true;
120                 if(y == 4) dfs(x+1,1);
121                 else dfs(x,y+1);
122                 used[i] = false;
123                 gra[x][y] = 0;
124             }
125         }
126     }
127 }
128
129 int main()
130 {
131     //freopen("input.txt","r",stdin);
132     int x,y;
133     scanf("%d%d",&x,&y);
134     memset(gra,0,sizeof(gra));
135     memset(used,false,sizeof(used));
136     gra[x][y] = 1;
137     used[1] = true;
138     dfs(1,1);
139     return 0;
140 }

转载于:https://www.cnblogs.com/npugen/p/9498186.html

洛谷P1274-魔术数字游戏相关推荐

  1. P1274 魔术数字游戏(DFS)

    文章目录 题目描述 输入格式 输出格式 样例输入 #1 样例输出 #1 提示 数据规模 Code 洛古原题 ~ 搜索+剪枝 ~ 题目描述 填数字方格的游戏有很多种变化,如下图所示的 4 × 4 4 \ ...

  2. 洛谷P1129 [ZJOI2007] 矩阵游戏 题解

    洛谷P1129 [ZJOI2007] 矩阵游戏 题解 题目链接:P1129 [ZJOI2007] 矩阵游戏 题意:给定一张有黑白棋子的正方形棋盘,问存不存在解法使得经过若干次交换行或列的操作后,左上角 ...

  3. 洛谷试炼场 P1553 数字反转(升级版)题解

    洛谷试炼场 P1553 数字反转(升级版)题解 [c] 题目描述 给定一个数,请将该数各个位上数字反转得到一个新数. 这次与NOIp2011普及组第一题不同的是:这个数可以是小数,分数,百分数,整数. ...

  4. 洛谷解题P1000 超级玛丽游戏(C++)

    此文章关于洛谷P1000 超级玛丽游戏 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)https://www.luogu.com.cn/problem/P1000题目的解析: 先看题 ...

  5. 洛谷:P1179 数字统计 C++三种写法总结

    0.前言 以前刷力扣的时候用过atoi函数,但是好像这道题没必要吧-- 今天刷洛谷的时候,看见一道数字统计,这么简单的题目还没做!天理难容啊,打开,我相信五分钟就敲完了,我打算改进代码,下面是几种方法 ...

  6. 洛谷——P1290 欧几里德的游戏

    P1290 欧几里德的游戏 题目描述 欧几里德的两个后代Stan和Ollie正在玩一种数字游戏,这个游戏是他们的祖先欧几里德发明的.给定两个正整数M和N,从Stan开始,从其中较大的一个数,减去较小的 ...

  7. 【bzoj3240 洛谷P1397】矩阵游戏[NOI2013](矩阵乘法+卡常)

    题目传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3240 这道题其实有普通快速幂+费马小定理的解法--然而我太弱了,一开始只想到了矩阵乘法的 ...

  8. [洛谷P2584][ZJOI2006]GameZ游戏排名系统

    题目大意:同[洛谷P4291][HAOI2008]排名系统(双倍经验) 题解:略 卡点:无 C++ Code: #include <cstdio> #include <map> ...

  9. 洛谷—— P1118 [USACO06FEB]数字三角形Backward Digit Su…

    https://www.luogu.org/problem/show?pid=1118#sub 题目描述 FJ and his cows enjoy playing a mental game. Th ...

  10. 洛谷P2252 取石子游戏(威佐夫博弈)

    题目背景 无 题目描述 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...

最新文章

  1. 突发!Spring Cloud 爆高危漏洞。。赶紧修复!!
  2. lnmp 60秒的服务器缓存时间
  3. 蓝牙激光雕刻机android,迷你型激光雕刻机!私人定制随你玩
  4. 每天被远程办公支配的恐惧,你怕了吗?
  5. VScode中Python的交互式命令环境使用笔记
  6. 三菱电机宣布放弃液晶面板业务 2022年6月停止生产相关模组
  7. java 合成mp3_java如何把文本合成音频格式(MP3)
  8. 报 java.lang.ExceptionInInitializerError 的常见解决方法
  9. 华为回应前员工被拘 251 天;暴风集团仅剩 10 余人;TiDB 3.0.6 发布 | 极客头条...
  10. Kafka必须掌握的核心技术:Java基础入门期末考试
  11. 【Python-3.3】字典中存储字典
  12. 远程执行命令不成功的问题
  13. 反射认识_03_改变成员变量Fields
  14. VS2017下载地址和安装教程(图解)
  15. pg数据库创建触发器
  16. Linux meset
  17. 十年程序人生——转自黎活明
  18. 20220601超简单百度地图街景图片爬取+绿视率计算
  19. 计算智能的极限、与人的关系及发展方向探讨
  20. android 百度地图自定义定位小箭头图标,并随着手机方向转动

热门文章

  1. 二叉树的非递归遍历(转载)
  2. [转]rails常用验证方法
  3. MySQL中json数据操作(转载)
  4. GCC、VS对C++标准的支持情况总结(转载)
  5. STL 容器迭代器失效总结(超级详细)
  6. MySQL 使用utf8mb4代替utf8
  7. 查找算法之六 哈希查找(C++版本)
  8. jdbc中excute,excuteUpdate,excuteQuery函数解释
  9. Android四大组件之Activity组件
  10. leetcode探索二叉树(一)