算法面试题-构造一个数组成为风车型排列
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8bda06d29cf0dd35e1799e560939047e.jpeg#pic_center)
实现一个 function foo($num) 函数,依照一下例子类推,组成一个风车型排列;
// foo(1) = [[1]];
// foo(2) = [ [1,2]
// [4,3] ];
// foo(3) = [ [7,8,9]
// [6,1,2]
// [5,4,3] ];
// foo(4) = [ [7,8,9,10]
// [6,1,2,11]
// [5,4,3,12]
// [16,15,14,13] ];
//
// foo(5) = [ [21,22,23,24,25]
// [20,7,8,9,10]
// [19,6,1,2,11]
// [18,5,4,3,12]
// [17,16,15,14,13] ];
题目详情
$num = 10;
$arr = foo($num);
for ($i = 0; $i < $num; $i++) {
echo implode(", ", $arr[$i]) . "\n";
}
function foo ($num) {
# TODO 实现代码
return [[1, 2], [4, 3]];
}
个人实现的逻辑
function foo($num) {
$arr = array_fill(0, $num, array_fill(0, $num, 0)); // 初始化 $num x $num 的矩阵
$count = $num * $num;
$value = 1;
$x = $y = 0;
$layer = 0;
while ($value <= $count) {
// 填充上边
for ($i = $layer; $i < $num - $layer; $i++) {
$arr[$layer][$i] = $value++;
}
// 填充右边
for ($i = $layer + 1; $i < $num - $layer; $i++) {
$arr[$i][$num - $layer - 1] = $value++;
}
// 填充下边
for ($i = $num - $layer - 2; $i >= $layer; $i--) {
$arr[$num - $layer - 1][$i] = $value++;
}
// 填充左边
for ($i = $num - $layer - 2; $i > $layer; $i--) {
$arr[$i][$layer] = $value++;
}
$layer++;
}
return $arr;
}
// 示例
$num = 2;
$arr = foo($num);
var_dump($arr);
网络搜索
function foo($num){
//填充map
$data = new stdClass();
$data->x = 1;
$data->y = 1;
$data->num = $num;
$data->len = $num * $num;
$data->tmp_data = [];
$data->rate = 1;//方向
$data->struct = create_struct($num);
for ($i = $data->len;$i>=1;$i--){
$data->i = $i;
$key = change_key($data);
$data->tmp_data[$key] = $i;
unset($data->struct[$key]);
}
//根据y坐标分组
$data->struct = create_struct($num);
foreach ($data->tmp_data as $key=> $value){
$data->struct[$key]= $value;
}
for ($i=1;$i<=$data->num;$i++){
$start = ($i - 1)*$data->num;
$end = $data->num;
$slice = array_slice($data->struct,$start,$end);
$data->slice[] = '['.implode(",", $slice).']';
}
return $data->slice;
}
function change_key($data){
//check key
$key = $data->x.','.$data->y;
if(isset($data->struct[$key])){
return $key;
}
switch ($data->rate)
{
case 1:
//left
$data->tmp_x = $data->x + 1;
$key = $data->tmp_x.','.$data->y;
if(isset($data->struct[$key])){
$data->x = $data->tmp_x;
return $key;
}else{
$data->rate=2;
$data->y = $data->y+1;
}
break;
case 2:
//down
$data->tmp_y = $data->y + 1;
$key = $data->x.','.$data->tmp_y;
if(isset($data->struct[$key])){
$data->y = $data->tmp_y;
return $key;
}else{
$data->rate=3;
}
break;
case 3:
//right
$data->tmp_x = $data->x - 1;
$key = $data->tmp_x.','.$data->y;
if(isset($data->struct[$key])){
$data->x = $data->tmp_x;
return $key;
}else{
$data->rate=4;
}
break;
case 4:
//up
$data->tmp_y = $data->y - 1;
$key = $data->x.','.$data->tmp_y;
if(isset($data->struct[$key])){
$data->y = $data->tmp_y;
return $key;
}else{
$data->rate=1;
}
break;
}
return change_key($data);
}
function create_struct($num){
$struct = [];
for ($i=1;$i<=$num;$i++){
//嵌套
for($m=1;$m<=$num;$m++){
$key = $m . ','. $i;
$struct[$key] = '';
}
}
return $struct;
}
function dump($data){
echo '<pre>';
print_r($data);
echo '</pre>';
echo '<br>';
}