蛇形填数问题—递归+迭代

#include<iostream>

using namespace std;

/* 蛇形填数问题: 输入n: 将1~n*n按下面的要求填入二维数组中: ---------> /|\-->... | | | <-------\|/ 例如当输入n=3时: 1 2 3 8 9 4 7 6 5 */

void snake_v1(int const n){

    //创建一个二维数组。
    int **snake = new int*[n];
    for (int i(0); i < n; i++)
        snake[i] = new int[n];


    int itemNum(0);
    int row(0), column(-1);
    int row_limit(n - 1), column_limit(n);//分别记录每一次沿对应方向变化的次数。
    while (itemNum<n*n)
    {
        //行不变,列变 ++
        for (int i(0); i< column_limit; i++)
        {
            snake[row][++column] = ++itemNum;
        }
        column_limit--;

        //列不变,行变 ++
        for (int i(0); i<row_limit; i++)
        {
            snake[++row][column] = ++itemNum;
        }

        //行不变,列变 --
        for (int i(0); i <column_limit; i++)
        {
            snake[row][--column] = ++itemNum;
        }
        row_limit--;

        //列不变,行变 --
        for (int i(0); i <row_limit; i++)
        {
            snake[--row][column] = ++itemNum;
        }

        column_limit--, row_limit--;
    }

    for (int i(0); i < n; i++)
    {
        for (int j(0); j < n; j++)
        {
            printf("%5d", snake[i][j]);

        }
        cout << endl;
    }

}

void snake_v2(int const n){

    //创建一个二维数组。
    int **snake = new int*[n];
    for (int i(0); i < n; i++)
    {
        snake[i] = new int[n]; 
    }

    //用零去初始化
    for (int i(0); i < n; i++)
        for (int j(0); j < n; j++)
            snake[i][j]=0;


    int itemNum(0);
    int row(0), column(-1);
    while (itemNum<n*n)
    {
        // snake[i][j]!=0时,相当于遇到了一睹墙。也可以用哨兵来形容这种情况
        //这样就不需要所谓的row_limit和column_limit来控制循环的次数了。
        while (column + 1 < n && !snake[column + 1][row]){
            snake[row][++column] = ++itemNum;
        }
        while (row + 1 <n && !snake[row + 1][column]){
            snake[++row][column] = ++itemNum;
        }
        while (column - 1 >= 0 && !snake[row][column - 1]){
            snake[row][--column] = ++itemNum;
        }
        while (row - 1 > 0 && !snake[row - 1][column]) {
            snake[--row][column] = ++itemNum;
        }
    }

    for (int i(0); i < n; i++)
    {
        for (int j(0); j < n; j++)
        {
            printf("%5d", snake[i][j]);

        }
        cout << endl;
    }
}

void fill_the_outside(int **snake, int const first_row, int const first_column, int firstNum, int const n){

    if (n == 0)return;

    if (n == 1){
        snake[first_row][first_column] = firstNum; return;
    }

    int row_end(first_row+n-1), colum_end(first_column+n-1);

    //填上第一行
    for (int i(first_column); i <= colum_end; i++){
        snake[first_row][i] = firstNum++;
    }
    //填上最后一列
    for (int i(first_row + 1); i <= row_end; i++){
        snake[i][colum_end] = firstNum++;
    }
    //填上最后一行
    for (int i(colum_end - 1); i >= first_column; i--){
        snake[row_end][i] = firstNum++;
    }
    //填上第一列
    for (int i(row_end - 1); i > first_row; i--){
        snake[i][first_column] = firstNum++;
    }
    //继续递归的进行填壳
    fill_the_outside(snake, first_row + 1, first_column + 1, firstNum, n - 2);
}

void snake_v3(int const n){

    //创建一个二维数组。
    int **snake = new int*[n];
    for (int i(0); i < n; i++)
    {
        snake[i] = new int[n];
    }

    //递归的将外壳一层层的填上。
    fill_the_outside(snake,0,0,1, n);

    for (int i(0); i < n; i++)
    {
        for (int j(0); j < n; j++)
        {
            printf("%5d", snake[i][j]);

        }
        cout << endl;
    }
}

int main(){

    int n;
    cout << "输入n:" << endl;
    cin >> n;   

    cout << "snake_v1:" << endl;
    snake_v1(n);

    cout << "snake_v2:" << endl;
    snake_v2(n);

    cout << "snake_v3:" << endl;
    snake_v3(n);

    system("pause");
}

你可能感兴趣的:(蛇形填数问题—递归+迭代)