【usaco】 checker

本题的要点在于位运算

!6>!n>!n>!if>!hint:>

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

HINTS (use them carefully!)

我的解法:

/*
ID: yunleis2
PROG: checker
LANG: C++
*/

#include<iostream>
#include<fstream>
#include<vector>
#include<stack>
using namespace std;
 

typedef unsigned int uint;
uint limit;
int * result;
int N;
int total=0;
void search(uint row,uint ld,uint rd,int depth);
fstream fout("checker.out",ios::out);
int main()
{
 
 fstream fin("checker.in",ios::in);

 fin>>N;
 limit=(1<<N) -1;
 result=new int[N];
 search(0,0,0,N);
 delete []result;
 fout<<total<<endl;
 return 0;
}
void search(uint row,uint ld,uint rd,int depth)
{
 int i=0;
 if(depth==0||row==limit)
 {
  total++;
  if(total>3)
   return;
  for(i=0;i<N;i++)
  {
   fout<<result[i]+1;
   if(i<(N-1))
    fout<<" ";
  }
  fout<<endl;
  return;
 }
 uint unused=~(row|rd|ld);
 uint tmp=1;
 for(i=0;i<N;tmp=tmp<<1,i++)
 {
  if((unused&tmp)!=0)
  {
   result[N-depth]=i;
   search(row|tmp,(ld|tmp)<<1,(rd|tmp)>>1,depth-1);
  }
 }


}

 

测试:

USER: Ma yunlei [yunleis2]
TASK: checker
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 3028 KB]
   Test 2: TEST OK [0.000 secs, 3028 KB]
   Test 3: TEST OK [0.000 secs, 3028 KB]
   Test 4: TEST OK [0.000 secs, 3028 KB]
   Test 5: TEST OK [0.000 secs, 3028 KB]
   Test 6: TEST OK [0.000 secs, 3028 KB]
   Test 7: TEST OK [0.054 secs, 3028 KB]
   Test 8: TEST OK [0.297 secs, 3028 KB]

All tests OK.
YOUR PROGRAM ('checker') WORKED FIRST TIME!  That's fantastic
-- and a rare thing.  Please accept these special automated
congratulations.

 Here are the test data inputs:
------- test 1 ----
6
------- test 2 ----
7
------- test 3 ----
8
------- test 4 ----
9
------- test 5 ----
10
------- test 6 ----
11
------- test 7 ----
12
------- test 8 ----
13
 Keep up the good work!



Thanks for your submission!

 

 

 

 

参考:http://blog.csdn.net/cmykrgb123/article/details/1854976