-
Notifications
You must be signed in to change notification settings - Fork 14
/
28.cpp
63 lines (60 loc) · 1.33 KB
/
28.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//Print the matrix in a Spiral manner. [Very IMP ]
#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for(int i=a;i<b;i++)
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define endl "\n"
#define mod 1000000007
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef unordered_map<int, int> umap;
void inputarr(int arr[], int n) {
REP(i, 0, n) {
cin >> arr[i];
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
int arr[n][m];
REP(i, 0, n) {
REP(j, 0, m) {
cin >> arr[i][j];
}
}
REP(i, 0, n) {
REP(j, 0, m) {
cout << arr[i][j] << " ";
}
cout << endl;
}
int row_start = 0, column_start = 0, row_end = n - 1, column_end = m - 1;
while (row_start <= row_end && column_start <= column_end) {
for (int col = column_start; col <= column_end; col++) {
cout << arr[row_start][col] << " ";
}
row_start++;
for (int row = row_start; row <= row_end; row++) {
cout << arr[row][column_end] << " ";
}
column_end--;
if (row_start <= row_end) {
for (int col = column_end; col >= column_start; col--) {
cout << arr[row_end][col] << " ";
}
}
row_end--;
if (column_start <= column_end) {
for (int row = row_end; row >= row_start; row--) {
cout << arr[row][column_start] << " ";
}
}
column_start++;
}
}