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 queen 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
跳棋的挑战
译 by Jeru
检查一个如下的6 x 6的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行,每列,每条对角线(包括两条主对角线的所有对角线)上都至多有一个棋子。
1 2 3 4 5 6
-------------------------
1 | | O | | | | |
-------------------------
2 | | | | O | | |
-------------------------
3 | | | | | | O |
-------------------------
4 | O | | | | | |
-------------------------
5 | | | O | | | |
-------------------------
6 | | | | | O | |
-------------------------
行号 1 2 3 4 5 6
列号 2 4 6 1 3 5
这只是跳棋放置的一个解。请遍一个程序找出所有跳棋放置的解。并把它们以上面的序列方法输出。解按字典顺序排列。请输出前3个解。最后一行是解的总个数。
PROGRAM NAME: checker
INPUT FORMAT
一个数字N (6 <= N <= 13) 表示棋盘是N x N大小的。SAMPLE INPUT(checker.in)
6OUTPUT FORMAT
前三行为前三个解,每个解的两个数字之间用一个空格隔开。第四行只有一个数字,表示解的总数。SAMPLE OUTPUT(checker.out)
2 4 6 1 3 5
3 6 2 5 1 4
4 1 5 2 6 3
4
一开始用DFS很明显会TLE...状态数太多了
于是考虑优化
标准的8皇后的优化,开个Y[k]表示k列是否有棋子了;
优化的结果,依然TLE...FT!
继续优化:
考虑到斜边上的坐标和
(/类型的斜变)是定值;
(/类型的)行,列之差是定值(有可能<0..);
优化完居然还是TLE...
无语继续优化,搜第一行的一半,利用对称性质,注意n是奇数的时候需要对第1行的中间列单独考虑.
最后面前过了....0.6xs..这题目很不错,虽然对我这新人来说BT了点...(-__-被虐啊)..
USACO的C1已经过了..(我菜-_-!)
考试咯,考试完慢慢刷,争取寒假通关..
- /*
- ID: chenh193
- PROG: checker
- LANG: C++
- */
- #include<iostream>
- #include<algorithm>
- #include<time.h>
- using namespace std;
- int n,ans[14],tot=0;
- bool xx[28],yy[28],Y[14],quit;
- void dfs(int x,int id)
- {
- int i,j,tmp;
- if(quit)return;
- if(x==n+1)
- {
- tot++;
- if(tot<=3)
- for(i=0;i<id;i++)
- printf("%d%c",ans[i],i==id-1?'/n':' ');
- else quit=true;
- return;
- }
- for(i=1;i<=n;i++)
- {
- if(Y[i]||yy[x+i]||xx[tmp=x-i+14])continue;
- Y[i]=true;
- xx[tmp]=true;
- yy[x+i]=true;
- ans[id]=i;
- dfs(x+1,id+1);
- Y[i]=false;
- xx[tmp]=false;
- yy[x+i]=false;
- }
- }
- void Dfs(int x)
- {
- int i,j,tmp;
- if(x==n+1)
- {
- tot++;
- return;
- }
- for(i=1;i<=n;i++)
- {
- if(Y[i]||yy[x+i]||xx[tmp=x-i+14])continue;
- Y[i]=true;
- xx[tmp]=true;
- yy[x+i]=true;
- Dfs(x+1);
- Y[i]=false;
- xx[tmp]=false;
- yy[x+i]=false;
- }
- }
- int main()
- {
- //freopen("checker.out","w",stdout);
- //freopen("checker.in","r",stdin);
- int i;
- scanf("%d",&n);
- dfs(1,0);
- tot=0;
- memset(xx,false,sizeof(xx));
- memset(yy,false,sizeof(yy));
- memset(Y,false,sizeof(Y));
- for(i=1;i<=n/2;i++)
- {
- Y[i]=true;
- xx[1-i+14]=true;
- yy[1+i]=true;
- Dfs(2);
- Y[i]=false;
- xx[1-i+14]=false;
- yy[1+i]=false;
- }
- tot*=2;
- if(n&0x1)
- {
- Y[n/2+1]=true;
- xx[1-(n/2+1)+14]=true;
- yy[1+n/2+1]=true;
- Dfs(2);
- }
- printf("%d/n",tot);
- return 0;
- }