题目
题目链接
解题思路
这是一个模拟题,需要按照顺时针方向填充矩阵。主要思路是:
- 创建指定大小的矩阵
- 使用方向数组控制移动
- 遇到边界或已填充的位置时改变方向
关键点
- 使用方向数组表示四个方向的移动
- 判断下一步是否可以移动(是否越界或已填充)
- 按照右->下->左->上的顺序循环移动
代码
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;class Solution {
private:// 四个方向:右、下、左、上const int dx[4] = {0, 1, 0, -1};const int dy[4] = {1, 0, -1, 0};public:void printSpiralMatrix(int width, int height) {vector<vector<int>> matrix(height, vector<int>(width, -1));int x = 0, y = 0; // 当前位置int dir = 0; // 当前方向int num = 0; // 当前数字// 填充矩阵while (true) {matrix[x][y] = num;// 尝试移动到下一个位置int nx = x + dx[dir];int ny = y + dy[dir];// 如果下一个位置不可用,改变方向if (nx < 0 || nx >= height || ny < 0 || ny >= width || matrix[nx][ny] != -1) {dir = (dir + 1) % 4;nx = x + dx[dir];ny = y + dy[dir];// 如果改变方向后仍然不可用,说明填充完成if (nx < 0 || nx >= height || ny < 0 || ny >= width || matrix[nx][ny] != -1) {break;}num++; // 改变方向时数字加1}x = nx;y = ny;}// 打印矩阵for (int i = 0; i < height; i++) {for (int j = 0; j < width; j++) {cout << matrix[i][j];}cout << endl;}}
};int main() {int width, height;cin >> width >> height;Solution solution;solution.printSpiralMatrix(width, height);return 0;
}
import java.util.Scanner;public class Main {static class Solution {// 四个方向:右、下、左、上private final int[] dx = {0, 1, 0, -1};private final int[] dy = {1, 0, -1, 0};public void printSpiralMatrix(int width, int height) {int[][] matrix = new int[height][width];// 初始化矩阵为-1for (int i = 0; i < height; i++) {for (int j = 0; j < width; j++) {matrix[i][j] = -1;}}int x = 0, y = 0; // 当前位置int dir = 0; // 当前方向int num = 0; // 当前数字// 填充矩阵while (true) {matrix[x][y] = num;// 尝试移动到下一个位置int nx = x + dx[dir];int ny = y + dy[dir];// 如果下一个位置不可用,改变方向if (nx < 0 || nx >= height || ny < 0 || ny >= width || matrix[nx][ny] != -1) {dir = (dir + 1) % 4;nx = x + dx[dir];ny = y + dy[dir];// 如果改变方向后仍然不可用,说明填充完成if (nx < 0 || nx >= height || ny < 0 || ny >= width || matrix[nx][ny] != -1) {break;}num++; // 改变方向时数字加1}x = nx;y = ny;}// 打印矩阵for (int i = 0; i < height; i++) {for (int j = 0; j < width; j++) {System.out.print(matrix[i][j]);}System.out.println();}}}public static void main(String[] args) {Scanner sc = new Scanner(System.in);int width = sc.nextInt();int height = sc.nextInt();Solution solution = new Solution();solution.printSpiralMatrix(width, height);sc.close();}
}
class Solution:def print_spiral_matrix(self, width: int, height: int) -> None:# 四个方向:右、下、左、上dx = [0, 1, 0, -1]dy = [1, 0, -1, 0]# 创建并初始化矩阵matrix = [[-1] * width for _ in range(height)]x, y = 0, 0 # 当前位置dir = 0 # 当前方向num = 0 # 当前数字# 填充矩阵while True:matrix[x][y] = num# 尝试移动到下一个位置nx = x + dx[dir]ny = y + dy[dir]# 如果下一个位置不可用,改变方向if (nx < 0 or nx >= height or ny < 0 or ny >= width or matrix[nx][ny] != -1):dir = (dir + 1) % 4nx = x + dx[dir]ny = y + dy[dir]# 如果改变方向后仍然不可用,说明填充完成if (nx < 0 or nx >= height or ny < 0 or ny >= width or matrix[nx][ny] != -1):breaknum += 1 # 改变方向时数字加1x, y = nx, ny# 打印矩阵for row in matrix:print(''.join(map(str, row)))def main():width, height = map(int, input().split())solution = Solution()solution.print_spiral_matrix(width, height)if __name__ == "__main__":main()
算法及复杂度
- 算法:模拟
- 时间复杂度:\(\mathcal{O}(width * height)\),需要填充整个矩阵
- 空间复杂度:\(\mathcal{O}(width * height)\),需要存储矩阵