Advertisement
gundambison

fhaslfhaslf

Jun 5th, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.14 KB | None | 0 0
  1. <?php
  2.  
  3. $str='SELAMAT_DATANG_DI_HALAMAN_INI';
  4. $aTxt=array(microtime());
  5.  
  6. $len = strlen($str);
  7. $aTxt[]="panjang:{$len}";
  8. $iSize= ceil( pow($len, .5 ));
  9. $aTxt[]="square:{$iSize}";
  10.  
  11. $arKotak=array();
  12. for($i=0;$i<$iSize;$i++){
  13.     for($j=0;$j<$iSize;$j++){
  14.         $arKotak[$i][$j]=NULL;
  15.     }  
  16. }
  17.  
  18. $aTxt[]="isi kotak";
  19. for($i=0;$i<$iSize;$i++){
  20.     $s=$i.' =>';
  21.     for($j=0;$j<$iSize;$j++){
  22.         $s.=$arKotak[$j][$i]."\t";
  23.     }
  24.     $aTxt[]=$s;
  25. }
  26.  
  27. $stepH=1;
  28. $stepV=0;
  29. $X=$Y=0;
  30.  
  31. for($i=0;$i<$len;$i++){
  32.     $aTxt[]=$str[$i]."\t $X $Y\t$stepH $stepV ";
  33.     $arKotak[$X][$Y]=$str[$i];
  34.     if($stepH!=0){
  35.         $X+=$stepH;
  36.     }
  37.     if($stepV!=0){
  38.         $Y+=$stepV;
  39.     }
  40.    
  41.     $change=FALSE;
  42.     /*
  43.     if(!isset($arKotak[$X][$Y])){
  44.         $aTxt[]='not set '.$X.'-'. $Y;
  45.     }
  46.     */
  47.     if($X>=$iSize||$Y>=$iSize){
  48.         $aTxt[]='to big '.$X.'|'. $Y;
  49.         $change=TRUE;
  50.     }
  51.     if($i>$iSize){
  52.     if($X<0||$Y<0){
  53.         $aTxt[]='to small '.$X.'|'. $Y;
  54.         $change=TRUE;
  55.     }
  56.     }
  57.     if($arKotak[$X][$Y]!=NULL){
  58.         $aTxt[]='not null';
  59.         $change=TRUE;
  60.         $X-=$stepH;
  61.         $Y-=$stepV;
  62.     }
  63.     if($change){
  64. //      $aTxt[]=$str[$i]."\tis not null ato tidak ada ->".$arKotak[$X][$Y];
  65.         $aTxt[]="(b)post $X $Y $stepH $stepV";
  66.             if($X>=$iSize){
  67.                 $X=$iSize-1;
  68.             }elseif($X< 0){
  69.                 $X=0;
  70.             }
  71.            
  72.             if($Y>=$iSize){
  73.                 $Y=$iSize-1;
  74.             }elseif($Y< 0){
  75.                 $Y=0;
  76.             }
  77.         $aTxt[]="(b)post $X $Y $stepH $stepV";
  78.         if($stepH!=0){
  79.  
  80.             if($stepH==1){               
  81.                 $stepV=1;
  82.             }else{
  83.                 $stepV=-1;
  84.             }
  85.             $stepH=0;
  86.             $aTxt[]="(h)post $X $Y $stepH $stepV";
  87.         }elseif($stepV!=0){
  88.            
  89.             if($stepV==1){
  90.                 $stepH=-1;
  91.             }else{
  92.                 $stepH=1;
  93.             }
  94.             $stepV=0;
  95.             $aTxt[]="(v)post $X $Y $stepH $stepV";
  96.         }
  97.        
  98.         if($stepH!=0){
  99.             $X+=$stepH;
  100.         }
  101.         if($stepV!=0){
  102.             $Y+=$stepV;
  103.         }
  104.        
  105.     }
  106.     $aTxt[]="(e)post $X $Y $stepH $stepV";
  107. }
  108.  
  109.  
  110. //beri Q tempat yg kosong
  111. for($i=0;$i<$iSize;$i++){
  112.     for($j=0;$j<$iSize;$j++){
  113.         if($arKotak[$i][$j]==NULL)
  114.             $arKotak[$i][$j]='Q';
  115.     }  
  116. }
  117.  
  118. $aTxt[]="isi kotak";
  119. for($i=0;$i<$iSize;$i++){
  120.     $s=$i.' =>';
  121.     for($j=0;$j<$iSize;$j++){
  122.         $s.=$arKotak[$j][$i]."\t";
  123.     }
  124.     $aTxt[]=$s;
  125. }
  126. $aTxt[]='<pre>'.print_r($arKotak,1).'</pre>';
  127.  
  128. echo '<pre>'.implode("\n",$aTxt).'</pre>';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement