BFS(宽搜)的PHP实现

先上个题:

Description

You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.      

Is an escape possible? If yes, how long will it take?      

Input

The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).      
L is the number of levels making up the dungeon.      
R and C are the number of rows and columns making up the plan of each level.      
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.

Output

Each maze generates one line of output. If it is possible to reach the exit, print a line of the form      

Escaped in x minute(s).


where x is replaced by the shortest time it takes to escape.      
If it is not possible to escape, print the line      

Trapped!

Sample Input

3 4 5
S....
.###.
.##..
###.#

#####
#####
##.##
##...

#####
#####
#.###
####E

1 3 3
S##
#E#
###

0 0 0

Sample Output

Escaped in 11 minute(s).

Trapped!

其实就是个三维的宽搜,小弟代码写的,不好勿喷。。。。。。。

大致的意思就是,有一个迷宫,迷宫的结构会给你,“#”是走不了的,然而“.”是可以走的,你需要从“S”开始,走到“E”结束,如果能走出来的话,就打印出所需要花的时间,出不来的话,就打印“Trapped!”

代码(直接运行,样例嵌入进去了,可以修改修改):

<?php
error_reporting( E_ALL&~E_NOTICE );
/*点的实现*/
class POINT{
 public $x;
 public $y;
 public $z;
 function __construct($x,$y,$z){
  $this->x = $x;
  $this->y = $y;
  $this->z = $z;
 }
}
/*直接上网套的一个仁兄的队列实现*/
class queue{  
    protected $front;//队头  
    protected $rear;//队尾  
    protected $queue=array('0'=>'队尾');//存储队列  
    protected $maxsize;//最大数  
      
    public function __construct($size){  
        $this->initQ($size);  
    }  
    //初始化队列  
    private function initQ($size){  
        $this->front=0;  
        $this->rear=0;  
        $this->maxsize=$size;  
    }  
    //判断队空  
    public function QIsEmpty(){  
        return $this->front==$this->rear;  
    }  
    //判断队满  
    public function QIsFull(){  
        return ($this->front-$this->rear)==$this->maxsize;  
    }  
    //获取队首数据  
    public function getFrontDate(){  
        return $this->queue[$this->front]->getData();  
    }  
    //入队  
    public function InQ($data){  
        if($this->QIsFull())echo $data."full<br>";  
        else {  
            $this->front++;  
            for($i=$this->front;$i>$this->rear;$i--){  
                //echo $data;  
                if($this->queue[$i])unset($this->queue[$i]);  
                $this->queue[$i]=$this->queue[$i-1];  
            }  
            $this->queue[$this->rear+1]=new data($data); 
        }  
    }  
    //出队  
    public function OutQ(){  
        if($this->QIsEmpty())echo "no data!<br>";  
        else{  
            unset($this->queue[$this->front]);  
            $this->front--;
   }
 }
}
class data {  
    //数据  
    private $data;  
      
    public function __construct($data){  
        $this->data=$data;  
    }  
      
    public function getData(){  
        return $this->data;  
    }  
    public function __destruct(){ 
    }  
}
class bfs{
 public $que;/*承载所有试过的点*/
 public $xx;/*全局变量,当x变化的时候,接住x的变化值*/
 public $yy;/*全局变量,当y变化的时候,接住y的变*/
 public $zz;/*全局变量,当z变化的时候,接住z的变*/
 public $start;/*记录游戏的起始点*/
 public $ended;/*记录游戏的终止点*/
 public $visit;/*三维数组,记录被访问过的点的走过的步数*/
 public $l,$r,$c;/*长、宽、高*/
 public $ch = array();/*其实比较多余,但是是记录棋盘的char化为数字,能好看点........*/
 public $chess = array();/*棋盘*/
 
