八皇后C++

// Note:Your choice is C++ IDE

//201108201730

#include <iostream>
using namespace std;

int num[9][9];
int row[9][9];

void search(int);
int check(int,int);
int solution(int);


int main()
{

    search(1);

    return 0;
}

void search(int i)
{
	static int ans=1;
	int result = solution(i);

	if(i==9)
	{

		for(int cal2=1; cal2<9; cal2++)
			for(int cal1=1; cal1<9; cal1++)
				if(num[cal2][cal1]==1)
					cout<<cal2<<","<<cal1<<" ";
		cout<<"answer:"<<ans++<<endl;
		
	}
	

	for(int a=1; a<result; a++)
	{

		for(int b=1; b<9; b++)
		{
			if(b == row[i][a])
			{
				num[i][b] = 1;
			}
		}

		search(i+1);

		for(int c=1; c<9; c++)
		{
			num[i][c] = 0;
		}
	}
			
}

int solution(int i)
{
	

	int count = 1;
	for(int a=1; a<9; a++)
	{
		if(check(i,a))
		{
			row[i][count] = a;
			count++;
		}
	}
	
	
	return count;

	
}


int check(int i, int j)
{
	int abc = 1;
	for(int a=1; a<9; a++)
		if(num[i][a] == 1)
			abc = 0;

	for(int b=1; b<9; b++)
	{
		if(num[b][j] == 1)
			abc = 0;
	}

	for(int c=i,d=j; c<9 && d<9; c++,d++)
	{
		if(num[c][d] == 1)
			abc = 0;
	}
	for(int e=i,f=j; e>0 && f>0; e--,f--)
		if(num[e][f] == 1)
			abc = 0;
	for(int g=i,h=j; g<9 && h>0; g++,h--)
		if(num[g][h] == 1)
			abc = 0;
	for(int k=i,l=j; k>0 && l<9; k--,l++)
		if(num[k][l] == 1)
			abc = 0;

	return abc;
}
	


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值