# Programmer interview question selection (39): An Autodesk pen question

Source: Internet
Author: User
In last May, I took part in the algorithm questions for Autodesk intern recruitment. At that time, I had a tight schedule and had a large number of questions. I didn't have time to do this because I was too lazy. I didn't see the answer from Cui, today, I suddenly thought about it. It's easier than I thought :)

/* ===================================================== ========================================================== =
Question: given an m x n matrix, print all matrix elements clockwise starting from the element in the upper left corner.
Key points: the direction control and left and right control of the traversal process, and the upper and lower control
Author: sunnyrain
Date: 2007.9.3 pm
Running Environment: VC ++ 6.0
========================================================== ========================================================== = */
# Include <iostream>
# Include <vector>
Using namespace STD;

// Define the direction
Enum direction {toright, down, toleft, up };

// Change the direction function
Void turndirect (Direction & cur)
{
Switch (cur)
{
Case toright:
Cur = down;
Break;
Case down:
Cur = toleft;
Break;
Case toleft:
Cur = up;
Break;
Case up:
Cur = toright;
Break;
}
}

// Traverse the Matrix
Void traverse (vector <int> & vv)
{
Int M = VV [0]. Size ();
Int n = vv. Size ();
Int up = 0, down = N, Left = 0, Right = m;
Int I = 0, j = 0;

Direction curd = toright;
For (; up! = Down | left! = Right ;)
{
Switch (curd)
{
Case toright: // traverse from left to right
For (j = left; j <right; j ++)
Cout <VV [I] [J] <"";
Cout <Endl;
-- J; // column coordinate return
Up ++; // move down the upper bound of the row after traversing
Turndirect (curd); // Changes the traversal direction
Break;
Case down: // traverse from top to bottom
For (I = up; I <down; I ++)
Cout <VV [I] [J] <"";
Cout <Endl;
-- I; // out-of-bounds rollback
Right --; // After traversing a column, move the right field to the left.
Turndirect (curd );
Break;
Case toleft: // traverse from right to left
For (j = right-1; j> = left; -- J)
Cout <VV [I] [J] <"";
Cout <Endl;
+ + J; // out-of-bounds rollback
Down --; // move down the world
Turndirect (curd );
Break;
Case up: // traverse from bottom up
For (I = down-1; I> = up; -- I)
Cout <VV [I] [J] <"";
Cout <Endl;
+ + I; // out-of-bounds rollback
Left ++; // shift left to right
Turndirect (curd );
Break;
}
}
}

Int main ()
{
Int m, n, I, J, K = 0;
Cout <"Please input the M and N of the matrix (M * n):" <Endl;
Cin> m> N;
Vector <vector <int> VEC (N );

// Initialization Matrix
For (I = 0; I <n; I ++)
For (j = 0; j <m; j ++)
VEC [I]. push_back (++ K );

// Normal traversal Matrix
For (I = 0; I <n; I ++)
{
For (j = 0; j <m; j ++)
Cout <VEC [I] [J] <"/t ";
Cout <Endl;
}

// Clockwise spiral traversal Matrix
Traverse (VEC );
Return 0;
}

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.