2020-05-29 03:27:49 +08:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief Print the elements of a matrix traversing it spirally
|
|
|
|
*/
|
2019-11-28 20:29:54 +08:00
|
|
|
#include <iostream>
|
|
|
|
|
2020-05-29 03:27:49 +08:00
|
|
|
/** Arrange sequence of numbers from '1' in a matrix form
|
|
|
|
* \param [out] a matrix to fill
|
|
|
|
* \param [in] r number of rows
|
|
|
|
* \param [in] c number of columns
|
|
|
|
*/
|
2020-05-30 07:26:30 +08:00
|
|
|
void genArray(int **a, int r, int c)
|
|
|
|
{
|
2019-11-28 20:29:54 +08:00
|
|
|
int value = 1;
|
2020-05-30 07:26:30 +08:00
|
|
|
for (int i = 0; i < r; i++)
|
|
|
|
{
|
|
|
|
for (int j = 0; j < c; j++)
|
|
|
|
{
|
2019-11-28 20:29:54 +08:00
|
|
|
a[i][j] = value;
|
2020-05-29 03:27:49 +08:00
|
|
|
std::cout << a[i][j] << " ";
|
2019-11-28 20:29:54 +08:00
|
|
|
value++;
|
|
|
|
}
|
2020-05-29 03:27:49 +08:00
|
|
|
std::cout << std::endl;
|
2019-11-28 20:29:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-29 03:27:49 +08:00
|
|
|
/** Traverse the matrix spirally and print the sequence of elements
|
|
|
|
* \param [in] a matrix to read from
|
|
|
|
* \param [in] r number of rows
|
|
|
|
* \param [in] c number of columns
|
|
|
|
*/
|
2020-05-30 07:26:30 +08:00
|
|
|
void spiralPrint(int **a, int r, int c)
|
|
|
|
{
|
2019-11-28 20:29:54 +08:00
|
|
|
int startRow = 0, endRow = r - 1;
|
|
|
|
int startCol = 0, endCol = c - 1;
|
|
|
|
int cnt = 0;
|
|
|
|
|
2020-05-30 07:26:30 +08:00
|
|
|
while (startRow <= endRow && startCol <= endCol)
|
|
|
|
{
|
2020-05-29 03:27:49 +08:00
|
|
|
/// Print start row
|
2020-05-30 07:26:30 +08:00
|
|
|
for (int i = startCol; i <= endCol; i++, cnt++)
|
|
|
|
{
|
2020-05-29 03:27:49 +08:00
|
|
|
std::cout << a[startRow][i] << " ";
|
2019-11-28 20:29:54 +08:00
|
|
|
}
|
|
|
|
startRow++;
|
|
|
|
|
2020-05-29 03:27:49 +08:00
|
|
|
/// Print the end col
|
2020-05-30 07:26:30 +08:00
|
|
|
for (int i = startRow; i <= endRow; i++, cnt++)
|
|
|
|
{
|
2020-05-29 03:27:49 +08:00
|
|
|
std::cout << a[i][endCol] << " ";
|
2019-11-28 20:29:54 +08:00
|
|
|
}
|
|
|
|
endCol--;
|
|
|
|
|
2020-05-29 03:27:49 +08:00
|
|
|
/// Print the end row
|
2020-05-30 07:26:30 +08:00
|
|
|
if (cnt == r * c)
|
|
|
|
{
|
2019-11-28 20:29:54 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-05-30 07:26:30 +08:00
|
|
|
for (int i = endCol; i >= startCol; i--, cnt++)
|
|
|
|
{
|
2020-05-29 03:27:49 +08:00
|
|
|
std::cout << a[endRow][i] << " ";
|
2019-11-28 20:29:54 +08:00
|
|
|
}
|
|
|
|
endRow--;
|
|
|
|
|
2020-05-29 03:27:49 +08:00
|
|
|
/// Print the start Col
|
2020-05-30 07:26:30 +08:00
|
|
|
if (cnt == r * c)
|
|
|
|
{
|
2019-11-28 20:29:54 +08:00
|
|
|
break;
|
|
|
|
}
|
2020-05-30 07:26:30 +08:00
|
|
|
for (int i = endRow; i >= startRow; i--, cnt++)
|
|
|
|
{
|
2020-05-29 03:27:49 +08:00
|
|
|
std::cout << a[i][startCol] << " ";
|
2019-11-28 20:29:54 +08:00
|
|
|
}
|
|
|
|
startCol++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-29 03:27:49 +08:00
|
|
|
/** main function */
|
2020-05-30 07:26:30 +08:00
|
|
|
int main()
|
|
|
|
{
|
2019-11-28 20:29:54 +08:00
|
|
|
int r, c;
|
2020-05-29 03:27:49 +08:00
|
|
|
std::cin >> r >> c;
|
|
|
|
int **a = new int *[r];
|
|
|
|
for (int i = 0; i < r; i++) a[i] = new int[c];
|
|
|
|
|
2019-11-28 20:29:54 +08:00
|
|
|
genArray(a, r, c);
|
|
|
|
spiralPrint(a, r, c);
|
|
|
|
|
2020-05-29 03:27:49 +08:00
|
|
|
for (int i = 0; i < r; i++) delete[] a[i];
|
|
|
|
delete[] a;
|
2019-11-28 20:29:54 +08:00
|
|
|
return 0;
|
|
|
|
}
|