Created
September 17, 2019 09:20
-
-
Save bz0/4680282bfd8787e1a3dd0496030dd8e4 to your computer and use it in GitHub Desktop.
深さ優先探索
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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