美文网首页初见
螺旋输出矩阵

螺旋输出矩阵

作者: Xamd_Dewey | 来源:发表于2017-08-22 10:45 被阅读0次

给定一个mmm行、nnn列的矩阵,按照顺时针螺旋的顺序输出矩阵中所有的元素(从[0][0]位置开始,具体请参见下图)。


输入格式

测评机会反复运行你写的程序。每次程序运行时,首先在第一行输入 222 个整数,分别对应题目描述中的 m 和 n(1 ≤ m, n ≤ 1001),之间用一个空格分隔。接下来输入 m 行,每行包含 n 个整数(−10000 ≤ a, b, c ≤ 10000),每两个整数之间用一个空格分隔。

输出格式

输出为一行,包括 m × n 个整数,按照题目要求的顺序依次输出所有矩阵元素,任意两个整数之间用一个空格分隔,最后一个整数后面没有空格。

解法:用四个变量来判断何时该往哪个方向移动,用一个相同大小的矩阵flag来记录所有路过的元素,用变量num来记录目前走过的元素的总数,最大值为m * n。

#include <iostream>
using namespace std;
int main() {
    int m, n, num;
    cin >> m >> n;
    int goRight = 0, goDown = 0, goUp = 0, goLeft = 0;
    int currentRow = 0, currentCol = n - 1, previousRow = 0, previousCol = 0;
    int matrix[m][n];
    int flag[m][n];

    // 构建矩阵
    for (int i = 0; i < m; ++i) {
        for (int j = 0; j < n; ++j) {
            cin >> matrix[i][j];
            flag[i][j] = 0;
        }
    }
    // 如果只有一行
    if (m == 1) {
        for (int i = 0; i < n; ++i) {
            if (i == 0) {
                cout << matrix[0][i];
            } else {
                cout << " " << matrix[0][i];
            }
        }
        return 0;
    }
    //如果只有一列
    if (n == 1) {
        for (int i = 0; i < m; ++i) {
            if (i == 0) {
                cout << matrix[i][0];
            } else {
                cout << " " << matrix[i][0];
            }
        }
        return 0;
    }
    int a = 0;
    goRight = 1;
    for(num = 0; num < m * n;) {
        if (goRight == 1 && goDown == 0 && goUp == 0 && goLeft == 0) {
            int i;
            if (a == 0) {
                for (i = previousCol; i <= currentCol; i++) {
                    if (i == previousCol) {
                        cout << matrix[currentRow][i];
                    } else {
                        cout << " " << matrix[currentRow][i];
                    }
                    flag[currentRow][i] = 1;
                    num ++;
                    //cout << "current Row: " << currentRow << ", current Column: " << currentCol << ", previous Row: " << previousRow << ", previous Column: " << previousCol << ", num: " << num << endl;
                }
                a = 1;
            } else if (a == 1) {
                for (i = previousCol + 1; i <= currentCol; i++) {
                    cout  << " " << matrix[currentRow][i];
                    flag[currentRow][i] = 1;
                    num ++;
                    //cout << "current Row: " << currentRow << ", current Column: " << currentCol << ", previous Row: " << previousRow << ", previous Column: " << previousCol << ", num: " << num << endl;
                }
            }

            // 如果该行末尾下的元素没有被访问过
            if (flag[currentRow + 1][currentCol] == 0) {
                goRight = 0;
                goDown = 1;
                int temp = currentRow;
                currentRow = m - 1 - previousRow;
                previousRow = temp;
                previousCol = currentCol;
                //cout << "Now go down, current Row: " << currentRow << ", current Column: " << currentCol << ", previous Row: " << previousRow << ", previous Column: " << previousCol << endl << endl;
                continue;
            } else {
                break;
            }

        }
        else if (goRight == 0 && goDown == 1 && goUp == 0 && goLeft == 0) {
            int i;
            for (i = previousRow + 1; i <= currentRow; i++) {
                cout << " " << matrix[i][currentCol];
                flag[i][currentCol] = 1;
                num ++;
                //cout << "current Row: " << currentRow << ", current Column: " << currentCol << ", previous Row: " << previousRow << ", previous Column: " << previousCol << ", num: " << num <<  endl;
            }
            // 如果该列末尾左边的元素没有被访问过
            if (flag[currentRow][currentCol - 1] == 0) {
                goDown = 0;
                goLeft = 1;
                int temp = currentCol;
                currentCol = n - 1 - previousCol;
                previousCol = temp;
                previousRow = currentRow;
                //cout << "Now go left, current Row: " << currentRow << ", current Column: " << currentCol << ", previous Row: " << previousRow << ", previous Column: " << previousCol << endl << endl;
                continue;
            } else {
                break;
            }
        }
        else if (goRight == 0 && goDown == 0 && goUp == 0 && goLeft == 1) {
            int i;
            for (i = previousCol - 1; i >= currentCol ; i--) {
                cout << " " << matrix[currentRow][i];
                flag[currentRow][i] = 1;
                num ++;
                //cout << "current Row: " << currentRow << ", current Column: " << currentCol << ", previous Row: " << previousRow << ", previous Column: " << previousCol << ", num: " << num <<  endl;
            }
            // 如果该行末尾上面的元素没有被访问过
            if (flag[currentRow - 1][currentCol] == 0) {
                goLeft = 0;
                goUp = 1;
                int temp = currentRow;
                currentRow = m - 1 + 1 - previousRow;
                previousRow = temp;
                previousCol = currentCol;
                //cout << "Now go up, current Row: " << currentRow << ", current Column: " << currentCol << ", previous Row: " << previousRow << ", previous Column: " << previousCol << endl << endl;
                continue;
            } else {
                break;
            }
        }
        else if (goRight == 0 && goDown == 0 && goUp == 1 && goLeft == 0) {
            int i;
            for (i = previousRow - 1; i >= currentRow; i--) {
                cout << " " << matrix[i][currentCol];
                flag[i][currentCol] = 1;
                num ++;
                //cout << "current Row: " << currentRow << ", current Column: " << currentCol << ", previous Row: " << previousRow << ", previous Column: " << previousCol << ", num: " << num <<  endl;
            }
            // 如果该列末尾右边的元素没有被访问过
            if (flag[currentRow][currentCol + 1] == 0) {
                goUp = 0;
                goRight = 1;
                int temp = currentCol;
                currentCol = n - 1 - 1 - previousCol;
                previousCol = temp;
                previousRow = currentRow;
                //cout << "Now go right, current Row: " << currentRow << ", current Column: " << currentCol << ", previous Row: " << previousRow << ", previous Column: " << previousCol << endl << endl;
                continue;
            } else {
                break;
            }
        }

    }

    return 0;
}

