一、數獨問題描述:
對於給出的數字二維陣列,要求每行每列的數字不能重複。
二、實現**:
<?php /* 數獨求解程式
* created on 2017-4-18
* */
class sudoku else
} function clear() }}
} function setcell($row, $col, $value)}}
//col
for($i = 0; $i < 9; $i++)}}
//square
$rs=intval($row / 3) * 3;
$cs=intval($col / 3) * 3;
for($i = $rs; $i < $rs + 3; $i++)}}
return true;
} function removevalue($row, $col, $value)
if (isset($this->matrix[$row][$col][$value]))
}程式設計客棧 return true;
} function set($arr) }}
} function dump() else
}echo "\n";
}echo "\n";
} function dumpall()
echo "\n";
}echo "\n";
} function calc($data)
function _calc()
foreach($this->matrix[$i][$j] as $v)
if(!$t->_calc())
$this->matrix = $t->matrix;
return true;
}return false;}}
return true;
}}$sd=new sudoku;
$sd->calc(array(
array(0,5,0,0,0,6,0,9,0),
array(0,4,7,0,8,2,6,0,0),
aarray(7,0,1,0,3,4,0,0,6),
array(0,3,0,0,2,0,0,8,0),
array(2,0,0,0,0,1,9,0,4),
array(4,7,0,1,0,0,0,6,0),
array(0,0,9,4,6,0,3,7,0),
array(0,1,0,2,0,0,0,4,0),
));$sd->calc(array(
array(1,0,0,0,0,6,9,0,0),
array(0,0,0,9,0,0,0,0,5),
array(2,0,0,1,0,0,0,0,3),
array(0,0,5,3,0,7,0,2,0),
array(3,0,0,6,0,0,0,0,1),
array(0,1,0,4,0,0,8,0,0),
array(9,0,0,0,0,2,0,0,7),
array(5,0,0,0,0,9,0,0,0),
array(0,0,3,7,0,0,0,0,4),
));$sd->calc(array(
array(7,0,0,1,0,0,0,0,5),
array(0,0,6,0,4,0,0,8,0),
array(0,0,1,0,0,0,0,0,0),
array(0,6,0,0,8,0,0,0,3),
array(0,8,0,0,0,9,0,7,0),
array(1,0,0,0,0,0,0,5,0),
array(0,0,0,0,0,0,9,0,0),
array(0,4,0,0,3,0,1,0,0),
array(9,0,0,0,0,7,0,0,2),
));$sd->calc(array(
array(0,5,0,0,0,0,0,2,0),
array(0,0,3,1,0,0,5,0,0),
array(0,0,6,0,0,8,0,0,0),
array(6,0,0,0,0,0,0,1,0),
array(8,0,0,6,0,0,0,0,4),
array(0,3,0,0,0,9,0,0,7),
array(0,0,0,5,0,0,3,0,0),
array(0,0,8,0,0,6,9,0,0),
array(0,9,0,0,0,0,0,7,0),
));?>
執行結果如下:
1 5 2 3 4 6 7 9 8
9 4 7 5 8 2 6 1 3
3 8 6 9 1 7 4 5 2
7 9 1 8 3 4 5 2 6
5 3 4 6 2 9 1 8 7
2 6 8 7 5 1 9 3 4
4 7 3 1 9 8 2 6 5
8 2 9 4 6 5 3 7 1
6 1 5 2 7 3 8 4 9
1 3 7 2 5 6 9 4 8
4 6 8 9 7 3 2 1 5
2 5 9 1 8 4 6 7 3
6 8 5 3 1 7 4 2 9
3 9 4 6 2 8 7 5 1
7 1 2 4 9 5 8 3 6
9 4 6 5 3 2 1 8 7
5 7 1 8 4 9 3 6 2
8 2 3 7 6 1 5 9 4
7 3 8 1 9 6 4 2 5
2 9 6 3 4 5 7 8 1
4 5 1 2 7 8 3 9 6
5 6 9 7 8 4 2 1 3
3 8 2 5 1 9 6 7 4
1 7 4 6 2 3 8 5 9
6 2 7 4 5 1 9 3 8
8 4 5 9 3 2 1 6 7
9 1 3 8 6 7 5 4 2
9 5 1 3 6 7 4 2 8
7 8 3 1 4 2 5 6 9
2 4 6 9 5 8 7 3 1
6 2 9 4 7 5 8 1 3
8 7 5 6 1 3 2 9 4
1 3 4 2 8 9 6 5 7
4 6 7 5 9 1 3 8 2
3 1 8 7 2 6 9 4 5
5 9 2 8 3 4 1 7 6
數獨求解程式 PHP暴力求解
數獨求解程式 class sudoku else function clear function setcell row,col,value col for i 0 i 9 i square rs intval row 3 3 cs intval col 3 3 for i rs i rs 3 i ...
經典數獨遊戲 數獨求解器 純C語言實現
心常樂數獨小遊戲 下面簡稱 本軟體 是一款windows平台下的數獨遊戲軟體。本軟體是開源 免費軟體。本軟體 使用純c語言編寫,mingw編譯,nsis打包。本軟體主要特性例如以下 支援 闖關模式 和 選關模式 支援 隨機生成遊戲 支援數獨遊戲提示 支援使用者對單元格做標記 提供超級工具 數獨求解器...
數獨問題求解二 解題思路(1)
首先對需求中的任務一步一步進行分析 1 題目要求生成不重複的終局至檔案,即首先要知道可生成的數獨終局的上限,該問題在後面詳述。將終局輸出至檔案需要涉及到相關函式,該問題較為簡單。2 讀取檔案內的數獨,關鍵在於數獨格式的讀取,因為檔案內數獨格式固定且已知,只需要根據格式編寫相關迴圈使得資料被讀取時考慮...