~cpp 
#include <iostream>
using namespace std;

const int Max = 8;

int check, cnt;
int ar[Max][Max];

void block(int, int);
void find(int, int);



void main()
{  
	int i, j, k;
	int cnt = 0;

	for (i = 0; i < Max; i++)
	{
		for (j = 0; j < Max; j++)
			for (k = 0; k < Max; k++)
				ar[j][k] = -1;
		check = 0;
		find(0, i);
	}
}  

void block(int row, int col)
{
	int crow, ccol;
	int i;
	for (i = row + 1; i < Max; i++)
		ar[i][col] = 0;
	crow = row;
	ccol = col;
	while (++crow < Max && --ccol >= 0)
		ar[crow][ccol] = 0;
	crow = row;
	ccol = col;
	while (++crow < Max && ++ccol < Max)
		ar[crow][ccol] = 0;
}

void find(int row, int col)
{
	int i, j, k;
	int temp[Max][Max];
	int tcheck;
	
	if (ar[row][col] == 0)
		return;
	else
	{
		ar[row][col] = 1;
		block(row, col);
		check++;
		if (check == Max)
		{
			cout << ++cnt << " : ";
			for (j = 0; j < Max; j++)
				for (k = 0; k < Max; k++)
					if (ar[j][k] == 1)
						cout << k + 1 << " ";
			cout << endl;
			if (cnt % 24 == 0)
				cin.get();
			return;
		}
		for (i = 0; i < Max; i++)
		{
			for (j = 0; j < Max; j++)
				for (k = 0; k < Max; k++)
					temp[j][k] = ar[j][k];
			tcheck = check;
			find(row + 1, i);
			for (j = 0; j < Max; j++)
				for (k = 0; k < Max; k++)
					ar[j][k] = temp[j][k];
			check = tcheck;
		}
	}
}



Retrieved from http://wiki.zeropage.org/wiki.php/8queen/곽세환
last modified 2021-02-07 05:22:19