Data Structure 0103 Nottingham & Eight Queens

Source: Internet
Author: User

The recursive algorithm is mainly realized from Hanoi and eight Queens problem.

Hanoi

#include <stdio.h>

void Move (int n, char X,char y, char z)
{
if (1==n)
{
printf ("%c-->%c\n", x,z);
}
Else
{
Move (N-1,x,z,y); Move n-1 plates from X to Y with Z
printf ("%c-->%c\n", x,z); Move the nth plate from X to Z
Move (N-1,Y,X,Z); Move n-1 plates from Y to Z with x
}
}

int main ()
{
int n;

printf ("Please enter the number of layers Hanoi: \ n");
scanf ("%d", &n);
printf ("Move the steps below: \ n");
Move (n, ' X ', ' Y ', ' Z ');

return 0;
}

Eight queens:

Put eight queens on the 8*8 chess, so that they cannot attack each other, that is, any two queens cannot be in the same row, the same column, ask how many kinds of pendulum (92 kinds, poor lifting method):

The code is as follows:

#include <stdio.h>
#include <stdlib.h>

int count=0;

int notdanger (int row,int j,int (*chess) [8])
{
int i,k,flag1=0,flag2=0,flag3=0,flag4=0,flag5=0;
//Judging column orientation
for (i=0;i<8;i++)
{
if (* (* (chess+i) +j)!=0)
{
flag1=1;
break;
}
}
//Judging upper left
for (i=row,k=j;i>=0 && k>=0;i--, k--)
{
if (* (* (chess+i) +k)!=0)
{
Flag2=1;
break;
}
}
//judgment right bottom
for (i=row,k=j;i<8 && k<8;i++,k++)
{
if (* (* (chess+i) +k)!=0)
{
Flag3=1;
break;
}
}
//judgment upper right
for (i=row,k=j;i>=0 && k<8;i--, k++)
{
if (* (* (chess+i) +k)!=0)
{
Flag4=1;
break;
}
}
//Judging lower left
for (i=row,k=j;i<8 && k>=0;i++,k--)
{
if (* (* (chess+i) +k)!=0)
{
Flag5=1;
break;
}
}
if (Flag1 | | flag2 | | flag3 | | flag4 | | flag5)
{
return 0;
}
Else
{
return 1;
}
}

void Eightqueen (int row, int n, int (*chess) [8])//Three parameters represent rows, columns, and pointers to expected each row
{
int i,j,chess2[8][8];

for (i=0;i<8;i++)
{
for (j=0;j<8;j++)
{
CHESS2[I][J]=CHESS[I][J];
}
}

if (8==row)
{
printf ("%d \ n", count+1);
for (i=0;i<8;i++)
{
for (j=0;j<8;j++)
{
printf ("%d", * (* (chess2+i) +j));
}
printf ("\ n");
}
printf ("\ n");
count++;
}
Else
{
for (j=0;j<n;j++)
{
if (Notdanger (row,j,chess))//Determine if there are two queen conflicts in this position
{
for (i=0;i<8;i++)
{
* (* (chess2+row) +i) = 0;
}
* (* (chess2+row) +j) = 1;
Eightqueen (ROW+1,N,CHESS2);
}
}
}
}

int main ()
{
int chess[8][8],i,j;

for (i=0;i<8;i++)//0 means no data, 1 means to store queen
{
for (j=0;j<8;j++)
{
chess[i][j]=0;
}
}

Eightqueen (0, 8, chess);

printf ("There are a total of%d workarounds!") \ n ", count);

return 0;
}

Data Structure 0103 Nottingham & Eight Queens

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.