螺旋输出矩阵

给定一个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;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 来源:NumPy Tutorial - TutorialsPoint 译者:飞龙 协议:CC BY-NC-SA 4...
    布客飞龙阅读 33,054评论 6 98
  • 个人学习批处理的初衷来源于实际工作;在某个迭代版本有个BS(安卓手游模拟器)大需求,从而在测试过程中就重复涉及到...
    Luckykailiu阅读 4,792评论 0 11
  • 数组索引 这样声明个数组,名为radius,含3个int型元素。我们可通过radius[0],radius[1],...
    夏威夷的芒果阅读 943评论 1 0
  • 最近跟一姑娘聊天,她性格不错,做事也努力,却总“命途多舛”,受人“欺负”——在老公司,被同事针锋相对,沦为受气包;...
    颜卤煮阅读 2,406评论 7 53
  • 1 开解一个想要放弃喜欢一个人的人。从晚上8点半左右,我们在市政府亲水平台里来来回回地不知走了多少遍,唯一的话题是...
    目禾阅读 215评论 0 0