$goal[0] or $next_pos[1] > $goal[1] or $next_pos[0] < 1 or $next_pos[1] < 1) { continue; } if (!isset($dist[$next_pos[0]][$next_pos[1]]) or (isset($dist[$next_pos[0]][$next_pos[1]]) and $dist[$next_pos[0]][$next_pos[1]] > $dist[$cur[0]][$cur[1]]+1) ) { $dist[$next_pos[0]][$next_pos[1]] = $dist[$cur[0]][$cur[1]]+1; array_push($queue,$next_pos); } } } if ( isset($dist[$size][$size]) ) { return($dist[$size][$size]); } else { return -1; } } ?>