Eight Queens

xiaoxiao2021-03-05  29

#include

#include

Void queen ();

Void print ();

Int Colflag [8], Upflag [15], DOWNFLAG [15];

INT n = 0;

Char a [8] [8];

Void Queen (int ROW)

{

INT COL;

For (COL = 0; Col <8; Col )

IF (Colflag [col] && Upflag [col row] && downflag [row-col 7])

{

Colflag [col] = 0; Upflag [col row] = 0; DOWNFLAG [ROW-COL 7] = 0;

A [ROW] [COL] = '@';

IF (row == 7)

PRINT ();

Else

Queen (ROW 1);

// If the previous queen is placed, it will not satisfy the requirements in any case, then backtrack, reset

COLFLAG [col] = 1; Upflag [col row] = 1; DOWNFLAG [ROW-COL 7] = 1;

A [ROW] [COL] = '*';

}

Row -;

}

Void print ()

{

N ;

Printf ("% d / n", n);

For (int i = 0; i <8; i )

{

For (int J = 0; j <8; j )

Printf ("% C", A [i] [j]);

Printf ("/ n");

}

Printf ("Press Any Key To Continue / N);

Getch ();

}

void main ()

{

For (int i = 0; i <8; i )

{

COLFLAG [I] = 1;

For (int J = 0; j <8; j )

a [i] [j] = '*';

}

FOR (i = 0; i <15; i )

{

Upflag [i] = 1;

Downflag [i] = 1;

}

Queen (0);

}

转载请注明原文地址:https://www.9cbs.com/read-34228.html

New Post(0)