现在的位置: 首页 > 综合 > 正文

USACO SECTION 1.5 Checker Challenge

2017年10月17日 ⁄ 综合 ⁄ 共 3331字 ⁄ 字号 评论关闭
文章目录

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

 

   Test 1: TEST OK [0.000 secs, 2124 KB]
   Test 2: TEST OK [0.000 secs, 2124 KB]
   Test 3: TEST OK [0.000 secs, 2124 KB]
   Test 4: TEST OK [0.000 secs, 2124 KB]
   Test 5: TEST OK [0.000 secs, 2124 KB]
   Test 6: TEST OK [0.011 secs, 2124 KB]
   Test 7: TEST OK [0.043 secs, 2124 KB]
   Test 8: TEST OK [0.140 secs, 2124 KB]

 

/*
ID: conicoc1
LANG: C
TASK: checker
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

FILE *fin,*fout;
int upperlim;
int count=0;
int A[14];
int N;
int count2=0;

void test(int row,int ld,int rd)
{
	int pos,p;
	if(row!=upperlim)
	{
		pos=upperlim & ~(row|ld|rd);
		while(pos!=0)
		{
			p=pos& -pos;    //-pos=^pos+1
			pos=pos-p;
			test(row+p,(ld+p)<<1,(rd+p)>>1);
		}
	}
	else
		count++;
}

int judge(int i)
{
	int j;
	for(j=1;j<i;j++)
	{
		if(A[j]==A[i]||i-j==A[i]-A[j]||i-j==A[j]-A[i])
			return 0;
	}
	return 1;
}
void queen()
{
	int i=1,j;
	
	while(i>=1&&count2<3)
	{
		A[i]++;
//		printf("%d %d\n",i,A[i]);
//		getchar();
		if(A[i]>N)
		{
			A[i]=0;
			i--;
		}
		else
		if(judge(i))
		{
			i++;
			if(i>N)
			{
				for(j=1;j<=N;j++)
				{
					fprintf(fout,"%d",A[j]);
					if(j!=N)
						fprintf(fout," ");
				}
				fprintf(fout,"\n");
				i--;
				count2++;
			}
		}			
	}	
}

int main()
{	
	fin = fopen("checker.in", "r");
        fout = fopen("checker.out", "w");
   	
 	memset(A,0,sizeof(A));
    
        fscanf(fin,"%d",&N);
    
    
	upperlim=(1<<N)-1;
	test(0,0,0);
	queen();
	fprintf(fout,"%d\n",count);
	
	return 0;	
}

想了半天就是超时啊,感觉对称和旋转不能解决本质问题,又没有好的剪枝办法

好吧 承认自己看了NOCOW上的题解。。

Matrix67不愧为大神。。07年的几篇日志详细介绍了位运算

并专门写一篇讲解用位运算计算N皇后问题的情况数量

为了防止以后忘记

void test(int row,int ld,int rd)  //row代表该行列方向能够摆放的位置,ld表示\方向,rd表示/方向 
{
	int pos,p;                       
	if(row!=upperlim)       //如果皇后没有放满 
	{
		pos=upperlim & ~(row|ld|rd);    //合并三种情况后取反,pos中1代表可以摆放的位置,0代表不能摆放的位置 
		while(pos!=0)                    //如果存在可以摆放的位置 
		{
			p=pos& -pos;    //-pos=^pos+1     //从右往左取pos的第一个1,作为摆放位置 
			pos=pos-p;						//为下一次取一个新的p(新的摆放情况,即向左找下一个1), 
			                               //将现在pos的最后一个1变为0 
			test(row+p,(ld+p)<<1,(rd+p)>>1);    // 递归 
		}
	}
	else
		count++;   //已放满皇后,计数器+1 
}

 忘了一个注释:

upperlim取某行都放满的情况

可以这样:

upperlim=(1<<N)-1;

 

 

抱歉!评论已关闭.