Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Wieże Hanoi, Klasa, PHP 5
dr_bonzo
post
Post #1





Grupa: Przyjaciele php.pl
Postów: 5 724
Pomógł: 259
Dołączył: 13.04.2004
Skąd: N/A

Ostrzeżenie: (0%)
-----


Co to robi? Rozwiazuje "Wieze Hanoi", uzywa minimalnej liczby ruchow i pokazuje je kolejno.

PHP5 REQUIRED

  1. <pre><?php
  2. class HanoiStack
  3. {
  4.     private $arrStack = array();
  5.     
  6.     public function HanoiStack()
  7.     {
  8.     }
  9.     
  10.     public function pushItem( $intItem )
  11.     {
  12.         $intItem = intval( $intItem );
  13.         
  14.         if ( count( $this->arrStack ) === 0 )
  15.         {
  16.             array_push( $this->arrStack, $intItem );
  17.         }
  18.         else
  19.         {
  20.             if ( $intItem > $this->getLastItem() )
  21.             {
  22.                 throw new Exception( 'Cannot put bigger brick on smaller (' . $intItem . ' on ' . $this->getLastItem() . ')' );
  23.             }
  24.             
  25.             array_push( $this->arrStack, $intItem );
  26.         }
  27.     }
  28.     
  29.     public function popItem()
  30.     {
  31.         if ( count( $this->arrStack ) === 0 )
  32.         {
  33.             print_r( $this->arrStack );
  34.             throw new Exception( 'Cannot pop intem from empty stack' );
  35.         }
  36.         
  37.         return array_pop( $this->arrStack );
  38.     }
  39.     
  40.     public function getLastItem()
  41.     {
  42.         if ( count( $this->arrStack ) === 0 )
  43.         {
  44.             return -1;
  45.         }
  46.         
  47.         return $this->arrStack[ count( $this->arrStack ) - 1 ];
  48.     }
  49.     
  50.     public function __toString()
  51.     {
  52.         $s = '[ ';
  53.         
  54.         foreach ( $this->arrStack as $v )
  55.         {
  56.             $s .= $v . ' ';
  57.         }
  58.         
  59.         $s .= &#092;"]n\";
  60.         return $s;
  61.     }
  62. }
  63.  
  64. class Hanoi
  65. {
  66.     private $arrStacks = array();
  67.     
  68.     private $intN;
  69.     
  70.     private $intMoves;
  71.     
  72.     public function Hanoi( $intN )
  73.     {
  74.         $this->intN = intval( $intN );
  75.         
  76.         for ( $i = 0; $i < 3; $i++ )
  77.         {
  78.             $this->arrStacks[ $i ] = new HanoiStack();
  79.         }
  80.         
  81.         for ( $i = $this->intN - 1; $i >= 0; $i-- )
  82.         {
  83.             $this->arrStacks[ 0 ]->pushItem( $i ); 
  84.         }
  85.     }
  86.     
  87.     public function solve()
  88.     {
  89.         $this->printTowers();
  90.         
  91.         // z wiezy 0 na wieze 2
  92.         if ( $this->intN <= 0 )
  93.         {
  94.             throw new Exception( 'Wrong intN' );
  95.         }
  96.         
  97.         $i_max = intval( pow( 2, $this->intN ) - 1 );
  98.         
  99.         $intBrickOnePosition = 0;
  100.         $intMoveDirection = ( $this->intN % 2 === 1 ) ? -: 1;
  101.         
  102.         for ( $i = 0; $i < $i_max; $i++ )
  103.         {
  104.             if ( $i % 2 === 0 )
  105.             {
  106.                 // zdejmij jedynke (tu zero: indeksujemy klocki od zera)
  107.                 $intItem = $this->arrStacks[ $intBrickOnePosition ]->popItem();
  108.                 print( '(' . $intBrickOnePosition . ')--[ ' . $intItem . ' ]-->(' . ( $intBrickOnePosition + $intMoveDirection + 3 ) % 3 . ')' . &#092;"n\" );
  109.                 $intBrickOnePosition = ( $intBrickOnePosition + $intMoveDirection + 3 ) % 3;
  110.                 // i przenies ja w keirunku $intMoveDirection
  111.                 $this->arrStacks[ $intBrickOnePosition ]->pushItem( $intItem );
  112.             }
  113.             else
  114.             {
  115.                 $arrTowerIndices = array();
  116.                 $arrTopItems = array();
  117.                 $x = 0;
  118.                 
  119.                 
  120.                 // znajdz wieze z min i max brickiem !== 1
  121.                 for ( $j = 0; $j < 3; $j++ )
  122.                 {
  123.                     if ( $j !== $intBrickOnePosition )
  124.                     {
  125.                         $arrTowerIndices[ $x ] = $j;
  126.                         $arrTopItems[ $x ] = $this->arrStacks[ $j ]->getLastItem();
  127.                         $x++;
  128.                     }
  129.                 }
  130.                 
  131.                 $intTowerWithSmallerTopItem = ( $arrTopItems[ 0 ] < $arrTopItems[ 1 ] ) ? $arrTowerIndices[ 0 ] : $arrTowerIndices[ 1 ];
  132.                 $intTowerWithBiggerTopItem = ( $arrTopItems[ 0 ] > $arrTopItems[ 1 ] ) ? $arrTowerIndices[ 0 ] : $arrTowerIndices[ 1 ];
  133.                 
  134.                 if ( $this->arrStacks[ $intTowerWithSmallerTopItem ]->getLastItem() === -1 )
  135.                 {
  136.                     // zamien je to ta pierwsza jest pusta
  137.                     $temp = $intTowerWithSmallerTopItem;
  138.                     $intTowerWithSmallerTopItem = $intTowerWithBiggerTopItem;
  139.                     $intTowerWithBiggerTopItem = $temp;
  140.                 }
  141.                 
  142.                 // przenies klocek
  143.                 $intItem = $this->arrStacks[ $intTowerWithSmallerTopItem ]->popItem();
  144.                 print( '(' . $intTowerWithSmallerTopItem . ')--[ ' . $intItem . ' ]-->(' . $intTowerWithBiggerTopItem . ')' . &#092;"n\" );
  145.                 $this->arrStacks[ $intTowerWithBiggerTopItem ]->pushItem( $intItem );
  146.                 
  147.                 
  148.             }
  149.             
  150.             $this->printTowers();
  151.         }
  152.     }
  153.     
  154.     private function printTowers()
  155.     {
  156.         print( '0: ' );
  157.         print( $this->arrStacks[ 0 ] );
  158.         print( '1: ' );
  159.         print( $this->arrStacks[ 1 ] );
  160.         print( '2: ' );
  161.         print( $this->arrStacks[ 2 ] );
  162.         print( &#092;"n\" );
  163.     }
  164. }
  165.  
  166.  
  167. class HanoiTowers
  168. {
  169.     public static function main()
  170.     {
  171.         try
  172.         {
  173.             $objHanoiTower = new Hanoi( 3 );
  174.             $objHanoiTower->solve();
  175.         }
  176.         catch ( Exception $e )
  177.         {
  178.             print( $e );
  179.         }
  180.     }
  181. }
  182.  
  183. HanoiTowers::main();
  184. ?></pre>


Ten post edytował dr_bonzo 2.06.2005, 17:47:06
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
dr_bonzo
post
Post #2





Grupa: Przyjaciele php.pl
Postów: 5 724
Pomógł: 259
Dołączył: 13.04.2004
Skąd: N/A

Ostrzeżenie: (0%)
-----


Twoj algorytm jest rekurencyjny, a moj nie. Oba opisane sa w wikipedii:

Cytat
Alternately move disc 1 and one of the larger discs. If two of the larger discs are available, always move the smaller one onto the larger. When you move an odd-numbered disc, always move it one peg clockwise; when you move an even-numbered disc, always move it one peg counterclockwise.

An even easier way to remember this solution is to notice that the smallest disk gets every second move, and always moves in the same direction. In between smallest disk moves, there is only one legal move that does does not involve moving the smallest disk again.

----

Cytat
Nie przygladalem sie za bardzo Twojemu skryptowi, ale mysle ze mozna bylo zrobic to krocej

No pewnie, w ogole porzucic klasy, obsluge bledow, skrocic printTowers(), itd.
Go to the top of the page
+Quote Post

Posty w temacie
- dr_bonzo   Wieże Hanoi   22.05.2005, 22:50:17
- - GrayHat   mozna jakis exampel?   25.05.2005, 22:06:35
- - dr_bonzo   Ehhh... a chociarz uruchomiles ten skrypt? Wystarc...   25.05.2005, 22:56:07
- - GrayHat   a wogole co to wierze hanoi??   25.05.2005, 23:41:07
- - dr_bonzo   WieZe -- google wiedzialo ale nie powiedzialo? htt...   25.05.2005, 23:53:17
- - Bielo   Sam algorytm rozwiązywania Hanoi wygląda tak: [PHP...   29.05.2005, 09:22:10
- - dr_bonzo   Twoj algorytm jest rekurencyjny, a moj nie. Oba op...   29.05.2005, 10:12:15
- - Bielo   to co ja napisalem to jest tylko algorytm, a nie c...   29.05.2005, 10:20:44
- - Radarek   A mi nawet nie chce sie czytac i probowac zrozumie...   29.05.2005, 11:45:05
- - M4chu   Najoptymalniejszym? Dla mnie najoptymalniejsze to ...   29.05.2005, 11:52:49
- - dr_bonzo   CytatAle braw nie bije bo znalem juz wczesniej to ...   29.05.2005, 12:15:32
- - Bakus   @Bielo: Jeżeli masz pomysł, to go rozwiń, napisz s...   29.05.2005, 14:36:03
- - Radarek   CytatNajoptymalniejszym? Dla mnie najoptymalniejsz...   29.05.2005, 14:38:41
- - bela_666   Cytat(Radarek @ 2005-05-29 15:38:41)Cytat Naj...   29.05.2005, 16:12:29
- - Radarek   Cytat(bela_666 @ 2005-05-29 15:12:29)Cytat(Ra...   29.05.2005, 16:46:24
- - dr_bonzo   Ale wez tez pod uwage wielokrotne wywolywanie funk...   29.05.2005, 17:50:27
- - Radarek   Cytat(dr_bonzo @ 2005-05-29 16:50:27)Ale wez ...   29.05.2005, 18:33:38
- - onlyX   A mi wyrzuciło taki błąd w tym pierszym skrypcie: ...   2.06.2005, 17:28:09
- - dr_bonzo   Bo musisz miec PHP5. PS. Uzupelniony 1szy post o ...   2.06.2005, 17:46:52
- - piratt   A wzial ktos pod uwage fakt ze w wersji rekurencyj...   29.09.2005, 10:34:07
- - Bakus   to raczej nie stanowi problemu - w koncu po wywola...   29.09.2005, 23:13:26
- - piratt   Po wykonaniu. A pamiec sie skonczy w trakcie;) Zau...   1.10.2005, 21:04:08


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 5.10.2025 - 09:03