#include <stdio.h>
#include <stdlib.h>
int flag_a[8];
int flag_b[15];
int flag_c[15];
int pos[8];
typedef struct
{
int max;
int prt;
int *stk;
} IntStack;
int Initialize(IntStack *s, int max)
{
s->prt = 0;
if ((s->stk = (int *)calloc(max, sizeof(int))) == NULL)
{
s->max = 0;
return -1;
}
s->max = max;
return 0;
}
int Push(IntStack *s, int x)
{
if (s->prt >= s->max)
return -1;
s->stk[s->prt++] = x;
return 0;
}
int Pop(IntStack *s, int *x)
{
if (s->prt <= 0)
return -1;
*x = s->stk[--s->prt];
return 0;
}
int IsEmpty(const IntStack *s)
{
return s->prt <= 0;
}
void print(void)
{
int i;
for (i = 0; i < 8; i++)
printf("%2d", pos[i]);
putchar('\n');
}
int main(void)
{
IntStack ii, jj;
Initialize(&ii, 100);
Initialize(&jj, 100);
int i = 0, j = 0;
while (1)
{
while(j<8)
{
if (!flag_a[j] && !flag_b[i + j] && !flag_c[i - j + 7])
{
pos[i] = j;
if (i == 7)
{
print();
}
else
{
Push(&ii, i);
Push(&jj, j);
flag_a[j] = flag_b[i + j] = flag_c[i - j + 7] = 1;
i = i + 1;
j = 0;
continue;
}
}
j++;
}
Pop(&ii, &i);
Pop(&jj, &j);
flag_a[j] = flag_b[i + j] = flag_c[i - j + 7] = 0;
j++;
if (i == 0 && j > 7)
break;
}
}
알고리즘 구상도
'TEST > 자료구조와 함께 배우는 알고리즘 입문(C언어)' 카테고리의 다른 글
<6> Q2. 자료구조와 함께 배우는 알고리즘 입문 (C언어) (0) | 2021.01.20 |
---|---|
<6> Q1. 자료구조와 함께 배우는 알고리즘 입문 (C언어) (0) | 2021.01.20 |
<5> Q8. 자료구조와 함께 배우는 알고리즘 입문 (C언어) (0) | 2021.01.13 |
<5> Q7. 자료구조와 함께 배우는 알고리즘 입문 (C언어) (0) | 2021.01.12 |
<5> Q6. 자료구조와 함께 배우는 알고리즘 입문 (C언어) (0) | 2021.01.12 |