【蓝桥杯 - 真题】六角幻方(dfs+剪枝)

标题:六角幻方

    把 1 2 3 ... 19 共19个整数排列成六角形状,如下:

    * * *
   * * * *
  * * * * *
   * * * * 
    * * *

    要求每个直线上的数字之和必须相等。共有15条直线哦!

    再给点线索吧!我们预先填好了2个数字,第一行的头两个数字是:15 13,参见图【p1.png】,黄色一行为所求。

    请你填写出中间一行的5个数字。数字间用空格分开。

    这是一行用空格分开的整数,请通过浏览器提交答案,不要填写任何多余的内容(比如说明性的文字等)

解题报告:

  好久没写搜索了,,当个练习。

不难发现每一行的值的和应该是38,所以我们已经有了三个已知数字了。

根据对称性把这三个数放在每一行的开始,相当于有了三个已知行的行首,然后按行dfs,同时判断是否凑够了38,如果超了38那就直接return就行,这一点可以用附初始值为比较大的值(这里用了100000)来实现。如果还没填满这一行但是和已经大于38了  或者  都填满了并且还不是38,那就return。这应该是最优秀的剪枝了,是每一步都会有剪枝。当然如果不这样剪枝,而是直接: 每一行都填满了再判断是否等于38,也可以。

最终答案:

10 12 16
13 4 2 19
15 8 5 7 3
14 6 1 17
9 11 18

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
int a[55][55],all[55] = {0,10,13,15,0,0};
int up[55] = {0,3,4,5,4,3};
bool vis[55],ok[55][55];
bool cc() {if(a[1][2]+a[2][2]+a[3][2]+a[4][1]!=38) return 0;if(a[1][3]+a[2][3]+a[3][3]+a[4][2]+a[5][1]!=38) return 0;if(a[2][4]+a[3][4]+a[4][3]+a[5][2]!=38) return 0;if(a[3][5]+a[4][4]+a[5][3]!=38) return 0;if(a[1][3]+a[2][4]+a[3][5]!=38) return 0;if(a[1][2]+a[2][3]+a[3][4]+a[4][4]!=38) return 0;if(a[1][1]+a[2][2]+a[3][3]+a[4][3]+a[5][3]!=38) return 0;if(a[2][1]+a[3][2]+a[4][2]+a[5][2]!=38) return 0;if(a[3][1]+a[4][1]+a[5][1]!=38) return 0;return 1;
}
bool check() {if(a[1][2]+a[2][2]+a[3][2]+a[4][1]!=38 && a[1][2]+a[2][2]+a[3][2]+a[4][1]<100000) return 0;if(a[1][3]+a[2][3]+a[3][3]+a[4][2]+a[5][1]!=38 && a[1][3]+a[2][3]+a[3][3]+a[4][2]+a[5][1]<100000) return 0;if(a[2][4]+a[3][4]+a[4][3]+a[5][2]!=38 && a[2][4]+a[3][4]+a[4][3]+a[5][2]<100000) return 0;if(a[3][5]+a[4][4]+a[5][3]!=38 && a[3][5]+a[4][2]+a[5][3]<100000) return 0;if(a[1][3]+a[2][4]+a[3][5]!=38 && a[1][3]+a[2][4]+a[3][5]<100000) return 0;if(a[1][2]+a[2][3]+a[3][4]+a[4][4]!=38 && a[1][2]+a[2][3]+a[3][4]+a[4][4]<100000) return 0;if(a[1][1]+a[2][2]+a[3][3]+a[4][3]+a[5][3]!=38 && a[1][1]+a[2][2]+a[3][3]+a[4][3]+a[5][3]<100000) return 0;if(a[2][1]+a[3][2]+a[4][2]+a[5][2]!=38 && a[2][1]+a[3][2]+a[4][2]+a[5][2]<100000) return 0;if(a[3][1]+a[4][1]+a[5][1]!=38 && a[3][1]+a[4][1]+a[5][1]<100000) return 0;	return 1;
}
void dfs(int x,int y) {if(x == 6) {if(cc() == 0) return;for(int i = 1; i<=5; i++) {for(int j = 1; j<=up[i]; j++) {printf("%d ",a[i][j]);}printf("\n");}return;			}if(check() == 0) return;if(ok[x][y]) {dfs(x,y+1);return;}for(int i = 1; i<=19; i++) {if(vis[i]) continue;if(all[x] + i > 38) continue;if(y == up[x]) {if(all[x] + i != 38) continue;else {all[x] += i;vis[i] = 1;a[x][y] = i;dfs(x+1,1);a[x][y] = 100000;all[x] -= i;vis[i] = 0;return;}}vis[i] = 1;all[x] += i;a[x][y] = i;dfs(x,y+1);a[x][y] = 100000;vis[i] = 0;all[x] -= i;}	
}
int main()
{for(int i = 1; i<=5; i++) {for(int j = 1; j<=5; j++) a[i][j] = 100000;}a[1][1]=10;a[2][1]=13;a[3][1]=15;ok[1][1]=1;ok[2][1]=1;ok[3][1]=1;vis[10]=1;vis[13]=1;vis[15]=1;dfs(1,1);return 0 ;
}
//19:00-19:20/*
10 12 16
13 4 2 19
15 8 5 7 3
14 6 1 17
9 11 18*/

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440399.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【2019浙江省赛 - J】Welcome Party(并查集,bfs,优先队列,建图)

