Print N * n spiral Matrix

Source: Internet
Author: User

Both via and EMC have written this question.

Input N, print N * n matrix
For example, n = 3, print:

1 2 3

8 9 4

7 6 5

N = 4, print:

1 2 3 4

12 13 14 5

11 16 15 6

10 9 8 7

/* Spiral matrix */

# Include <stdio. h>

# Include <conio. h>

# Define right 0

# Define down 1

# Define left 2

# Define up 3

// N * n matrix

# Define N 5

Void printmatrix (int * A [], int N)

{

Int I, J;

For (I = 0; I <n; I ++)

{

For (j = 0; j <n; j ++)

{

Printf ("% 4D", a [I] [J]);

}

Printf ("/N ");

}

}

Void spiralmatrix (int * A [], int N) // int * A [] Note Interface Design

{

Int I, j; // coordinates

Int count; // counter

Int K; // a cyclic variable that controls the number of points on each edge.

Int direct; // indicates the direction to control the addition and deletion of rows and columns.

I = 0; // start point (0, 0)

J = 0;

Count = 1;

Direct = right;

While (n> 1) // the following code is available for Square

{

For (k = 0; k <n-1; k ++) // The number of points on each edge is (2n + 2 (n-2)/4 = 4 (n-1) /4 = n-1

{

A [I] [J] = count ++;

Switch (direct)

{

Case down:

I ++;

Break;

Case left:

J --;

Break;

Case up:

I --;

Break;

Case right:

J ++;

Break;

}

}

// If the orientation is up, the four sides are filled and the start point is returned. The step size is reduced by 2 and the position is corrected.

If (direct = up)

{

I ++;

J ++;

N-= 2;

}

// Change the direction

Direct = (direct + 1) % 4;

}

If (n = 1) // orphan

{

A [I] [J] = count;

}

}

Void spiralmatrix2 (int * A [], int N) // int * A [] note the interface design

{

Int K = 0, I = 0, j = 0;

Int COUNT = 1;

/// Pay attention to the changes in I and j, and draw a graph to understand it. just assign values in the order of the spiral matrix.

For (; k <(n + 1)/2; k ++)

{

While (j <n-k) A [I] [J ++] = count ++; I ++; j --; // The row above

While (I <n-k) A [I ++] [J] = count ++; I --; j --; // a column on the right

While (j> k-1) A [I] [j --] = count ++; I --; j ++; // the row below

While (I> K) A [I --] [J] = count ++; I ++; j ++; // a column on the left

}

}

In this method, the cyclic conditions in each while are different. Every time you finish an edge, you need to correct the IJ, which is more complex than the preceding method.

Recursive mode. Modify the spiralmatrix slightly. Pay attention to the conditions of the next recursive round and the conditions for the final exit.

Change the recursive function interface accordingly.

/*

* Matrix two-dimensional matrix Array

* (X, y): coordinates of the first element

* Start: the value of the first element.

* Width: The size and width of the matrix.

*/

Void spiralmatrix3 (int * matrix [], int X, int y, int width, int start)

{

Int I, j; // coordinates

Int K; // cyclic variable

Int direct; // Direction Indication

If (width <= 0) // recursive end condition. This is the case when the even number is used.

Return;

If (width = 1) {// when the matrix size is 1, the odd number

Matrix [x] [Y] = start;

Return;

}

I = 0;

J = 0;

Direct = right;

While (direct <up + 1) // exit after a circle

{

For (k = 0; k <width-1; k ++)

{

Matrix [x + I] [Y + J] = start ++;

Switch (direct)

{

Case down:

I ++;

Break;

Case left:

J --;

Break;

Case up:

I --;

Break;

Case right:

J ++;

Break;

}

}

Direct ++;

}

// After completing one lap, the step size is reduced by 2 and the position is corrected.

I ++;

J ++;

Width-= 2;

Spiralmatrix (matrix, x + I, Y + J, width, start); // fill again

}

Void main (void)

{

Int M [N] [N] = {0 };

Int * A [n];

Int I;

For (I = 0; I <n; I ++)

{

A [I] = m [I]; // the two-dimensional array cannot be directly used as a parameter, which is essentially different from the pointer pointing to the pointer.

}

// Spiralmatrix (A, N );

// Spiralmatrix2 (A, N );

Spiralmatrix3 (A, 0, 0, N, 1 );

Printmatrix (A, N );

Printf ("press any key to exit ...");

Getch ();

}

Contact Us

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.