I think you meant spiral traversal, #include <cstdio> #include <cstring> using namespace std;
#define REP(i, n) for(int i = 0; i < n; i++) const int MX = 1000; int a[MX][MX], seen[MX][MX], n; int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; int main() { scanf("%d", &n); REP(i, n) REP(j, n) scanf("%d", &a[i][j]); REP(i, n) memset(seen[i], 0, sizeof(seen[i])); int x, y, dir; x = y = dir = 0; REP(i, n*n) { printf("%d ", a[x][y]); seen[x][y] = 1; x += dx[dir]; y += dy[dir]; if((x<0) || (x>=n) || (y<0) || (y>=n) || seen[x][y]) { x -= dx[dir]; y -= dy[dir]; dir = (dir+1)%4; x += dx[dir]; y += dy[dir]; } } } On Jul 28, 11:57 am, Deoki Nandan <deok...@gmail.com> wrote: > there is no specification on complexity . if input matrix is > 1 2 3 > 4 5 6 > 7 8 9 > then after 180 rotation output should be > 1 2 3 6 9 8 7 4 5 > > On Wed, Jul 27, 2011 at 11:34 PM, amit karmakar > <amit.codenam...@gmail.com>wrote: > > > > > > > If you meant "rotate a 2D matrix by angle 180" of order n x n > > Then you cannot have a O(n) algo, Each of the n^2 elements must be > > accessed so you cannot have anything less than n^2 > > > On Jul 27, 10:59 pm, Puneet Gautam <puneet.nsi...@gmail.com> wrote: > > > Can anyone give an O(n) solution pls...?? > > > I think the above code is an O(n^2) solution.. > > > if i am not wrong...!!! > > > > On 7/27/11, amit <amit.codenam...@gmail.com> wrote: > > > > > #include <cstdio> > > > > #include <algorithm> > > > > using namespace std; > > > > > const int MX = 1000; > > > > int n, m; > > > > int a[MX][MX]; > > > > > int main() { > > > > scanf("%d%d", &n, &m); > > > > for(int i = 0; i < n; i++) > > > > for(int j = 0; j < m; j++) > > > > scanf("%d", &a[i][j]); > > > > > for(int i = 0; i < n/2; i++) > > > > for(int j = 0; j < m; j++) > > > > swap(a[i][j], a[n-i-1][m-j-1]); > > > > if(n&1) > > > > for(int j = 0; j < m/2; j++) > > > > swap(a[n/2][j], a[n/2][m-j-1]); > > > > > for(int i = 0; i < n; i++) { > > > > for(int j = 0; j < m; j++) > > > > printf("%d ", a[i][j]); > > > > printf("\n"); > > > > } > > > > } > > > > > On Jul 27, 7:54 pm, Anika Jain <anika.jai...@gmail.com> wrote: > > > >> is it lyk for {1,2,3 > > > >> 4,5,6, > > > >> 7,8,9} > > > >> to be {3,2,1, > > > >> 6,5,4, > > > >> 9,8,7} ?? > > > > >> On Wed, Jul 27, 2011 at 9:37 AM, Deoki Nandan <deok...@gmail.com> > > wrote: > > > >> > rotate a 2D matrix by angle 180 > > > > >> > -- > > > >> > **With Regards > > > >> > Deoki Nandan Vishwakarma > > > > >> > * > > > >> > * > > > > >> > -- > > > >> > You received this message because you are subscribed to the Google > > > >> > Groups > > > >> > "Algorithm Geeks" group. > > > >> > To post to this group, send email to algogeeks@googlegroups.com. > > > >> > To unsubscribe from this group, send email to > > > >> > algogeeks+unsubscr...@googlegroups.com. > > > >> > For more options, visit this group at > > > >> >http://groups.google.com/group/algogeeks?hl=en. > > > > > -- > > > > You received this message because you are subscribed to the Google > > Groups > > > > "Algorithm Geeks" group. > > > > To post to this group, send email to algogeeks@googlegroups.com. > > > > To unsubscribe from this group, send email to > > > > algogeeks+unsubscr...@googlegroups.com. > > > > For more options, visit this group at > > > >http://groups.google.com/group/algogeeks?hl=en. > > > -- > > You received this message because you are subscribed to the Google Groups > > "Algorithm Geeks" group. > > To post to this group, send email to algogeeks@googlegroups.com. > > To unsubscribe from this group, send email to > > algogeeks+unsubscr...@googlegroups.com. > > For more options, visit this group at > >http://groups.google.com/group/algogeeks?hl=en. > > -- > **With Regards > Deoki Nandan Vishwakarma > > * > * -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.