题干&#xff1a; The 44th World Finals of the International Collegiate Programming Contest (ICPC 2020) will be held in Moscow, Russia. To celebrate this annual event for the best competitive programmers around the world, it is decided to host a welcome pa…

app android de,Android Deobfuscation

Android Deobfuscation11/11/2019本文内容ProGuard、DexGuard 和 R8 是用于对 Android 应用程序的代码进行优化和模糊处理的工具。 它会删除未使用的代码、重命名具有语义模糊名称的类、字段和方法&#xff0c;使基本代码更小&#xff0c;更难反向工程。 若要在 Android 应用中…

【2019浙江省赛 - B】Element Swapping(思维,数学)

题干&#xff1a; DreamGrid has an integer sequence and he likes it very much. Unfortunately, his naughty roommate BaoBao swapped two elements and () in the sequence when DreamGrid wasnt at home. When DreamGrid comes back, he finds with dismay that his …

android 没有指令,android – 运行时没有命令输出:’am start -n

最近,当我试图在我的设备上运行我的Android应用程序时,我必须在实际启动之前从Eclipse运行它几次.我试图重新安装JRE,JDK和IDE,我试图切换工作区.我还让Eclipse为Java VM使用更多的RAM.我的IDE,JRE,JDK,ADT和ADT插件都是最新的.对此有任何建议非常感谢.No command output when …

【2019浙江省赛 - E】Sequence in the Pocket(思维)

题干&#xff1a; DreamGrid has just found an integer sequence in his right pocket. As DreamGrid is bored, he decides to play with the sequence. He can perform the following operation any number of times (including zero time): select an element and move i…

android和ios系统的内存,WP和Saipan系统的流畅程度相当于ios,占用的内存很少,但是为什么要用Android取代它...

当涉及到WP和Symbian系统时&#xff0c;许可能没有听说过它&#xff0c;但是对于大多数关注智能手机市场增长的消费者来说&#xff0c;它已经为人们所熟悉&#xff0c;并且许已经使用了它. 当时在功能性机器上使用了Saipan系统&#xff0c;但是您会发现该系统的流畅性与当时的i…

【CodeForces - 1042C】Array Product(思维,有坑细节)

题干&#xff1a; You are given an array aa consisting of nn integers. You can perform the following operations with it: Choose some positions ii and jj (1≤i,j≤n,i≠j1≤i,j≤n,i≠j), write the value of ai⋅ajai⋅aj into the jj-th cell and remove the num…

红米pro android o刷机,红米Pro如何刷机?你可以通过这两种方法获取root权限!

小米官网最近发布了关于红米pro的消息&#xff0c;相信很多米粉们已经上手了&#xff0c;那么新到手的机子怎么刷机呢&#xff1f;下面小编为大家带来一个完整的红米Pro官方卡刷机教程&#xff0c;希望可以帮助到大家。红米Pro卡刷升级教程&#xff1a;准备工作1.进入红米Pro刷…

【2019浙江省赛 - K 】Strings in the Pocket(马拉车,思维)

