85 lines
1.8 KiB
Plaintext
85 lines
1.8 KiB
Plaintext
|
/*
|
|||
|
file name: queen.c
|
|||
|
Description: <20>Q<EFBFBD>λ<EFBFBD><CEBB>j<EFBFBD>k<EFBFBD>D<EFBFBD>X 8 <20>ӬӦZ<D3A6><5A><EFBFBD>D<EFBFBD><44><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
|
|||
|
#include <stdio.h>
|
|||
|
#include <stdlib.h>
|
|||
|
|
|||
|
#define TRUE 1
|
|||
|
#define FALSE 0
|
|||
|
#define MAXQUEEN 5
|
|||
|
#define ABS(x) ((x>0) ?(x): -(x)) /* <20>Dx<44><78><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
|
|||
|
/* <20>s<EFBFBD><73> 5 <20>ӬӦZ<D3A6><5A><EFBFBD>C<EFBFBD><43><EFBFBD>m,<2C>}<7D>C<EFBFBD><43><EFBFBD>Ь<EFBFBD><D0AC>ӦZ<D3A6><5A><EFBFBD><EFBFBD><EFBFBD>C<EFBFBD><43> */
|
|||
|
int queen[MAXQUEEN];
|
|||
|
int total_solution; /* <20>p<EFBFBD><70><EFBFBD>@<40><><EFBFBD>X<EFBFBD>ո<EFBFBD> */
|
|||
|
|
|||
|
/* <20><><EFBFBD>ƭ쫬<C6AD>ŧi */
|
|||
|
void place(int);
|
|||
|
int attack(int, int);
|
|||
|
void output_solution(void);
|
|||
|
|
|||
|
int main()
|
|||
|
{
|
|||
|
place(0); /* <20>q<EFBFBD><71> 0 <20>ӬӦZ<D3A6>}<7D>l<EFBFBD>\<5C><><EFBFBD>ܴѽL */
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|
|||
|
void place(int q)
|
|||
|
{
|
|||
|
int i;
|
|||
|
|
|||
|
i = 0;
|
|||
|
while ( i < MAXQUEEN ) {
|
|||
|
if (!attack(i, q)) { /*<2A>ӦZ<D3A6><5A><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
|||
|
queen[q] = i; /* <20>x<EFBFBD>s<EFBFBD>ӦZ<D3A6>Ҧb<D2A6><62><EFBFBD>C<EFBFBD><43><EFBFBD>m */
|
|||
|
/* <20>P<EFBFBD>_<EFBFBD>O<EFBFBD>_<EFBFBD><5F><EFBFBD><EFBFBD><EFBFBD>@<40>ո<EFBFBD> */
|
|||
|
if (q == MAXQUEEN-1)
|
|||
|
output_solution(); /* <20>C<EFBFBD>X<EFBFBD><58><EFBFBD>ո<EFBFBD> */
|
|||
|
else
|
|||
|
place(q+1); /* <20>_<EFBFBD>h<EFBFBD>~<7E><><EFBFBD>\<5C>U<EFBFBD>@<40>ӬӦZ */
|
|||
|
}
|
|||
|
i++;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/* <20><><EFBFBD>զb(row, col)<29>W<EFBFBD><57><EFBFBD>ӦZ<D3A6>O<EFBFBD>_<EFBFBD>D<EFBFBD><44><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
<20>Y<EFBFBD>D<EFBFBD><44><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>h<EFBFBD>Ǧ^<5E>Ȭ<EFBFBD> 1<>A<EFBFBD>_<EFBFBD>h<EFBFBD>Ǧ^ 0 */
|
|||
|
int attack(int row, int col)
|
|||
|
{
|
|||
|
int i, atk = FALSE;
|
|||
|
int offset_row, offset_col;
|
|||
|
|
|||
|
i = 0;
|
|||
|
while (!atk && i < col) {
|
|||
|
offset_col = ABS(i - col);
|
|||
|
offset_row = ABS(queen[i] - row);
|
|||
|
/* <20>P<EFBFBD>_<EFBFBD><5F><EFBFBD>ӦZ<D3A6>O<EFBFBD>_<EFBFBD>b<EFBFBD>P<EFBFBD>@<40><>,<2C>ӦZ<D3A6>O<EFBFBD>_<EFBFBD>b<EFBFBD>﨤<EFBFBD>u<EFBFBD>W */
|
|||
|
/* <20>Y<EFBFBD>ӦZ<D3A6>P<EFBFBD>@<40><><EFBFBD>ι﨤<CEB9>u<EFBFBD>W<EFBFBD>A<EFBFBD>h<EFBFBD>|<7C><><EFBFBD>ͧ<EFBFBD><CDA7><EFBFBD>, atk == TRUE */
|
|||
|
atk = (queen[i] == row) || (offset_row == offset_col);
|
|||
|
i++;
|
|||
|
}
|
|||
|
return atk;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/*<2A>C<EFBFBD>X 5 <20>ӬӦZ<D3A6><5A><EFBFBD><EFBFBD>*/
|
|||
|
void output_solution()
|
|||
|
{
|
|||
|
int x,y;
|
|||
|
|
|||
|
total_solution += 1;
|
|||
|
printf("Solution #%d\n\t", total_solution);
|
|||
|
|
|||
|
for ( x = 0; x < MAXQUEEN; x++ ) {
|
|||
|
for ( y = 0; y< MAXQUEEN; y++ )
|
|||
|
if ( x == queen[y] )
|
|||
|
printf("Q");
|
|||
|
else
|
|||
|
printf("-");
|
|||
|
printf("\n\t");
|
|||
|
}
|
|||
|
printf("\n");
|
|||
|
}
|