【数据结构】数组与广义表-螺旋矩阵的实现(图解、c++、java)

GitHub同步更新(已分类)Data_Structure_And_Algorithm-Review

公众号:URLeisure 的复习仓库
公众号二维码见文末

以下是本篇文章正文内容,下面案例可供参考。


什么是螺旋矩阵?

3阶螺旋矩阵:
3阶螺旋矩阵

4阶螺旋矩阵:
4阶螺旋矩阵

螺旋矩阵的实现很简单,学习它是为了更好地了解如何在二维数组里进行上下左右的移动操作,强化基础。

创建思路

  • 创建方法很简单,别找规律一行一行输就行。

如图,我们本次的螺旋矩阵按照右下左上的顺序完成创建。

4阶螺旋矩阵

  • 先一直朝向一个方向创建,创建过程中判断是否需要转向,然后继续创建。

  • 我们默认数组操作前数值全部为 0 ,而边界为 -1。

则判断条件为:

  1. 下一个值是否为 -1;
  2. 下一个值是否为 0。

(其实只用判断下一个值是否为 0 就行了,不为 0 就转向)

算法设计

  • 生成螺旋矩阵就需要转向操作,转向操作也十分的简单。

我们先定义一个转向用的结构体(内部类),包含行(x),列(y)转向信息。

c++代码如下(示例):

struct Position{
    int x;
    int y;
};

java代码如下(示例):

public static class Position {
    int x;
    int y;

    Position() {
    }

    Position(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

初始化

  • 将边界设为 -1,而其他位置为 0.

c++代码如下(示例):

void Init(int n) {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            m[i][j] = 0;
        }
    }
    for (int i = 0; i <= n + 1; i++) {
        m[0][i] = m[n + 1][i] = -1;
    }
    for (int i = 0; i <= n + 1; i++) {
        m[i][0] = m[i][n + 1] = -1;
    }
}

java代码如下(示例):

public static void init(int n) {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            m[i][j] = 0;
        }
    }
    for (int i = 0; i <= n + 1; i++) {
        m[0][i] = m[n + 1][i] = -1;
    }
    for (int i = 0; i <= n + 1; i++) {
        m[i][0] = m[i][n + 1] = -1;
    }
}

转向操作

  1. 向右:行 +0,列 +1;
  2. 向下:行 +1,列 +0;
  3. 向左:行 +0,列 -1;
  4. 向上:行 -1,列 +0。

c++代码如下(示例):

Position DIR[4] = {0, 1, 1, 0, 0, -1, -1, 0};
Position here, nextt;//当前位置 下一个位置

void Solve(int n) {
    here.x = 1;
    here.y = 1;//当前位置为 1 1
    int num = 1;
    m[1][1] = 1;//开始位置为数字 1
    int dirIndex = 0;
    while (num < n * n) {
        nextt.x = here.x + DIR[dirIndex].x;
        nextt.y = here.y + DIR[dirIndex].y;//更新下一个位置
        if (m[nextt.x][nextt.y] == 0) {//判断下一个位置是否合法
            m[nextt.x][nextt.y] = ++num;
            here = nextt;
        } else {
            dirIndex = (dirIndex + 1) % 4;
        }
    }
}

java代码如下(示例):

public static Position here = new Position();//当前位置
public static Position nextt = new Position();//下一个位置

public static Position dir[] = {
        new Position(0, 1),
        new Position(1, 0),
        new Position(0, -1),
    	new Position(-1, 0)
};

public static void solve(int n) {
    here.x = 1;
    here.y = 1;
    int num = 1;
    m[1][1] = 1;
    int dirIndex = 0;
  	while (num < n * n) {
        nextt.x = here.x + dir[dirIndex].x;
        nextt.y = here.y + dir[dirIndex].y;
        if (m[nextt.x][nextt.y] == 0) {
            m[nextt.x][nextt.y] = ++num;
            here.x = nextt.x;
            here.y = nextt.y;
        } else {
            dirIndex = (dirIndex + 1) % 4;
        }
    }
}

完整代码

c++代码如下(示例):

#include<iostream>

using namespace std;

struct Position {
    int x;
    int y;
};
Position DIR[4] = {0, 1, 1, 0, 0, -1, -1, 0};
Position here, nextt;

int m[100][100];

void Init(int n) {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            m[i][j] = 0;
        }
    }
    for (int i = 0; i <= n + 1; i++) {
        m[0][i] = m[n + 1][i] = -1;
    }
    for (int i = 0; i <= n + 1; i++) {
        m[i][0] = m[i][n + 1] = -1;
    }
}

void Solve(int n) {
    here.x = 1;
    here.y = 1;
    int num = 1;
    m[1][1] = 1;
    int dirIndex = 0;
    while (num < n * n) {
        nextt.x = here.x + DIR[dirIndex].x;
        nextt.y = here.y + DIR[dirIndex].y;
        if (m[nextt.x][nextt.y] == 0) {
            m[nextt.x][nextt.y] = ++num;
            here = nextt;
        } else {
            dirIndex = (dirIndex + 1) % 4;
        }
    }
}

void Print(int n) {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cout << m[i][j] << "\t";
        }
        cout << endl;
    }
}

int main() {
    int n;
    cin >> n;
    Init(n);
    Solve(n);
    Print(n);
}

java代码如下(示例):

import java.util.Scanner;

public class A {
    public static class Position {
        int x;
        int y;

        Position() {
        }

        Position(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }

    public static int m[][] = new int[100][100];
    public static Position here = new Position();
    public static Position nextt = new Position();

    public static Position dir[] = {
            new Position(0, 1),
            new Position(1, 0),
            new Position(0, -1),
            new Position(-1, 0)
    };

    public static void init(int n) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                m[i][j] = 0;
            }
        }
        for (int i = 0; i <= n + 1; i++) {
            m[0][i] = m[n + 1][i] = -1;
        }
        for (int i = 0; i <= n + 1; i++) {
            m[i][0] = m[i][n + 1] = -1;
        }
    }

    public static void solve(int n) {
        here.x = 1;
        here.y = 1;
        int num = 1;
        m[1][1] = 1;
        int dirIndex = 0;
        while (num < n * n) {
            nextt.x = here.x + dir[dirIndex].x;
            nextt.y = here.y + dir[dirIndex].y;
            if (m[nextt.x][nextt.y] == 0) {
                m[nextt.x][nextt.y] = ++num;
                here.x = nextt.x;
                here.y = nextt.y;
            } else {
                dirIndex = (dirIndex + 1) % 4;
            }
        }
    }

    public static void print(int n) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                System.out.print(m[i][j] + "\t");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        int n;
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        init(n);
        solve(n);
        print(n);
    }
}

关注公众号,感受不同的阅读体验

请添加图片描述

下期预告:树-概述篇

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

扑腾的江鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值