hi all , there is a problem which demands print a matrix in spiral order, for example if the matrix is like the one below:

1 2 3 4 5
6 7 8 9 10
11 12 13 14 15

then the print order should be " 1 2 3 4 5 10 15 14 13 12 11 6 7 8 9"
how to do this fairily?

The solution I have thought out is by defining four states {left,down,right,up},and move to the next place based on the current states and whether the next place has been visited or overpass the boundary,if it is ,then change the state to the next and go on.

the code body is something like this:
Code:
for(int i = 0 ; i < m*n; i++){
switch(state){
case right: move to right if we can, or change state to down
case left:
case down:
case up:
}
}
but I think this method is some tedious and messy, any one can give good idea?

thanks.