Advertisement
villers

count_island

Jun 7th, 2014
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.38 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Web@cademie Lyon, login: viller_m
  5.  * En gros pour l'utiliser dans la fonction count_island.
  6.  * il faut mettre le contenue du fichier dans une map (tab[y][x])
  7.  * puis il faut boucler dessus normalement
  8.  * et des que un X est trouvé il faut rentrer dans la fonction récursif
  9.  * puis continuer à boucler jusqu'à la fin de la map
  10. **/
  11.  
  12.  
  13. /**
  14.  * @function recursif
  15.  *
  16.  * @param string &$tab  contient une map du fichier
  17.  * & = passage par référence ( http://www.php.net/manual/fr/language.references.pass.php )
  18.  * @param int $x        position du caractère en x
  19.  * @param int $y        position du caractère en y
  20.  * @param int $number       numero de l'ilo
  21. **/
  22. function recursif(&$tab, $x, $y, $number)
  23. {
  24.     if($tab[$y][$x] != '*')
  25.         return;
  26.  
  27.     $tab[$y][$x] = "$number";
  28.  
  29.     if($y + 1 < count($tab))
  30.         recursif($tab, $x, $y + 1, $number);
  31.  
  32.     if($y - 1 >= 0)
  33.         recursif($tab, $x, $y - 1, $number);
  34.  
  35.     if($x + 1 < count($tab[$y]))
  36.         recursif($tab, $x + 1, $y, $number);
  37.  
  38.     if($x - 1 >= 0)
  39.         recursif($tab, $x - 1, $y, $number);
  40. }
  41.  
  42. // Exemple de map:
  43. $map = array(
  44.     array('x','x','x','*','*','*','x','x','x','*','*','x','x'),
  45.     array('x','x','*','*','*','x','x','x','x','x','x','x','x'),
  46.     array('x','x','x','x','x','x','x','x','x','x','x','x','x'),
  47.     array('x','x','x','x','x','*','*','x','x','x','x','x','x'),
  48.     array('x','x','x','x','x','x','x','x','x','x','x','x','x'),
  49. );
  50.  
  51. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement