數獨求解程式 PHP暴力求解

2021-09-19 05:01:40 字數 3314 閱讀 8675

<?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++)}}

return true;

}function removevalue($row, $col, $value)

if (isset($this->matrix[$row][$col][$value]))

}return true;

}function set($arr) }}

}function dump() else

}echo php_eol;

}echo php_eol;

}function dumpall()

echo php_eol;

}echo php_eol;

}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;

//$t = microtime(true);

//$sd->calc(array(

// array(0,5,0,0,0,6,0,9,0),

// array(0,4,7,0,8,2,6,0,0),

// array(0,8,0,0,0,7,0,5,2),

// array(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),

//));

//$t2 = microtime(true);

//echo ($t2 - $t) .php_eol;

//$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),

//));

//芬蘭數學家因卡拉花費3個月設計出了世界上迄今難度最大的九宮格遊戲,而且它只有乙個答案

$t = microtime(true);

$sd->calc(array(

array(8,0,0,0,0,0,0,0,0),

array(0,0,3,6,0,0,0,0,0),

array(0,7,0,0,9,0,2,0,0),

array(0,5,0,0,0,7,0,0,0),

array(0,0,0,0,4,5,7,0,0),

array(0,0,0,1,0,0,0,3,0),

array(0,0,1,0,0,0,0,6,8),

array(0,0,8,5,0,0,0,1,0),

array(0,9,0,0,0,0,4,0,0),

));$t2 = microtime(true);

echo ($t2 - $t) .php_eol;

輸出結果:

8 1 2 7 5 3 6 4 9 

9 4 3 6 8 2 1 7 5

6 7 5 4 9 1 2 8 3

1 5 4 2 3 7 8 9 6

3 6 9 8 4 5 7 2 1

2 8 7 1 6 9 5 3 4

5 2 1 9 7 4 3 6 8

4 3 8 5 2 6 9 1 7

7 9 6 3 1 8 4 5 2

0.50202894210815

列舉暴力求解

題目描述 有n個賭徒打算賭一局。規則是 每人下乙個賭注,賭注為非負整數,且任意兩個賭注都不相同。勝者為賭注恰好是其餘任意三個人的賭注之和的那個人。如果有多個勝者,我們取賭注最大的那個為最終勝者。例如,a,b,c,d,e分別下賭注為2 3 5 7 12,最終勝者是e,因為12 2 3 7。輸入輸入包含...

暴力求解總結

輸入正整數n,按從小到大的順序輸出所有形如abcde fghij n的表示式,其中a j恰好為數字0 的乙個排列 可以有前導0 2 n 79。樣例輸入 62 樣例輸出 79546 01283 62 94736 01528 62 分析 分成兩部分加一些細節處理。列舉數字 分母 至少是四位數,從1000...

暴力求解法

1 簡單列舉 輸入正整數n,按從小到大的順序輸出所有形如abcde fghij n的表示式,其中a j恰好位數字0 9的乙個排序 可以有前導0 2 n 79.樣例輸入 樣例輸出 79546 01283 62 94736 01528 62 思路 只要列舉fghij就可以算出abcde,然後判斷所有數字...