<?php
function KthinMatrix( $matrix , $k )
{
$pq = new PQ();
$n =count($matrix);
$visited = [];
for($i=0;$i<$n;$i++){
for($j=0;$j<$n;$j++){
$visited[$i][$j] =false;
}
}
$pq->add(0,0,$matrix[0][0]);
$visited[0][0]= true;
for($i=0;$i<$k-1;$i++){
$poll = $pq->poll();
$x = $poll[0];
$y = $poll[1];
if($x <$n && $y+1<$n && !$visited[$x][$y+1]){
$pq->add($x,$y+1,$matrix[$x][$y+1]);
$visited[$x][$y+1]=true;
}
if($x+1<$n&&$y<$n && !$visited[$x+1][$y]){
$pq->add($x+1,$y,$matrix[$x+1][$y]);
$visited[$x+1][$y]=true;
}
}
return $pq->poll()[2];
}
class PQ{
public $arr=[];
public $defaultsize =10;
public $size;
public function __construct()
{
$this->size =0;
for($i=0;$i<$this->defaultsize;$i++){
$this->arr[$i] =[];
}
}
public function ensurecap($cap){
$curlen =count($this->arr);
if($curlen >= $cap){
return;
}
$newlen = $curlen+$curlen>>1;
$newarr = [];
for($i=0;$i<$newlen;$i++){
if($i<$curlen){
$newarr[$i] = $this->arr[$i];
}else{
$newarr[$i] =[];
}
}
$this->arr = $newarr;
}
public function add($x,$y,$data){
$this->ensurecap($this->size+1);
$this->arr[$this->size] = [$x,$y,$data];
$this->shiftup($this->size);
$this->size++;
}
public function shiftup($idx){
$root = $this->arr[$idx];
while ($idx>0){
$pidx = ($idx-1) >>1;
$parent= $this->arr[$pidx];
if($parent[2] <=$root[2]){
break;
}
$this->arr[$idx] = $parent;
$idx = $pidx;
}
$this->arr[$idx] = $root;
}
public function poll() {
$root = $this->arr[0];
$this->arr[0] = $this->arr[$this->size-1];
$this->size-=1;
$this->shiftdown(0);
return $root;
}
public function shiftdown($idx){
$root = $this->arr[$idx];
$half = $this->size >>1;
while ($idx<$half){
$childidx = ($idx << 1)+1;
$rightidx = $childidx+1;
$child = $this->arr[$childidx];
if($rightidx<$this->size && $child[2] > $this->arr[$rightidx][2]){
$childidx = $rightidx;
$child = $this->arr[$rightidx];
}
if($child[2] >=$root[2]){
break;
}
$this->arr[$idx] = $child;
$idx =$childidx;
}
$this->arr[$idx] = $root;
}
}