/*接上篇
对了,我是来找规律的,找幻立方的生成方法的
先看看这个7阶的完美幻立方
初始位置1是在4-1-1(第4行,第1列,第1层,0为基数)
下一步的位置与上一步的相对位移是4-2-2
7的倍数后有个跳步,因为按4-2-2规则,如果不跳的话8的位置就是1的位置,总不能把1给换了吧
7的倍数后的数与前数的相对位移是2-4-0
7*7=49的倍数后又有一个跳步,同样,按照前面的规则,如果不跳的话就没得走了
跳步的相对位移是2-3-3
我们把阶数step,起始位置point0,移步规则deltaPoint,7倍数的跳步规则deltaLine,49倍数的跳步规则deltaSide都写成参数
这样创建幻立方的通用方法就出来了
*/
func createCube(#step: Int, #point0:(row:Int, col:Int, deep:Int),#deltaPoint:(row:Int, col:Int, deep:Int),#deltaLine:(row:Int, col:Int, deep:Int),#deltaSide:(row:Int, col:Int, deep:Int)) -> ([[[Int]]])?{
let cube1 = [Int](count: step, repeatedValue: 0)
let cube2 = [[Int]](count: step, repeatedValue: cube1)
var cube = [[[Int]]](count: step, repeatedValue: cube2)
func deltaP(#pcurr:(row:Int, col:Int, deep:Int),#delta:(row:Int, col:Int, deep:Int),#step:Int)->(row:Int, col:Int, deep:Int){
var nextrow = pcurr.row + delta.row
var nextcol = pcurr.col + delta.col
var nextdeep = pcurr.deep + delta.deep
nextrow = correction(nextrow, step)
nextcol = correction(nextcol, step)
nextdeep = correction(nextdeep, step)
return (row: nextrow, col: nextcol, deep: nextdeep)
}
var p = point0
var iPut = 1
cube[p.row][p.col][p.deep] = iPut++
var time = step * step * step
do{
var nextp = deltaP(pcurr: p, delta: deltaPoint, step: step)
if (cube[nextp.row][nextp.col][nextp.deep] != 0){
nextp = deltaP(pcurr: p, delta: deltaLine, step: step)
if (cube[nextp.row][nextp.col][nextp.deep] != 0){
nextp = deltaP(pcurr: p, delta: deltaSide, step: step)
if (cube[nextp.row][nextp.col][nextp.deep] != 0){
if time < 2{
return cube
}else{
return nil
}
}
}
}
p = nextp
cube[p.row][p.col][p.deep] = iPut++
}while(time-- > 0)
return nil
}
//测试方法
func testCreatePerfectCube7(){
//测试一下我这个号称通用的方法能不能生成上述完美7阶幻立方
let perfectCube7 = createCube(step: 7, point0: (row: 4, col: 1, deep: 1), deltaPoint: (row: 4, col: 2, deep: 2), deltaLine: (row: 2, col: 4, deep: 0), deltaSide: (row: 2, col: 3, deep: 3))
printMagicCube(perfectCube7!)
println("\nisMagicCube 检查")
let line1 = isMagicCube(perfectCube7!,true)
if let line1_ = line1 {
println("不是幻立方,line=\(line1_)")
}
}
//testCreatePerfectCube7()
//看看下面这打印的,顿时觉得自己还真有点三脚猫的功夫
/*
第0层
[226,252,320,45,64,139,158]
[115,183,209,277,296,28,96]
[4,72,147,166,234,253,328]
[285,304,29,55,123,191,217]
[174,242,261,336,12,80,99]
[63,131,150,218,293,312,37]
[337,20,88,107,182,201,269]
第1层
[41,60,128,154,222,290,309]
[273,341,17,85,111,179,198]
[155,230,249,317,49,68,136]
[93,119,187,206,274,300,25]
[325,1,76,144,163,238,257]
[214,282,308,33,52,120,195]
[103,171,239,265,333,9,84]
第2层
[192,211,286,305,30,56,124]
[81,100,175,243,262,330,13]
[313,38,57,132,151,219,294]
[202,270,338,21,89,108,176]
[140,159,227,246,321,46,65]
[22,97,116,184,210,278,297]
[254,329,5,73,141,167,235]
第3层
[301,26,94,113,188,207,275]
[232,258,326,2,77,145,164]
[121,196,215,283,302,34,53]
[10,78,104,172,240,266,334]
[291,310,42,61,129,148,223]
[180,199,267,342,18,86,112]
[69,137,156,231,250,318,43]
第4层
[109,177,203,271,339,15,90]
[47,66,134,160,228,247,322]
[279,298,23,98,117,185,204]
[168,236,255,323,6,74,142]
[50,125,193,212,287,306,31]
[331,14,82,101,169,244,263]
[220,288,314,39,58,133,152]
第5层
[260,335,11,79,105,173,241]
[149,224,292,311,36,62,130]
[87,106,181,200,268,343,19]
[319,44,70,138,157,225,251]
[208,276,295,27,95,114,189]
[146,165,233,259,327,3,71]
[35,54,122,190,216,284,303]
第6层
[75,143,162,237,256,324,7]
[307,32,51,126,194,213,281]
[245,264,332,8,83,102,170]
[127,153,221,289,315,40,59]
[16,91,110,178,197,272,340]
[248,316,48,67,135,161,229]
[186,205,280,299,24,92,118]
isMagicCube 检查
经检查,行的和都是相等的
经检查,列的和都是相等的
经检查,深的和都是相等的
前视图的各条对角线的和相等
左视图的各条对角线的和相等
俯视图的各条对角线的和相等
经检查,主对角线和是相等的
经检查,辅对角线row和是相等的
经检查,辅对角线col和是相等的
经检查,辅对角线deep和是相等的
*/
/*
这个幻立方的牛B之处还不止这些,看看齐鲁晚报上是怎么说的
“这个七阶幻立方的主视图的七层,每层都是完美的。也就是说,每层的七行、七列、十四个斜行的七数之和都是幻和1204。而且第四层还是中心对称的,中心数是172,所有对称的两数之和都是344。
这个幻立方的整体也是中心对称的。”
*/