一个m*n的矩阵里按照下图形式填充,最后形成的矩阵即为蛇形矩阵,下图是m=4, n =5时的蛇形矩阵:
方法一:逐层循环
#includeusing namespace std;const int ROW = 6;const int COLUMN = 10;const string space = " ";void setSnakeMatrix(int array[ROW][COLUMN], int start){ int x = 0, y = 0; while(start =0 && array[x][y-1]==0) { array[x][y--] = start++; } //左列:从下向上 while(x-1>=0 && array[x-1][y]==0) { array[x--][y] = start++; } } //最后一个 array[x][y] = start; }void displayMatrix(int array[ROW][COLUMN]){ for(int row = 0; row
=10) { cout<
<