Checker Challenge

Checker Challenge

Examine the 6x6 checkerboard below and note that the six checkers are arranged on the board so that one and only one is placed in each row and each column, and there is never more than one in any diagonal. (Diagonals run from southeast to northwest and southwest to northeast and include all diagonals, not just the major two.)

          Column
    1   2   3   4   5   6
  -------------------------
1 |   | O |   |   |   |   |
  -------------------------
2 |   |   |   | O |   |   |
  -------------------------
3 |   |   |   |   |   | O |
  -------------------------
4 | O |   |   |   |   |   |
  -------------------------
5 |   |   | O |   |   |   |
  -------------------------
6 |   |   |   |   | O |   |
  -------------------------

The solution shown above is described by the sequence 2 4 6 1 3 5, which gives the column positions of the checkers for each row from 1 to 6:

ROW 1 2 3 4 5 6
COLUMN 2 4 6 1 3 5

This is one solution to the checker challenge. Write a program that finds all unique solution sequences to the Checker Challenge (with ever growing values of N). Print the solutions using the column notation described above. Print the the first three solutions in numerical order, as if the checker positions form the digits of a large number, and then a line with the total number of solutions.

Special note: the larger values of N require your program to be especially efficient. Do not precalculate the value and print it (or even find a formula for it); that's cheating. Work on your program until it can solve the problem properly. If you insist on cheating, your login to the USACO training pages will be removed and you will be disqualified from all USACO competitions. YOU HAVE BEEN WARNED.

TIME LIMIT: 1 CPU second

PROGRAM NAME: checker

INPUT FORMAT

A single line that contains a single integer N (6 <= N <= 13) that is the dimension of the N x N checkerboard.

SAMPLE INPUT (file checker.in)

6

OUTPUT FORMAT

The first three lines show the first three solutions found, presented as N numbers with a single space between them. The fourth line shows the total number of solutions found.

SAMPLE OUTPUT (file checker.out)

2 4 6 1 3 5
3 6 2 5 1 4
4 1 5 2 6 3
4
// bitchecker.cpp : 定义控制台应用程序的入口点。
/*
ID:verydar1
LANG:C++
TASK:checker
*/

#include "stdafx.h"
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<assert.h>
using namespace std;

int sum=0;
int upperlim=0;
const int a[]={0x55555555,0x33333333,0x0F0F0F0F,0x00FF00FF,0x0000FFFF}; 
int list[3][13];
int writeinList=0;

inline int number(int row) {

    row=(row&a[0])+((row>>1)&a[0]);
    row=(row&a[1])+((row>>2)&a[1]);
    row=(row&a[2])+((row>>4)&a[2]);
    row=(row&a[3])+((row>>8)&a[3]);
    row=(row&a[4])+((row>>16)&a[4]);
    return row;
}


void test(int row,int ld,int rd)//row表示横行放皇后的情况,ld、rd分别是左右对角线的情况 {
{
    int pos,p;
	int occupied;
    if(row!=upperlim)//upperlim表示全部都放上皇后的那种情况
    {
		 occupied=row|rd|ld; 
         pos=upperlim & ~(row|ld|rd);//pos表示还能在哪些位置上放皇后
         while(pos)
			 //递归执行,在过程中用pos表示该递归子树的剩余可能性,随着pos-=p,pos最后变为0,意味着该子树的结束
			 //每次调用test新增一棵子树
         {
                p=pos & -pos;//p表示最右边的能放的那个位置
                pos-=p;//放上这个位置
                if(sum<3) 
				{
					//int num=number(row),pnt=(int)log2(p)+1;
					int num=number(row),pnt=(int)(log((float)p)/log((float)2))+1;
					for(int i=sum;i<3;i++)
					list[i][num]=pnt;//list便是记录答案的数组,最节约的大小为list[3][13]
				}
                test(row+p,(ld+p)<<1,(rd+p)>>1);//找下一个
         }
    }
    else
         sum++;
}

int _tmain(int argc, _TCHAR* argv[])
{
	freopen("checker.in","r",stdin);
	freopen("checker.out","w",stdout);
	int rows;
	scanf("%d",&rows);
	upperlim=(1<<rows)-1;
	test(0,0,0);
	int i=0,j=0;
	for(i=0;i<3;i++)
	{
	        for(j=0;j<rows-1;j++)
	                printf("%d ",list[i][j]);
            printf("%d\n",list[i][j++]);
       
    }
	printf("%d\n",sum);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值