PHP ツリーの深いカレンダー生成迷路と A* 自動経路探索アルゴリズムの分析例、迷路例分析
この記事では、PHP ツリーの深いカレンダー生成迷路と A* 自動経路探索アルゴリズムについて説明します。参考のためにみんなで共有してください。具体的な分析は次のとおりです:
同僚が Sansi の迷路アルゴリズムを勧めてくれたので、読んでとても良いと思ったので、PHP に変換しました。
Sansi の迷路アルゴリズムはツリー深度トラバースの原理を使用しており、この方法で生成される迷路は非常に薄く、行き止まりの数は比較的少ないです。
任意の 2 点間にはパスが 1 つだけあります。
これ以上ナンセンスは不要です。コードを投稿するだけです
迷路生成クラス:
コードをコピーします コードは次のとおりです: class Maze{
//迷路作成
プライベート $_w;
プライベート $_h;
プライベート $_grid;
プライベート $_walkHistory;
プライベート $_walkHistory2;
プライベート $_targetSteps;
// 構築
パブリック関数 Maze() {
$this->_w = 6;
$this->_h = 6;
$this->_grids = array();
}
//迷路のサイズを設定します
パブリック関数セット($width = 6, $height = 6) {
If ( $width > 0 ) $this->_w = $width;
If ( $height > 0 ) $this->_h = $height;
$this を返します;
}
// 迷路を手に入れましょう
パブリック関数 get() {
return $this->_grids;
}
// 迷路を生成します
パブリック関数 create() {
$this->_init();
return $this->_walk(rand(0, count($this->_grids) -1 ));
}
// 行き止まり点を取得します
パブリック関数ブロック($n = 0, $rand = false) {
$l = count($this->_grids);
for( $i = 1; $i
$v = $this->_grids[$i];
If ( $v == 1 || $v == 2 || $v == 4 || $v == 8 ) {
$return[] = $i;
}
}
// ランダムにポイントを選択します
if ( $rand ) shuffle($return);
if ( $n == 0 ) return $return;
if ( $n == 1 ) {
return array_pop($return);
} else {
return array_slice($return, 0, $n);
}
}
/**
|------------------------------------------------- ---------------
| 迷路を生成する一連の関数
|------------------------------------------------- ---------------
*/
プライベート関数 _walk($startPos) {
$this->_walkHistory = array();
$this->_walkHistory2 = array();
$curPos = $startPos;
while ($this->_getNext0() != -1) {
$curPos = $this->_step($curPos);
If ( $curPos === false ) Break;
}
$this を返します;
}
プライベート関数 _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);
} その他 {
array_push($a, -1);
}
$p = $curPos + $this->_w;
if ($p _grids) && $this->_grids[$p] === 0 && ! $this->_isRepeating($p)) {
array_push($a, $p);
} その他 {
array_push($a, -1);
}
$p = $curPos - 1;
if (($curPos % $this->_w) != 0 && $this->_grids[$p] === 0 && ! $this->_isRepeating($p)) {
array_push($a, $p);
} その他 {
array_push($a, -1);
}
$a を返します;
}
プライベート関数 _noStep() {
$l = count($this->_targetSteps);
for ($i = 0; $i
if ($this->_targetSteps[$i] != -1) return false;
}
true を返します;
}
プライベート関数 _step($curPos) {
$this->_targetSteps = $this->_getTargetSteps($curPos);
if ( $this->_noStep() ) {
if ( count($this->_walkHistory) > 0 ) {
$tmp = array_pop($this->_walkHistory);
} その他 {
false を返します;
}
array_push($this->_walkHistory2, $tmp);
return $this->_step($tmp);
}
$r = ランド(0, 3);
while ( $this->_targetSteps[$r] == -1) {
$r = ランド(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);
$isCross を返しますか? false : $nextPos;
}
プライベート関数 _isRepeating($p) {
$l = count($this->_walkHistory);
for ($i = 0; $i
if ($this->_walkHistory[$i] == $p) return true;
}
$l = count($this->_walkHistory2);
for ($i = 0; $i
if ($this->_walkHistory2[$i] == $p) return true;
}
false を返します;
}
プライベート関数 _getNext0() {
$l = count($this->_grids);
for ($i = 0; $i
if ( $this->_grids[$i] == 0) return $i;
}
-1 を返します;
}
プライベート関数 _init() {
$this->_grids = array();
for ($y = 0; $y _h; $y ++) {
for ($x = 0; $x _w; $x ++) {
array_push($this->_grids, 0);
}
}
$this を返します;
}
}
A*寻路算法
复制代码代码如下:class AStar{
// Aスター
プライベート $_open;
プライベート $_closed;
プライベート $_start;
プライベート $_end;
プライベート $_grid;
プライベート$_w;
プライベート $_h;
// 構築
パブリック関数 AStar(){
$this->_w = null;
$this->_h = null;
$this->_grids = null;
}
public function set($width, $height, $grids) {
$this->_w = $width;
$this->_h = $height;
$this->_grids = $grids;
$this を返します;
}
// 迷宫中寻路
public function search($start = false, $end = false) {
return $this->_search($start, $end);
}
/**
|------------------------------------------------- ---------------
| 自動経路検索 - A-star アルゴリズム
|------------------------------------------------- ---------------
*/
public function _search($start = false, $end = false) {
if ( $start !== false ) $this->_start = $start;
if ( $end !== false ) $this->_end = $end;
$_sh = $this->_getH($start);
$point['i'] = $start;
$point['f'] = $_sh;
$point['g'] = 0;
$point['h'] = $_sh;
$point['p'] = null;
$this->_open[] = $point;
$this->_closed[$start] = $point;
while ( 0
$minf = false;
foreach( $this->_open as $key => $maxNode ) {
if ( $minf === false || $minf > $maxNode['f'] ) {
$minIndex = $key;
}
}
$nowNode = $this->_open[$minIndex];
unset($this->_open[$minIndex]);
if ( $nowNode['i'] == $this->_end ) {
$tp = 配列();
while( $nowNode['p'] !== null ) {
array_unshift($tp, $nowNode['p']);
$nowNode = $this->_closed[$nowNode['p']];
}
array_push($tp, $this->_end);
休憩;
}
$this->_setPoint($nowNode['i']);
}
$this->_closed = array();
$this->_open = array();
$tp を返します;
}
プライベート関数 _setPoint($me) {
$point = $this->_grids[$me];
// すべての選択可能な方向入力队列
if ( $point & 1 ) {
$next = $me - $this->_w;
$this->_checkPoint($me, $next);
}
if ( $point & 2 ) {
$next = $me + 1;
$this->_checkPoint($me, $next);
}
if ( $point & 4 ) {
$next = $me + $this->_w;
$this->_checkPoint($me, $next);
}
if ( $point & 8 ) {
$next = $me - 1;
$this->_checkPoint($me, $next);
}
}
プライベート関数 _checkPoint($pNode, $next) {
if ( $this->_closed[$next] ) {
$_g = $this->_closed[$pNode]['g'] + $this->_getG($next);
if ( $_g
$this->_closed[$next]['g'] = $_g;
$this->_closed[$next]['f'] = $this->_closed[$next]['g'] + $this->_closed[$next]['h'];
$this->_closed[$next]['p'] = $pNode;
}
} その他 {
$point['p'] = $pNode;
$point['h'] = $this->_getH($next);
$point['g'] = $this->_getG($next);
$point['f'] = $point['h'] + $point['g'];
$point['i'] = $next;
$this->_open[] = $point;
$this->_closed[$next] = $point;
}
}
プライベート関数 _getG($point) {
return abs($this->_start - $point);
}
プライベート関数 _getH($point) {
return abs($this->_end - $point);
}
}
完全なサンプル コード ポイントはここからダウンロードできます。
必要があります大家は直接デモを行うことができます、看看効果!

tomodifydatainaphpsession、starthessession withsession_start()、$ _sessiontoset、modify、orremovevariables.1)startthessession.2)

配列はPHPセッションに保存できます。 1。セッションを開始し、session_start()を使用します。 2。配列を作成し、$ _Sessionで保存します。 3. $ _Sessionを介して配列を取得します。 4.セッションデータを最適化してパフォーマンスを向上させます。

PHPセッションガベージコレクションは、有効期限が切れたセッションデータをクリーンアップするために確率メカニズムを通じてトリガーされます。 1)構成ファイルにトリガー確率とセッションのライフサイクルを設定します。 2)Cronタスクを使用して、高負荷アプリケーションを最適化できます。 3)データの損失を避けるために、ごみ収集の頻度とパフォーマンスのバランスを取る必要があります。

PHPでのユーザーセッションアクティビティの追跡は、セッション管理を通じて実装されます。 1)SESSION_START()を使用してセッションを開始します。 2)$ _Sessionアレイを介してデータを保存およびアクセスします。 3)セッションを終了するには、session_destroy()を呼び出します。セッショントラッキングは、ユーザーの動作分析、セキュリティ監視、パフォーマンスの最適化に使用されます。

データベースを使用してPHPセッションデータを保存すると、パフォーマンスとスケーラビリティが向上します。 1)MySQLを構成してセッションデータを保存します:PHP.iniまたはPHPコードでセッションプロセッサを設定します。 2)カスタムセッションプロセッサを実装します:データベースと対話するために、開いて、閉じ、読み取り、書き込み、その他の機能を定義します。 3)最適化とベストプラクティス:インデックス、キャッシュ、データ圧縮、分散ストレージを使用して、パフォーマンスを向上させます。

phpssionsStrackuserdataacrossmultiplepagerequestsusingauniqueidstoredinacookie.here'showtomanageetheemefectively:1)Startassession withsession_start()andstoredatain $ _ session.2)RegeneratesseSsessidafterloginwithsession_id(the topreventes_id)

PHPでは、次の手順を通じてセッションデータを繰り返すことができます。1。session_start()を使用してセッションを開始します。 2。$ _Sessionアレイのすべてのキー価値ペアを介してforeachループを反復します。 3.複雑なデータ構造を処理する場合、is_array()またはis_object()関数を使用し、print_r()を使用して詳細情報を出力します。 4.トラバーサルを最適化する場合、ページングを使用して、一度に大量のデータの処理を避けることができます。これにより、実際のプロジェクトでPHPセッションデータをより効率的に管理および使用するのに役立ちます。

このセッションは、サーバー側の状態管理メカニズムを介してユーザー認証を実現します。 1)セッションの作成と一意のIDの生成、2)IDはCookieを介して渡されます。3)サーバーストアとIDを介してセッションデータにアクセスします。


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

Safe Exam Browser
Safe Exam Browser は、オンライン試験を安全に受験するための安全なブラウザ環境です。このソフトウェアは、あらゆるコンピュータを安全なワークステーションに変えます。あらゆるユーティリティへのアクセスを制御し、学生が無許可のリソースを使用するのを防ぎます。

メモ帳++7.3.1
使いやすく無料のコードエディター

SAP NetWeaver Server Adapter for Eclipse
Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

SublimeText3 中国語版
中国語版、とても使いやすい

EditPlus 中国語クラック版
サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

ホットトピック









