# 幻方算法

1、总算法

2、四阶幻方算法

3、奇数阶幻方算法

4、偶数阶幻方算法

function createMagicSquare(factor,firstNum) {    var component = {};    //阶数    if(factor < 3){        alert("幻方阶数必须大于3");        return null;    }    component.factor = factor;    component.rows = new Array();  //    if(isNaN(firstNum))        firstNum = 1;    component.firstNum = firstNum;    //return;    (function () {   //初始化自执行        for(var i = 0;i <component.factor;i++ ){            var  numPerRow= new Array();            for(var j = 0;j <component.factor;j++){                numPerRow.push(0);            }            component.rows.push(numPerRow);        }    }());    //return;    //幻方平衡    component.balance = function () {        if(component.factor == 4){            _balance4(component);        }        else if(component.factor % 2 == 0){            _balanceEven(component);        }        else{            _balanceOdd(component);        }    };    //偶数阶幻方平衡    function _balanceEven(magicEvem){        var factor = magicEvem.factor;        var firstNum = magicEvem.firstNum;        if(factor%2 == 0){            //分区域算平衡            var sonFactor = factor/2;            var areaA = createMagicSquare(sonFactor,firstNum);            areaA.balance();            //areaA.show();            var areaB = createMagicSquare(sonFactor,firstNum + sonFactor * sonFactor);            areaB.balance();            //areaB.show();            var areaC = createMagicSquare(sonFactor,firstNum + 2*sonFactor * sonFactor);            areaC.balance();            //areaC.show();            var areaD = createMagicSquare(sonFactor,firstNum + 3*sonFactor * sonFactor);            areaD.balance();            //areaD.show();            //A,B,C,D 平衡算好后，交换数字            if(sonFactor%2 ==0){                //A <----> D相应位置交换，每行的交换方格数为subFactor/2                var  exchangeNum = sonFactor/2;                for(var i = 0; i < sonFactor; i++){                    for( j =0; j < exchangeNum;j++){                        // A<---->D                        var temp = areaA.rows[i][j];                        areaA.rows[i][j] = areaD.rows[i][j];                        areaD.rows[i][j] = temp;                        // C<---->B                        var temp = areaC.rows[i][j];                        areaC.rows[i][j] = areaB.rows[i][j];                        areaB.rows[i][j] = temp;                    }                }            }            else{                var  exchangeNum = (sonFactor - 1)/2;                for(var i = 0; i < sonFactor; i++){                    for( j =0; j < exchangeNum;j++){                        // A<---->D                        var temp = areaA.rows[i][j];                        areaA.rows[i][j] = areaD.rows[i][j];                        areaD.rows[i][j] = temp;                        if(j < exchangeNum - 1) {                            // C<---->B                            var temp = areaC.rows[i][j];                            areaC.rows[i][j] = areaB.rows[i][j];                            areaB.rows[i][j] = temp;                        }                    }                }            }            //交换完了，要把数字连成一片,连接A区            for(i = 0; i<sonFactor; i++){                var currentRow = magicEvem.rows[i];                for(j = 0; j<sonFactor; j++){                    currentRow[j] = areaA.rows[i][j];                }            }            delete areaA.rows;            //连接C区            for(i = 0; i<sonFactor; i++){                var currentRow = magicEvem.rows[i];                for(j = 0; j<sonFactor; j++){                    currentRow[sonFactor + j] = areaC.rows[i][j];                }            }            delete areaC.rows;            //连接D区            for(i = 0; i<sonFactor; i++){                var currentRow = magicEvem.rows[i + sonFactor];                for(j = 0; j<sonFactor; j++){                    currentRow[j] = areaD.rows[i][j];                }            }            delete areaD.rows;            //连接B区            for(i = 0; i<sonFactor; i++){                var currentRow = magicEvem.rows[i + sonFactor];                for(j = 0; j<sonFactor; j++){                    currentRow[sonFactor + j] = areaB.rows[i][j];                }            }            delete areaB.rows;        }    };    //奇数阶幻方平衡    function _balanceOdd (magicOdd){        if(magicOdd.factor%2 == 0)        {            alert("必须是奇数阶");            return;        }        var row = 0;        var col = (magicOdd.factor - 1)/2;        var currentNum = magicOdd.firstNum;        magicOdd.rows[row][col] = currentNum;        //document.write("<br/> factor: " +  magicOdd.factor + " row:" + (row +1) + " col:" + (col +1) + " --- " + currentNum);        while(currentNum < magicOdd.firstNum + magicOdd.factor * magicOdd.factor - 1)        {            currentNum++;            row = row -1;            col = col + 1;            if(row < 0 )            {                if(col >= magicOdd.factor) {  // 在本幻方外部的右上角                    row = 1;                    col = col - 1;                }                else                {                    row = magicOdd.factor - 1;                }            }            else            {                if(col >= magicOdd.factor) { //仅仅超出右边界                    col = 0;                }                else                {                    //判断格子有没有被占                    if(magicOdd.rows[row][col] != 0){                        row = row +2;                        col = col -1;                    }                }            }            if(currentNum>=70)                debugger;            //document.write("<br/>row:" + (row +1) + " col:" + (col +1) + " --- " + currentNum);            magicOdd.rows[row][col] = currentNum;        }    };    //四阶幻方    //startx  ==    function _balance4(magic4) {        if(magic4.factor != 4)        {            alert("必须是4阶");        }        for(var i = 0;i <4;i++ ){            var currentRow = component.rows[i];            for(var j = 0;j <4;j++){                currentRow[j] = component.firstNum + i * component.factor + j;            }        }        var temp = magic4.rows[3][3];  //本四阶里最后一行最后一个数字        magic4.rows[3][3] = magic4.rows[0][0];        magic4.rows[0][0] =  temp;        temp = magic4.rows[3][0];        magic4.rows[3][0] = magic4.rows[0][3];        magic4.rows[0][3] =  temp;        temp = magic4.rows[1][1];        magic4.rows[1][1] = magic4.rows[2][2];        magic4.rows[2][2] =  temp;        temp = magic4.rows[1][2];        magic4.rows[1][2] = magic4.rows[2][1];        magic4.rows[2][1] =  temp;    }    component.show = function () {        document.write("<table style='border-width: 1px'>")        for(var i = 0; i< component.factor;i++){            document.write("<tr>")            for(var j = 0; j< component.factor;j++){                document.write("<td>")                document.write(component.rows[i][j]);                document.write("</td>")            }            document.write("</tr>")        }        document.write("</table>")    }    return component;}
posted on 2017-11-30 21:35  Shark Xu  阅读(985)  评论(0编辑  收藏