Non-recursive solution to N queen problem in C Language

Source: Internet
Author: User

Non-recursive solution to N queen problem in C Language

Queen N's problem is an old problem. You can read an algorithm book to see its introduction. In fact, the problem of Queen N can be solved using non-recursive methods, this effectively avoids recursive working stack overflow when N is too large, occupies a small amount of storage space, and runs at a fast speed. The code is simple and not complex, so it achieves both the running speed and the reasonable use of space, sometimes simplicity is also a kind of beauty, which means reliability and efficiency. Programmers who pursue the complexity and difficulty of understanding do not agree with this, but it is not necessary to describe the problems that can be solved by simple design with complicated data representation.

The Code is as follows (c ):

# Include <stdio. h>
# Include <malloc. h>
# Include <math. h>
Int place (int I, int k, int * p); // check whether k + 1 rows of I columns can be placed in the queen; 1 is returned; otherwise, 0 is returned.
Int find (int n, int * p, int k); // you can search in the k + 1 line for the location where the queen can be placed. If the location is found, the column mark is returned. Otherwise, 0 is returned.
Void output (int n, int * p); // output a solution to the n queen problem

Void main ()
{
Int k, n;
Int m;
Int * p;

Printf ("you want to solve n queens problem \ n ");
Printf ("n = ");
Scanf ("% d", & n); // enter the problem Scale

P = (int *) malloc (n * sizeof (int); // p [k] indicates the column in which k + 1 rows of queens are located
For (k = 0; k <n; k ++)
{
P [k] = 0; // initialize the array p
}

K = 0; // initialize to the first line
M = 0; // The number of records for variable Initialization
Loop: if (k = 0) // enter or backtrack to the first line
{
P [k] = p [k] + 1; // test the next column

If (p [k]> n) // all columns in the first row are tested and end with backtracing.
{
Goto exit;
}

K ++; // test the next row
Goto loop;
}
Else
{
If (find (n, p, k) = 0) // The row k + 1 does not find the location where the queen can be placed.
{
P [k] = 0; // necessary cleanup
K --; // trace
Goto loop;
}
Else
{
P [k] = find (n, p, k); // assign the position found in the k + 1 row to p [k]
If (k! = (N-1) // not all queens have been placed
{
K ++; // test the next row
Goto loop;
}
Else // all the queens are successfully placed. Find the solution.
{
M ++;
Printf ("The % dnd solution \ n", m );
Output (n, p); // output Solution

Goto loop; // backtracking
}
}
}

Exit :;
Printf ("There are % d solutions in total \ n", m); // number of output solutions
}

Int place (int I, int k, int * p)
{
Int m;

For (m = 0; m <k; m ++)
{
If (p [m] = I) | (abs (m-k) = abs (p [m]-I ))) // The column I in the k + 1 row is not a valid position
Return (0 );
}

Return (1); // k + 1 rows of I columns are valid locations
}

Int find (int n, int * p, int k)
{
Int I;
I = p [k];

For (I ++; I <= n; I ++)
{
If (place (I, k, p) = 1) // locate the columns that can be placed in the Queen's column in the k + 1 row
Return (I); // return this column
}
Return (0); // No columns can be placed in the k + 1 row
}

Void output (int n, int * p)
{
Int I, j;

For (I = 0; I <n; I ++)
{
For (j = 1; j <= n; j ++)
{
If (j = p [I])
Printf ("1 ");
Else
Printf ("0 ");
}
Printf ("\ n ");
}
Printf ("\ n ");
}

The recursive code of n queen's question is attached:

This program segment is self-created (there is no need to copy and paste the existing code in the textbook). It doesn't make a lot of sense, because it is complicated. When n <= 8, it can output the correct results, STACKOVERFLOW directly at n> 8 hours, so it is not practical for everyone to look at it.

# Include <stdio. h>
# Include <math. h>
# Include <malloc. h>
# Include <stdlib. h>
Void print (int n );
Void put (int k, int n, int * q, int * p );
Void output (int n, int * p );
Int law (int k, int I, int * p );

Void main ()
{
Int n;
Printf ("you want to solve the problem of n queens the n =? \ N ");
Scanf ("% d", & n );
Print (n );
}

Void print (int n)
{
Int number, I;
Int * p;
Number = 0;
P = (int *) malloc (n * sizeof (int ));
For (I = 0; I <n; I ++)
* (P + I) = 0;

Put (1, n, & number, p );

Printf ("There are % d solutions in total \ n", number );
Free (p );
}
Void put (int k, int n, int * q, int * p)
{
Int I;
If (k = 1)
{
(* P) ++;
If (* p)> n)
Return;
Put (k + 1, n, q, p );
}
Else
{
I = * (p + k-1 );
I ++;
While (I <= n)
{
If (law (k, I, p ))
{
* (P + k-1) = I;
If (k = n)
{
(* Q) ++;
Printf ("The % dnd solution \ n", * q );
Output (n, p );
Put (k, n, q, p );
}
Else
{
Put (k + 1, n, q, p );
}
Break;
}
I ++;
}
If (I> n)
{
* (P + k-1) = 0;
Put (K-1, n, q, p );
}
}
}

Void output (int n, int * p)
{
Int j, c;
For (j = 1; j <= n; j ++)
{
For (c = 1; c <= n; c ++)
{
If (c = * (p + J-1 ))
Printf ("% d", 1 );
Else
Printf ("% d", 0 );
}
Printf ("\ n ");
}
Printf ("\ n ");
}

Int law (int k, int I, int * p)
{
Int m;
For (m = 1; m <k; m ++)
{
If (* (p + s-1) = I) | (abs (m-k) = abs (* (p + s-1)-I )))
Return (0 );
}
Return (1 );
}

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.