给定一个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;
}