蓝桥杯java第五届决赛第二题--六角幻方
生活随笔
收集整理的這篇文章主要介紹了
蓝桥杯java第五届决赛第二题--六角幻方
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
標(biāo)題:六角幻方把 1 2 3 ... 19 共19個(gè)整數(shù)排列成六角形狀,如下:* * ** * * ** * * * ** * * * * * *要求每個(gè)直線上的數(shù)字之和必須相等。共有15條直線哦!再給點(diǎn)線索吧!我們預(yù)先填好了2個(gè)數(shù)字,第一行的頭兩個(gè)數(shù)字是:15 13,參見(jiàn)圖【p1.png】,黃色一行為所求。請(qǐng)你填寫(xiě)出中間一行的5個(gè)數(shù)字。數(shù)字間用空格分開(kāi)。這是一行用空格分開(kāi)的整數(shù),請(qǐng)通過(guò)瀏覽器提交答案,不要填寫(xiě)任何多余的內(nèi)容(比如說(shuō)明性的文字等)
思路:由于這個(gè)數(shù)據(jù)量比較大,所以需要提前剪枝,不然可能半天都運(yùn)行不出來(lái),gg了。
public class _2 { static int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 };// 總集 static int[] book = new int[20];// 標(biāo)示集 static int[] res = new int[a.length];// 結(jié)果集 public static void main(String[] args) { dfs(0); } public static void dfs(int step) { if (step == 1) {// 遞歸過(guò)程中,對(duì)函數(shù)進(jìn)行剪枝,而且盡量在遞歸前期就剪掉,剪得越早,越快出結(jié)果 if (res[0] != 15) return; } if (step == 2) { if (res[1] != 13 && res[1] != 15) return; } if (step == 7) { if (28 + res[2] != res[3] + res[4] + res[5] + res[6]) return; } if (step == 8) { if (28 + res[2] != 15 + res[3] + res[7]) return; } if (step == 12) { if (28 + res[2] != res[7] + res[8] + res[9] + res[10] + res[11]) return; if (28 + res[2] != res[2] + res[6] + res[11]) return; } if (step == 13) { if (28 + res[2] != 13 + res[4] + res[8] + res[12]) return; } if (step == 16) { if (res[12] + res[13] + res[14] + res[15] != res[2] + 28) return; if (28 + res[2] != 13 + res[5] + res[10] + res[15]) return; } if (step == 17) { if (28 + res[2] != res[7] + res[12] + res[16]) return; if (28 + res[2] != res[2] + res[5] + res[9] + res[13] + res[16]) return; } if (step == 19) {// 結(jié)果集滿(mǎn),進(jìn)行具體判斷結(jié)果是否合法操作 int s1 = res[2] + res[6] + res[11]; int s2 = res[1] + res[5] + res[10] + res[15]; int s3 = res[0] + res[4] + res[9] + res[14] + res[18]; int s4 = res[3] + res[8] + res[13] + res[17]; int s5 = res[7] + res[12] + res[16]; int s6 = res[0] + res[1] + res[2]; int s7 = res[3] + res[4] + res[5] + res[6]; int s8 = res[7] + res[8] + res[9] + res[10] + res[11]; int s9 = res[12] + res[13] + res[14] + res[15]; int s10 = res[16] + res[17] + res[18]; int s11 = res[0] + res[3] + res[7]; int s12 = res[1] + res[4] + res[8] + res[12]; int s13 = res[2] + res[5] + res[9] + res[13] + res[16]; int s14 = res[6] + res[10] + res[14] + res[17]; int s15 = res[11] + res[15] + res[18]; // 十五條直線上的數(shù)字和必須相等并且滿(mǎn)足題中條件,15和13位置固定 if (res[0] == 15 && res[1] == 13 && s1 == s5 && s1 == s2 && s1 == s3 && s1 == s4 && s1 == s6 && s1 == s7 && s1 == s8 && s1 == s9 && s1 == s10 && s1 == s11 && s1 == s12 && s1 == s13 && s1 == s14 && s1 == s15) { System.out.println(res[7] + " " + res[8] + " " + res[9] + " " + res[10] + " " + res[11]);// 輸出結(jié)果 } return;// 遞歸出口 } for (int i = 0; i < a.length; i++) {// 遍歷總集 if (book[a[i]] == 0) {// 判斷當(dāng)前元素是否被使用過(guò) res[step] = a[i];// 使用當(dāng)前元素,加入res結(jié)果集 book[a[i]] = 1;// 將當(dāng)前元素標(biāo)記為已使用 dfs(step + 1);// 遞歸調(diào)用自身,step為下一個(gè)元素元素下標(biāo) book[a[i]] = 0;// 遞歸結(jié)束后至此,回溯,回收當(dāng)前元素,標(biāo)記為未使用 } } } }總結(jié)
以上是生活随笔為你收集整理的蓝桥杯java第五届决赛第二题--六角幻方的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 蓝桥杯java第五届决赛第一题--国王的
- 下一篇: 蓝桥杯java第五届决赛第三题--格子放