打印旋转矩阵

给定一个整形矩阵matrix,请按照旋转的方式打印它

例如:

1  2  3  4

5  6  7  8

9    10   11   12

13     14     15      16

打印结果为:1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10

本题在算法上没有难度,关键在于设计一种逻辑容易理解、代码易于实现的转圈遍历方式。这里介绍这样一种矩阵处理方式,该方式不仅可用于这道题,还适合很多其他的面试题,就是矩阵分圈处理。在矩阵中用左上角的坐标(tR,tC)和右下角的坐标(dR,dC)就可以表示一个子矩阵,比如,题目中的矩阵,当(tR,tC)=(0,0)、(dR,dC)=(3,3)时,表示的子矩阵就是整个矩阵,那么这个子矩阵最外层的部分如下:

1

2

3

4

5

 

 

8

9

 

 

12

13

14

15

16

如果能把这个子矩阵的外层转圈打印出来,那么在(tR,tC)=(0,0)、(dR,dC)=(3,3)时,打印的结果为:1,2,3,4,8,12,16,15,14,13,9,5。接下来令 tR 和 tC 加 1,即(tR,tC)=(1,1), 令 dR 和 dC 减 1,即(dR,dC)=(2,2),此时表示的子矩阵如下:

6     7

10    11

再把这个子矩阵转圈打印出来,结果为:6,7,11,10。把 tR 和 tC 加 1,即(tR,tC)=(2,2), 令 dR 和 dC 减 1,即(dR,dC)=(1,1)。如果发现左上角坐标跑到了右下角坐标的右方或下方, 整个过程就停止。已经打印的所有结果连起来就是我们要求的打印结果。具体请参看如下代码中的 spiralOrderPrint 方法,其中 printEdge 方法是转圈打印一个子矩阵的外层。

#include<iostream>
using namespace std;

void printCircle(int a[][4], int tR, int tC, int dR, int dC)
{
    if (tR == dR)
        for (int i = tC;i <= dC;++i)
            cout << a[tR][i] << " ";
    else if (tC == dC)
        for (int i = tR;i <= dR;++i)
            cout << a[i][tC] << " ";
    else {
        int curCol = tC, curRow = tR;
        while (curCol != dC)
            cout << a[tR][curCol++] << " ";
        while (curRow != dR)
            cout << a[curRow++][dC] << " ";
        while (curCol != tC)
            cout << a[dR][curCol--] << " ";
        while (curRow != tR)
            cout << a[curRow--][tC] << " ";
    }
}

void spiralOrderPrint(int a[][4], int rowCount, int colCount)
{
    int topRow = 0;
    int topCol = 0;
    int downRow = rowCount - 1;
    int downCol = colCount - 1;
    while (topRow <= downRow && topCol <= downCol)
        printCircle(a, topRow++, topCol++, downRow--, downCol--);
}


int main()
{
    int matrix[][4] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 };
    spiralOrderPrint(&matrix[0], 4, 4);
    system("pause");
}
原文地址:https://www.cnblogs.com/huangzhenxiong/p/7646432.html