八皇后变形之四皇后问题

#include<stdio.h>
int main()
{
//	char *s = "abc";
//	printf("%s",s);
	int r,c,r1,c1;    //因为四行四列,所以我们应该确定1皇后和2皇后的位置,r行,c列。
	char a[100];
	for(int i=0;i<16;i++)
		for(int j=0;j<16;j++)   //注意嵌套循环
			{
				//a[j]=0;
				// set all a to '0'
				for(int k=0;k<16;k++)
					a[k]='0';    //先让所有的16位字符全为0
				if(i!=j)
					{
					a[i]='a';
					a[j]='b';                  //    列:  1 2 3 4
					r=i/4,c=i%4,r1=j/4;c1=j%4;          0 1 2 3  最后一行懒得写了  
                                                                               4 5 6 7  会发现每一列都是4的倍数
					                                       8 9 10 11//
					// in the same row then continue;
					if(r==r1){
						continue;
					}
					// in the same column continue;
					if(c==c1){
						continue;
					}
					// -1
					if((c-c1)/(r-r1)==-1){
						continue;
					}
					// 1 
					if((c-c1)/(r-r1)==1){
						continue;
					}
					// print a 0 to 15
					for(int j=0;j<16;j++)
					{
					printf("%c", a[j]);
					if((j+1)%4==0)
					printf("\n");}
					printf("---------------\n"); 
					
					
//					for(int i=0;i<16;i++)
//					printf("%c", a[i]);
				}
			}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值