Description
Given an array of integers with row and col columns, it is required to start from the elements of array[0][0] and traverse the entire array clockwise from the outside to the inside according to the shape. as the picture shows:
Input
There are two integers on the first line of input, row and col in sequence.
There are row rows remaining, and each row contains col integers, forming a two-dimensional integer array.
(Note: The input row and col are guaranteed to be 0 <row <100, 0 <col <100) Output Each integer is
output
in the order of traversal. Each integer occupies one line.
Sample input
4 4
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
Sample output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;
#define MAX 101
int main()
{
int col,row,a[MAX][MAX],count,k,i,j;
cin>>row>>col;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d", &a[i][j]);
}
}
if(row==1)
{
i=0;
j=0;
for(j;j<col;j++)
{
cout<<a[i][j]<<endl;
}
}else if(col==1)
{
i=0;
j=0;
for(i;i<row;i++)
{
cout<<a[i][j]<<endl;
}
}else
{
i=0;
j=0;
count = col*row;
while(count>0)
{
for(k=1;k<col;k++)
{
cout<<a[i][j++]<<endl;
count--;
}
for(k=1;k<row;k++)
{
cout<<a[i++][j]<<endl;
count--;
}
for(k=1;k<col;k++)
{
cout<<a[i][j--]<<endl;
count--;
}
for(k=1;k<row;k++)
{
cout<<a[i--][j]<<endl;
count--;
}
row -= 2;
col -= 2;
i++;
j++;
if(row==1)
{
for(k=1;k<=col;k++)
{
cout<<a[i][j++]<<endl;
count--;
}
} else if(col==1)
{
for(k=1;k<=row;k++)
{
cout<<a[i++][j]<<endl;
count--;
}
}
}
}
return 0;
}