暴力解N皇后

另一位同学的暴力解
https://blog.csdn.net/weixin_44771588/article/details/102575036

#include<stdio.h>
#include<math.h>
#define n 8
int place(int a[n])
{
	int i, j;
	for(i = 0; i < n; i++)
		for (j = i + 1; j < n; j++)
		{
			if ((a[i] == a[j]) || (abs(a[i] - a[j]) == abs(i - j)))
				return 0;
		}
	return 1;
}
int main()
{
	int a[n];
	int num, temp;
	int i;
	int count = 0;

	for (num = 0; num < pow(n, n); num++)
	{
		temp = num;
		for (i = 0; i < n; i++)
		{
			
			a[i] = temp%n;
			temp = temp / n;
		}
			if (place(a))
			{
				count++;
				for (i = 0; i < n; i++)
				{
					printf("(%d,%d) ", i+1,a[i]+1);
				}
				putchar('\n');
			}
		
	}
	printf("total is : %d\n", count);
	return 0;
}

posted @ 2019-10-15 22:11  AmosAlbert  阅读(61)  评论(0编辑  收藏  举报