PHP樹(shù)的深度編歷生成迷宮及A*自動(dòng)尋路算法實(shí)例分析_PHP教程
推薦:PHP實(shí)現(xiàn)扎金花游戲之大小比賽的方法這篇文章主要介紹了PHP實(shí)現(xiàn)扎金花游戲之大小比賽的方法,實(shí)例分析了扎金花游戲的實(shí)現(xiàn)原理與相關(guān)算法技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下 本文實(shí)例講述了PHP實(shí)現(xiàn)扎金花游戲之大小比賽的方法。分享給大家供大家參考。具體分析如下: 程序離不開(kāi)算法,前面討論
這篇文章主要介紹了PHP樹(shù)的深度編歷生成迷宮及A*自動(dòng)尋路算法,實(shí)例分析了php實(shí)現(xiàn)A*尋路算法的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了PHP樹(shù)的深度編歷生成迷宮及A*自動(dòng)尋路算法。分享給大家供大家參考。具體分析如下:
有一同事推薦了三思的迷宮算法,看了感覺(jué)還不錯(cuò),就轉(zhuǎn)成php
三思的迷宮算法是采用樹(shù)的深度遍歷原理,這樣生成的迷宮相當(dāng)?shù)募?xì),而且死胡同數(shù)量相對(duì)較少!
任意兩點(diǎn)之間都存在唯一的一條通路。
至于A(yíng)*尋路算法是最大眾化的一全自動(dòng)尋路算法
廢話(huà)不多說(shuō),貼上帶代碼
迷宮生成類(lèi):
代碼如下: class Maze{// Maze Create
private $_w;
private $_h;
private $_grids;
private $_walkHistory;
private $_walkHistory2;
private $_targetSteps;
// Construct
public function Maze() {
$this->_w = 6;
$this->_h = 6;
$this->_grids = array();
}
// 設(shè)置迷宮大小
public function set($width = 6, $height = 6) {
if ( $width > 0 ) $this->_w = $width;
if ( $height > 0 ) $this->_h = $height;
return $this;
}
// 取到迷宮
public function get() {
return $this->_grids;
}
// 生成迷宮
public function create() {
$this->_init();
return $this->_walk(rand(0, count($this->_grids) -1 ));
}
// 獲取死胡同點(diǎn)
public function block($n = 0, $rand = false) {
$l = count($this->_grids);
for( $i = 1; $i < $l; $i++ ) {
$v = $this->_grids[$i];
if ( $v == 1 || $v == 2 || $v == 4 || $v == 8 ) {
$return[] = $i;
}
}
// 隨機(jī)取點(diǎn)
if ( $rand ) shuffle($return);
if ( $n == 0 ) return $return;
if ( $n == 1 ) {
return array_pop($return);
} else {
return array_slice($return, 0, $n);
}
}
/**
|---------------------------------------------------------------
| 生成迷宮的系列函數(shù)
|---------------------------------------------------------------
*/
private function _walk($startPos) {
$this->_walkHistory = array();
$this->_walkHistory2 = array();
$curPos = $startPos;
while ($this->_getNext0() != -1) {
$curPos = $this->_step($curPos);
if ( $curPos === false ) break;
}
return $this;
}
private function _getTargetSteps($curPos) {
$p = 0;
$a = array();
$p = $curPos - $this->_w;
if ($p > 0 && $this->_grids[$p] === 0 && ! $this->_isRepeating($p)) {
array_push($a, $p);
} else {
array_push($a, -1);
}
$p = $curPos + 1;
if ($p % $this->_w != 0 && $this->_grids[$p] === 0 && ! $this->_isRepeating($p)) {
array_push($a, $p);
} else {
array_push($a, -1);
}
$p = $curPos + $this->_w;
if ($p < count($this->_grids) && $this->_grids[$p] === 0 && ! $this->_isRepeating($p)) {
array_push($a, $p);
} else {
array_push($a, -1);
}
$p = $curPos - 1;
if (($curPos % $this->_w) != 0 && $this->_grids[$p] === 0 && ! $this->_isRepeating($p)) {
array_push($a, $p);
} else {
array_push($a, -1);
}
return $a;
}
private function _noStep() {
$l = count($this->_targetSteps);
for ($i = 0; $i < $l; $i ++) {
if ($this->_targetSteps[$i] != -1) return false;
}
return true;
}
private function _step($curPos) {
$this->_targetSteps = $this->_getTargetSteps($curPos);
if ( $this->_noStep() ) {
if ( count($this->_walkHistory) > 0 ) {
$tmp = array_pop($this->_walkHistory);
} else {
return false;
}
array_push($this->_walkHistory2, $tmp);
return $this->_step($tmp);
}
$r = rand(0, 3);
while ( $this->_targetSteps[$r] == -1) {
$r = rand(0, 3);
}
$nextPos = $this->_targetSteps[$r];
$isCross = false;
if ( $this->_grids[$nextPos] != 0)
$isCross = true;
if ($r == 0) {
$this->_grids[$curPos] ^= 1;
$this->_grids[$nextPos] ^= 4;
} elseif ($r == 1) {
$this->_grids[$curPos] ^= 2;
$this->_grids[$nextPos] ^= 8;
} elseif ($r == 2) {
$this->_grids[$curPos] ^= 4;
$this->_grids[$nextPos] ^= 1;
} elseif ($r == 3) {
$this->_grids[$curPos] ^= 8;
$this->_grids[$nextPos] ^= 2;
}
array_push($this->_walkHistory, $curPos);
return $isCross ? false : $nextPos;
}
private function _isRepeating($p) {
$l = count($this->_walkHistory);
for ($i = 0; $i < $l; $i ++) {
if ($this->_walkHistory[$i] == $p) return true;
}
$l = count($this->_walkHistory2);
for ($i = 0; $i < $l; $i ++) {
if ($this->_walkHistory2[$i] == $p) return true;
}
return false;
}
private function _getNext0() {
$l = count($this->_grids);
for ($i = 0; $i <= $l; $i++ ) {
if ( $this->_grids[$i] == 0) return $i;
}
return -1;
}
private function _init() {
$this->_grids = array();
for ($y = 0; $y < $this->_h; $y ++) {
for ($x = 0; $x < $this->_w; $x ++) {
array_push($this->_grids, 0);
}
}
return $this;
}
}
分享:php獲取本周開(kāi)始日期和結(jié)束日期的方法這篇文章主要介紹了php獲取本周開(kāi)始日期和結(jié)束日期的方法,實(shí)例分析了php操作日期的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下 本文實(shí)例講述了php獲取本周開(kāi)始日期和結(jié)束日期的方法。分享給大家供大家參考。具體如下: 代碼如下://當(dāng)前日期 $sdefaultDate = date(
- PHP實(shí)現(xiàn)扎金花游戲之大小比賽的方法
- php獲取本周開(kāi)始日期和結(jié)束日期的方法
- php數(shù)組轉(zhuǎn)成json格式的方法
- php實(shí)現(xiàn)將數(shù)組轉(zhuǎn)換為XML的方法
- php返回字符串中所有單詞的方法
- php通過(guò)正則表達(dá)式記取數(shù)據(jù)來(lái)讀取xml的方法
- PHP實(shí)現(xiàn)算式驗(yàn)證碼和漢字驗(yàn)證碼實(shí)例
- PHP實(shí)現(xiàn)指定字段的多維數(shù)組排序函數(shù)分享
- PHP多線(xiàn)程之內(nèi)部多線(xiàn)程實(shí)例分析
- php建立Ftp連接的方法
- Thinkphp調(diào)用Image類(lèi)生成縮略圖的方法
- PHP實(shí)現(xiàn)懶加載的方法
PHP教程Rss訂閱編程教程搜索
PHP教程推薦
- 解讀使用PHP與XML進(jìn)行網(wǎng)站編程
- 使用PHP生成1000個(gè)隨機(jī)注冊(cè)碼
- PHP導(dǎo)出Excel 之 Spreadsheet_Excel_Writer
- PHP Error與Logging函數(shù)的深入理解
- 解析link_mysql的php版
- PHP的包含文件函數(shù)require和include路徑總結(jié)
- php有規(guī)律大文件的讀取與寫(xiě)入
- 淺析Dos下運(yùn)行php.exe,出現(xiàn)沒(méi)有找到php_mbstring.dll 錯(cuò)誤的解決方法
- 壞狼的php學(xué)習(xí)第3天
- php設(shè)計(jì)模式之單例模式實(shí)例分析
- 相關(guān)鏈接:
復(fù)制本頁(yè)鏈接| 搜索PHP樹(shù)的深度編歷生成迷宮及A*自動(dòng)尋路算法實(shí)例分析
- 教程說(shuō)明:
PHP教程-PHP樹(shù)的深度編歷生成迷宮及A*自動(dòng)尋路算法實(shí)例分析
。