-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrixnm.cpp
More file actions
55 lines (44 loc) · 941 Bytes
/
matrixnm.cpp
File metadata and controls
55 lines (44 loc) · 941 Bytes
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
#include<vector>
#include<iostream>
#include<algorithm>
#include<string>
#include<unordered_map>
#include<stack>
#include<queue>
#include<map>
#include<set>
using namespace std;
//sprial Traversal
#define mp(x,y) make_pair(x,y)
int main(int argc, char const *argv[])
{
int n,m;
cin>>n>>m;
vector<vector<int>> ans(n,vector<int>(m,-1));
int rs=0,cs=0,ce=m-1,re=n-1,coun=1;
while(cs<=ce && rs<=re){
for(int i=cs;i<=ce;i++){
ans[rs][i]=coun++;
}
for(int i = rs+1;i<=re;i++){
ans[i][ce]=coun++;
}
for(int i=ce-1;i>=cs;i--){
ans[re][i]=coun++;
}
for(int i=re-1;i>=rs+1;i--){
ans[i][cs]=coun++;
}
cs++;
rs++;
ce--;
re--;
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
printf("%3d ",ans[i][j]);
}
cout<<endl;
}
return 0;
}