螺旋输出矩阵

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

 

 

 

 

 

 

输入格式

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

输出格式

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

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

  1 #include <iostream>
  2 using namespace std;
  3 int main() {
  4     int m, n, num;
  5     cin >> m >> n;
  6     int goRight = 0, goDown = 0, goUp = 0, goLeft = 0;
  7     int currentRow = 0, currentCol = n - 1, previousRow = 0, previousCol = 0;
  8     int matrix[m][n];
  9     int flag[m][n];
 10 
 11     // 构建矩阵
 12     for (int i = 0; i < m; ++i) {
 13         for (int j = 0; j < n; ++j) {
 14             cin >> matrix[i][j];
 15             flag[i][j] = 0;
 16         }
 17     }
 18     // 如果只有一行
 19     if (m == 1) {
 20         for (int i = 0; i < n; ++i) {
 21             if (i == 0) {
 22                 cout << matrix[0][i];
 23             } else {
 24                 cout << " " << matrix[0][i];
 25             }
 26         }
 27         return 0;
 28     }
 29     //如果只有一列
 30     if (n == 1) {
 31         for (int i = 0; i < m; ++i) {
 32             if (i == 0) {
 33                 cout << matrix[i][0];
 34             } else {
 35                 cout << " " << matrix[i][0];
 36             }
 37         }
 38         return 0;
 39     }
 40     int a = 0;
 41     goRight = 1;
 42     for(num = 0; num < m * n;) {
 43         if (goRight == 1 && goDown == 0 && goUp == 0 && goLeft == 0) {
 44             int i;
 45             if (a == 0) {
 46                 for (i = previousCol; i <= currentCol; i++) {
 47                     if (i == previousCol) {
 48                         cout << matrix[currentRow][i];
 49                     } else {
 50                         cout << " " << matrix[currentRow][i];
 51                     }
 52                     flag[currentRow][i] = 1;
 53                     num ++;
 54                     //cout << "current Row: " << currentRow << ", current Column: " << currentCol << ", previous Row: " << previousRow << ", previous Column: " << previousCol << ", num: " << num << endl;
 55                 }
 56                 a = 1;
 57             } else if (a == 1) {
 58                 for (i = previousCol + 1; i <= currentCol; i++) {
 59                     cout  << " " << matrix[currentRow][i];
 60                     flag[currentRow][i] = 1;
 61                     num ++;
 62                     //cout << "current Row: " << currentRow << ", current Column: " << currentCol << ", previous Row: " << previousRow << ", previous Column: " << previousCol << ", num: " << num << endl;
 63                 }
 64             }
 65 
 66             // 如果该行末尾下的元素没有被访问过
 67             if (flag[currentRow + 1][currentCol] == 0) {
 68                 goRight = 0;
 69                 goDown = 1;
 70                 int temp = currentRow;
 71                 currentRow = m - 1 - previousRow;
 72                 previousRow = temp;
 73                 previousCol = currentCol;
 74                 //cout << "Now go down, current Row: " << currentRow << ", current Column: " << currentCol << ", previous Row: " << previousRow << ", previous Column: " << previousCol << endl << endl;
 75                 continue;
 76             } else {
 77                 break;
 78             }
 79 
 80         }
 81         else if (goRight == 0 && goDown == 1 && goUp == 0 && goLeft == 0) {
 82             int i;
 83             for (i = previousRow + 1; i <= currentRow; i++) {
 84                 cout << " " << matrix[i][currentCol];
 85                 flag[i][currentCol] = 1;
 86                 num ++;
 87                 //cout << "current Row: " << currentRow << ", current Column: " << currentCol << ", previous Row: " << previousRow << ", previous Column: " << previousCol << ", num: " << num <<  endl;
 88             }
 89             // 如果该列末尾左边的元素没有被访问过
 90             if (flag[currentRow][currentCol - 1] == 0) {
 91                 goDown = 0;
 92                 goLeft = 1;
 93                 int temp = currentCol;
 94                 currentCol = n - 1 - previousCol;
 95                 previousCol = temp;
 96                 previousRow = currentRow;
 97                 //cout << "Now go left, current Row: " << currentRow << ", current Column: " << currentCol << ", previous Row: " << previousRow << ", previous Column: " << previousCol << endl << endl;
 98                 continue;
 99             } else {
100                 break;
101             }
102         }
103         else if (goRight == 0 && goDown == 0 && goUp == 0 && goLeft == 1) {
104             int i;
105             for (i = previousCol - 1; i >= currentCol ; i--) {
106                 cout << " " << matrix[currentRow][i];
107                 flag[currentRow][i] = 1;
108                 num ++;
109                 //cout << "current Row: " << currentRow << ", current Column: " << currentCol << ", previous Row: " << previousRow << ", previous Column: " << previousCol << ", num: " << num <<  endl;
110             }
111             // 如果该行末尾上面的元素没有被访问过
112             if (flag[currentRow - 1][currentCol] == 0) {
113                 goLeft = 0;
114                 goUp = 1;
115                 int temp = currentRow;
116                 currentRow = m - 1 + 1 - previousRow;
117                 previousRow = temp;
118                 previousCol = currentCol;
119                 //cout << "Now go up, current Row: " << currentRow << ", current Column: " << currentCol << ", previous Row: " << previousRow << ", previous Column: " << previousCol << endl << endl;
120                 continue;
121             } else {
122                 break;
123             }
124         }
125         else if (goRight == 0 && goDown == 0 && goUp == 1 && goLeft == 0) {
126             int i;
127             for (i = previousRow - 1; i >= currentRow; i--) {
128                 cout << " " << matrix[i][currentCol];
129                 flag[i][currentCol] = 1;
130                 num ++;
131                 //cout << "current Row: " << currentRow << ", current Column: " << currentCol << ", previous Row: " << previousRow << ", previous Column: " << previousCol << ", num: " << num <<  endl;
132             }
133             // 如果该列末尾右边的元素没有被访问过
134             if (flag[currentRow][currentCol + 1] == 0) {
135                 goUp = 0;
136                 goRight = 1;
137                 int temp = currentCol;
138                 currentCol = n - 1 - 1 - previousCol;
139                 previousCol = temp;
140                 previousRow = currentRow;
141                 //cout << "Now go right, current Row: " << currentRow << ", current Column: " << currentCol << ", previous Row: " << previousRow << ", previous Column: " << previousCol << endl << endl;
142                 continue;
143             } else {
144                 break;
145             }
146         }
147 
148     }
149 
150     return 0;
151 }

 

posted @ 2017-08-22 11:20  Xehanort_Hajime  阅读(868)  评论(0)    收藏  举报