 function __construct(){
  $this->que = new queue(100);
  $this->start = new POINT(0,0,0);
  $this->ended = new POINT(0,0,0);
  $this->visit = array();
 }
 /*走一步的各种情况,可能往上下左右前后走,六种情况*/
 function path($x,$y,$z,$t){
   if($t==0)
   { 
    $this->xx=$x+1;$this->yy=$y;$this->zz=$z;
   }
   if($t==1)
   {
    $this->xx=$x;$this->yy=$y+1;$this->zz=$z;
   }
   if($t==2)
   {
    $this->xx=$x;$this->yy=$y;$this->zz=$z+1;
   }
   if($t==3)
   {
    $this->xx=$x-1;$this->yy=$y;$this->zz=$z;
   }
   if($t==4)
   {
    $this->xx=$x;$this->yy=$y-1;$this->zz=$z;
   }
   if($t==5)
   {
    $this->xx=$x;$this->yy=$y;$this->zz=$z-1;
   }
 }
 
 
 /*宽度搜索*/
 function bfs()
 {
  $cur = new POINT(0,0,0);
  
  $start = $this->start;
  
  $this->que->InQ($start);/*起始点肯定是走过的,这个第一个放在队列里*/
  $this->visit[$this->start->z][$this->start->y][$this->start->x] = 1;
  /*起始点的时候姑且算走过一步了*/
  
  /*当还有路可走的时候*/
  while($this->que->QIsEmpty() == false)
  {
   /*找到上一步的位置*/
   $cur=$this->que->getFrontDate();
   /*弹出来*/
   $this->que->OutQ();
   /*如果这一步是终点的话,就返回走到这一步的时候所花的步数*/
   if($cur->x==$this->ended->x&&$cur->y==$this->ended->y&&$cur->z==$this->ended->z)
   {
    return $this->visit[$this->ended->z][$this->ended->y][$this->ended->x];
   }
  /*总共有六种情况,上下左右前后,所以都试一试,行的话,就进入队列表示走过*/
  for($i=0;$i<6;$i++)
  {
   
  $this->path($cur->x,$cur->y,$cur->z,$i);
  /*判断条件,看看是否在边界内,是否走过了,是否是可以走的(不是岩石)*/
  if($this->xx>=0&&$this->yy>=0&&$this->zz>=0&&
   $this->zz<$this->l&&$this->xx<$this->c&&$this->yy<$this->r&&
    $this->visit[$this->zz][$this->yy][$this->xx]==0&&
     $this->ch[$this->zz][$this->yy][$this->xx]==1)
   {
/*这一步是在上一步的基础上又多走了一步,所以要加1*/
$this->visit[$this->zz][$this->yy][$this->xx] = $this->visit[$cur->z][$cur->y][$cur->x]+1;
$this->que->InQ(new POINT($this->xx,$this->yy,$this->zz));
   }
 
  } 
 }
 /*最后还是没结果的话,就返回0*/
  return 0;
 }
 
 function main()
 {
   /*初始化*/
   for($i=0;$i<80;$i++)
   {
    for($j=0;$j<80;$j++)
    {
     
     for($k=0;$k<80;$k++)
     {
      $this->ch[$i][$j][$k] = 0;
      $this->visit[$i][$j][$k] = 0;
      $this->chess[$i][$j] = 0;
     }
    }
   }
   
   /*苦逼呀,录入数据自己敲*/
   $this->l = 3;
   $this->r = 4;
   $this->c = 5;
   $this->chess[0][0] = "S....";
   $this->chess[0][1] = ".###.";
   $this->chess[0][2] = ".##..";
   $this->chess[0][3] = "###.#";
   
   $this->chess[1][0] = "#####";
   $this->chess[1][1] = "#####";
   $this->chess[1][2] = "##.##";
   $this->chess[1][3] = "##...";
   
   $this->chess[2][0] = "#####";
   $this->chess[2][1] = "#####";
   $this->chess[2][2] = "#.###";
   $this->chess[2][3] = "####E";
   
   
   /*把棋盘的#%&之类的符号变成1,0,能走的话就是1不能走就是0,能好看点*/
   for($i=0;$i<$this->l;$i++)
   {
    for($j=0;$j<$this->r;$j++)
    {
     
     for($k=0;$k<$this->c;$k++)
     {
        if(substr($this->chess[$i][$j],$k,1)=='.')
       $this->ch[$i][$j][$k]=1;
        else if(substr($this->chess[$i][$j],$k,1)=='#')
       $this->ch[$i][$j][$k]=0;
        else if(substr($this->chess[$i][$j],$k,1)=='S')
        {
          $this->ch[$i][$j][$k]=1;
       $this->start->x=$k;
       $this->start->y=$j;
       $this->start->z=$i;
      }
      else if(substr($this->chess[$i][$j],$k,1)=='E')
        {
          $this->ch[$i][$j][$k]=1;
       $this->ended->x=$k;
       $this->ended->y=$j;
       $this->ended->z=$i;
      }
     }
    }
   }
   
   $t=$this->bfs();
   /*把队列清空,为了下一次测试*/
   while($this->que->QIsEmpty()==false)
    $this->que->OutQ();
    /*判断是否等于0,如果是,证明最后没有可以移动的步了的就跳出来了,如果不是,在之前的时候,把初始值(也就是没走的时候)设为了1,要减回来*/
   if($t!=0)
    echo "Escaped in " . ($t-1) . " minute(s).<br>";
   else
    echo "Trapped!";
  }
}
?>
<?php
$BFS = new bfs();
$BFS->main();
?>


你可能感兴趣的:(PHP,算法,ACM,bfs,宽搜)