Skip to content

Instantly share code, notes, and snippets.

@bz0
Created September 17, 2019 09:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bz0/4680282bfd8787e1a3dd0496030dd8e4 to your computer and use it in GitHub Desktop.
Save bz0/4680282bfd8787e1a3dd0496030dd8e4 to your computer and use it in GitHub Desktop.
深さ優先探索
<?php
class DfsPractice{
private $size = 2;
private $count = 0;
public function exec(){
$this->dfs(0, 0);
echo sprintf("全部で%d通り", $this->count);
}
private function dfs(int $x, int $y){
if ($x == $this->size && $y == $this->size){
echo "カウント追加: size:" . $this->size . " x:" . $x . " y:" . $y . "\n";
$this->count++;
return;
}
if ($y<$this->size){
$this->dfs($x, $y + 1);
}
if ($x<$this->size){
$this->dfs($x + 1, $y);
}
}
}
$d = new DfsPractice();
$d->exec();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment