Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Blog
spiral traversal of a matrix
#include <bits/stdc++.h> using namespace std; int main() { //code int t; cin>>t; while(t--) { int m,n;cin>>m>>n; int vect[m][n]; for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { cin>>vect[i][j]; } } int left=0; int right=n-1; int top=0; int bottom=m-1; while(top<=bottom && left<=right) { for(int i=left;i<=right;i++) cout<<vect[top][i]<<" "; top++; for(int i=top;i<=bottom;i++) cout<<vect[i][right]<<" "; right--; if(top<=bottom) { for(int i=right;i>=left;i--) { cout<<vect[bottom][i]<<" "; } bottom--; } if(left<=right) { for(int i=bottom;i>=top;i--) { cout<<vect[i][left]<<" "; } left++; } } cout<<endl; } return 0; }
run
|
edit
|
history
|
help
0
Please
log in
to post a comment.
Hello World C++ - minimal
Matrix spiral print
Click 5
Day3
Elevator 3
Ineritance
precision and fixed point notation
Shultz_Lab2.CPP
rstring
Hii
stackse - search stackoverflow differently
Please log in to post a comment.