usaco 1.5.4 Checker Challenge
2012-02-28 22:12
309 查看
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. (Diagonalsrun 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 |
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 H***E 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
居然过了。不可思议。
/* ID: wtff0411 PROG: checker LANG: C++ */ #include <iostream> #include <fstream> #include <string> #include <cstring> #include <vector> #include <cmath> #include <queue> #include <algorithm> int n; int res=0; int re[4][14]; //bool box[14][14]; bool dip[40],dim[40],col[14]; void solve(int i) { if(i==n) { res++; if(res<4) for(int kk=0;kk<n;kk++) re[res][kk]=re[res-1][kk];//must update to next answer return; } int j; for(j=0;j<n;j++) { if(dip[i+j]==true) continue; if(dim[i-j+14]==true)//not get abs of them continue; if(col[j]==true) continue; //box[i][j]=true; dip[i+j]=true; dim[i-j+14]=true; col[j]=true; if(res<3) re[res][i]=j; i++; /*for(int kk=0;kk<n;kk++) printf("%d ",re[0][kk]+1); printf("\n"); for(int kk=0;kk<n;kk++) printf("%d ",re[1][kk]+1); printf("\n"); for(int kk=0;kk<n;kk++) printf("%d ",re[2][kk]+1); printf("\n"); system("pause");*/ solve(i); i--; dip[i+j]=false; dim[i-j+14]=false; col[j]=false; } return ; } using namespace std; int main() { freopen("checker.in","r",stdin); freopen("checker.out","w",stdout); cin>>n;//6~13 //memset(box,false,sizeof(box)); memset(dip,false,sizeof(dip)); memset(dim,false,sizeof(dim)); memset(col,false,sizeof(col)); solve(0); for(int i=0;i<3;i++) { int j;//can't int in next loop; for(j=0;j<n-1;j++) { cout<<re[i][j]+1<<" "; } cout<<re[i][j]+1;//the last didn't follow with a " " cout<<endl; } cout<<res<<endl; //system("pause"); return 0; }
昨天可得意了,可惜然后就断电了。
今天。好累。
如果你们都不在意,我在哪里又有什么可惜。
相关文章推荐
- USACO1.5.4 checker challenge
- USACO 1.5.4 Checker Challenge
- USACO 1.5.4 Checker Challenge 题解
- USACO1.5.4 checker challenge
- There are 常数s 和 常数s! Usaco 1.5.4 Checker Challenge
- USACO 1.5.4 Checker Challenge ———— DFS
- [USACO 1.5.4] Checker Challenge
- [USACO 1.5.4]checker(水题重做——位运算(lowbit的应用))
- USACO / Checker Challenge(位运算的搜索)
- USACO1.5 Checker Challenge(类n皇后问题)
- USACO Checker Challenge
- usaco Section 1.5 Checker Challenge 最慢0.162秒0.0+n皇后问题位运算版(C语言)
- usaco 1.5.4 checker
- USACO Checker Challenge 位运算
- usaco 1.5.4——checker
- USACO6.5.5 Checker Challenge( checker)
- USACO 1.5 Checker Challenge (DFS)
- USACO 1.5 Checker Challenge (DFS + 剪枝)
- USACO 1.5 Checker Challenge
- USACO 6.5 Checker Challenge