国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Java實現解出世界最難九宮格問題

2019-11-26 15:19:28
字體:
來源:轉載
供稿:網友

芬蘭數學家因卡拉花費3個月設計出了世界上迄今難度最大的數獨游戲,而且它只有一個答案。因卡拉說只有思考能力最快、頭腦最聰明的人才能破解這個游戲。

今日,一則騰訊的新聞稱中國老頭三天破解世界最難九宮格,雖然最后老人是改了一個數字,但是引起本人一時興趣,想通過計算機程序求解該問題,于是在宿舍呆了一下午,終于成功求解,程序源碼如下。

復制代碼 代碼如下:

package numberGame;

public class Point {
    private int col;// 行號
    private int row;// 列號
    private boolean flag;// 真為未設置。
    private int value;
    // 構造點
    public Point(int col, int row, boolean flag, int value) {
        super();
        this.col = col;
        this.row = row;
        this.flag = flag;
        this.value = value;
    }

    public void changeFlag() {
        flag = !flag;
    }

    public boolean getFlag() {
        return flag;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public boolean canHere(Point[][] pArr) {
        boolean cb = canCol(pArr);
        boolean cr = canRow(pArr);
        boolean cminiArr = canMiniArr(pArr);
        return cb && cr && cminiArr;
    }
    //判斷在小3*3格子里是否有相同元素
    private boolean canMiniArr(Point[][] pArr) {
        int coltemp = this.col % 3;
        int rowtemp = this.row % 3;

        for (int i = this.col - coltemp; i < col + (3 - coltemp); i++) {
            for (int j = this.row - rowtemp; j < row + (3 - rowtemp); j++) {
                if(i == this.col && j == this.row){
                    continue;
                }else{              
                    if(this.value == pArr[i][j].getValue()){
                        return false;
                    }               
                }
            }
        }
        return true;
    }

    // 判斷列上是否有相同元素
    private boolean canRow(Point[][] pArr) {
        for (int i = 0; i < 9; i++) {
            if (i == this.col) {
                continue;
            } else {
                if (this.value == pArr[i][this.row].value) {// 行變,列不變
                    return false;
                }
            }
        }
        return true;
    }

    // 判斷行上是否有相同元素
    private boolean canCol(Point[][] pArr) {
        for (int i = 0; i < 9; i++) {
            if (i == this.row) {
                continue;
            } else {
                if (this.value == pArr[this.col][i].value) {// 列邊,行不變
                    return false;
                }
            }
        }
        return true;
    }
}

主站蜘蛛池模板: 通辽市| 武威市| 安福县| 阿坝| 淮安市| 葵青区| 榆社县| 绥阳县| 靖江市| 松阳县| 雅江县| 明星| 陆丰市| 仙桃市| 江门市| 吉木萨尔县| 五寨县| 仙居县| 苍梧县| 海阳市| 滦平县| 茌平县| 响水县| 九江市| 翁源县| 鄂托克旗| 和硕县| 丹棱县| 瓮安县| 玉山县| 石台县| 连城县| 天津市| 台中县| 托克逊县| 来凤县| 蒙城县| 本溪市| 常德市| 陇川县| 永州市|