题干&#xff1a; BaoBao has just found two strings and in his left pocket, where indicates the -th character in string , and indicates the -th character in string . As BaoBao is bored, he decides to select a substring of and reverse it. Formally spe…

android+微信一键关注,一键关注微信公众平台JS代码有哪些?

一键关注微信公众平台JS代码有哪些&#xff1f;在网页设置一个按钮或者链接可以让用户一键关注微信公众平台&#xff0c;那么这种一键关注微信公众平台的功能如何实现呢&#xff1f;下面小编分享给大家一键关注微信公众平台的JS代码。在微信上&#xff0c;通过微信公众平台推送…

【2019浙江省赛 - A】Vertices in the Pocket(权值线段树下二分,图,思维)

题干&#xff1a; DreamGrid has just found an undirected simple graph with vertices and no edges (thats to say, its a graph with isolated vertices) in his right pocket, where the vertices are numbered from 1 to . Now he would like to perform operations …

html写原生曲线图,HTML5 平滑的正弦波曲线图

JavaScript语言&#xff1a;JaveScriptBabelCoffeeScript确定var waves new SineWaves({el: document.getElementById(waves),speed: 4,width: function() {return $(window).width();},height: function() {return $(window).height();},ease: SineInOut,wavesWidth: 80%,wav…

【CodeForces - 1150C】Prefix Sum Primes(思维)

题干&#xff1a; Were giving away nice huge bags containing number tiles! A bag we want to present to you contains nn tiles. Each of them has a single number written on it — either 11or 22. However, there is one condition you must fulfill in order to re…

asp.net 写入html代码,asp.net读取模版并写入文本文件

本文要介绍的是ASP.NET怎样读写文本文件&#xff0c;但更重要的是实现的过程。使用的工具是Visual Studio 2015 &#xff0c;.NET版本是4.6.1 。一共建立的2个项目&#xff0c;HoverTreePanel和HoverTreeWeb&#xff0c;都是ASP.NET项目。文章末尾附源码下载。项目结果如下图&a…

【CodeForces - 1150A】Stock Arbitraging (贪心,水题)

题干&#xff1a; Welcome to Codeforces Stock Exchange! Were pretty limited now as we currently allow trading on one stock, Codeforces Ltd. We hope youll still be able to make profit from the market! In the morning, there are nn opportunities to buy share…

html以图像中心定位,在HTML图像上水平和垂直居中文本(绝对定位)

Michael Roach0htmlcssflexbox鉴于以下设计元素,我试图在html中包含图像,以便可以使用css过渡(悬停效果)操纵不透明度.这里的主要缺点是我使用手动垂直居中(绝对/顶部:4​​0%),这在缩小浏览器时变得明显.在使用绝对定位时,是否可以使用flexbox或table进行垂直居中&#xff1f;…

*【CodeForces - 1150D】Three Religions(dp,预处理,思维)

题干&#xff1a; During the archaeological research in the Middle East you found the traces of three ancient religions: First religion, Second religion and Third religion. You compiled the information on the evolution of each of these beliefs, and you now…

基于android公交车线路查询论文文献,本科毕业论文---基于android的手机公交线路查询系统.doc...

毕 业 设 计( 论 文 )题目手机公交线路查询系统作者学院专业学号指导教师摘 要关键词&#xff1b;AbstractWith the level of people’s life improving,going out by bus become a necessary part of daily life.And the traffic line to destination should be known everyti…

【POJ - 3159】Candies (差分约束,卡SPFA)

题干&#xff1a; 在幼儿园的时候&#xff0c;Flymouse是班上的班长。有时班主任会给班上的孩子们带来一大袋糖果&#xff0c;让他们分发。所有的孩子都非常喜欢糖果&#xff0c;经常比较他们和别人买的糖果的数量。一个孩子A可以有这样的想法&#xff0c;尽管可能是另一个孩子…

计算机英语第六单元,计算机专业英语第六版第十单元课后汉译英,We do use other forms....这个do...

同样的 &#xff0c;多线程也存在许多缺点 &#xff0c;在考虑多线程时需要进行充分的考虑。多线程的主要缺点包括&#xff1a;Similarly, multi thread also has many shortcomings, in the consideration of the need for the full consideration of multiple threads. The m…