相关文章

  • 螺旋输出矩阵

    给定一个mmm行、nnn列的矩阵,按照顺时针螺旋的顺序输出矩阵中所有的元素(从[0][0]位置开始,具体请参见下图...

  • Java 实现输出螺旋矩阵

    题目 好记性不如烂笔头,记下来 用java实现输入一个50以内的数字num,让这个num的平方数字以螺旋矩阵的方式...

  • Python实现螺旋矩阵

    螺旋矩阵 什么是螺旋矩阵? 螺旋矩阵是指一个呈螺旋状的矩阵,它的数字由第一行开始到右边不断变大,向下变大,向左变大...

  • C语言 矩阵螺旋输出 解法

    C语言算法题 给定一个 m行、n列的矩阵,请按照顺时针螺旋的顺序输出矩阵中所有的元素(从[0][0]位置开始,具体...

  • 螺旋矩阵

    螺旋矩阵 1.想法: 对于矩阵的螺旋我们可以规约为4个方向 2.代码:

  • 螺旋矩阵

    递归 非递归

  • 螺旋矩阵

    给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。 示例 1...

  • 螺旋矩阵

    题目描述:给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。...

  • 螺旋矩阵

    给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。 示例 1...

  • 螺旋矩阵

    原文地址,我的个人博 1.题目 2.分析 上图展示了一轮完整的顺时针螺旋遍历的过程,整个过程可以分为如图所示的四个...

网友评论

    本文标题:螺旋输出矩阵

    本文链接:https://www.haomeiwen.com/subject/fhwkdxtx.html