Checker Challenge
来源:互联网 发布:若风淘宝 编辑:程序博客网 时间:2024/06/05 05:07
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:
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 53 6 2 5 1 44 1 5 2 6 34
/*ID: des_jas1PROG: checkerLANG: C++*/#include <iostream>#include <fstream>#include <string>#include <string.h>#include <cmath>#include <algorithm>//#define fin cin//#define fout coutusing namespace std;int N,cn=0,cur[14],pr=0;bool col[7<<1]={0},dial[13<<1+2]={0},diar[13<<1+2]={0};//<<乘2,>>除以2ofstream fout("checker.out");ifstream fin("checker.in");void dfs(int r,int half){if(r>N) { cn++; if(N>6 && cur[1]<((N>>1)+1)) // >>优先权小于 + 噢,对称性cn++; if(pr<3)//升序{ fout<<cur[1];int j; for(j=2;j<=N;j++) fout<<" "<<cur[j];fout<<endl;pr++;} return; }int j;for(j=1;j<=half;j++)//half取等,row=1考虑对称性{if(!col[j] && !dial[r+j] && !diar[r-j+N]){ col[j]=true; dial[r+j]=true; diar[r-j+N]=true; //对角线有 r1-j1+N=r2-j2+N(+N保证下标>0) 和 r1+j1=r2+j2 cur[r]=j; dfs(r+1,N); col[j]=false; dial[r+j]=false; diar[r-j+N]=false;}}}int main() {fin>>N;dfs(1,N>6?(N+1)>>1:N); //row=1的时候除了N=6的情况其余只要取一半,另一半对称;N=6的一半为3,如果考虑对称就没法输出第三种情况;N必须+1fout<<cn<<endl;fout.close();fin.close(); return 0;}
- Checker Challenge
- Checker Challenge
- Checker Challenge
- Checker Challenge
- Checker Challenge
- Checker Challenge
- USACO 1.5 Checker Challenge (checker)
- USACO6.5.5 Checker Challenge( checker)
- 【搜索】【USACO】Checker Challenge
- Section 1.5 Checker Challenge
- USACO1.5.4 checker challenge
- 1.5Checker Challenge
- USACO1.5.4 checker challenge
- [usaco]1.5 Checker Challenge
- usaco Checker Challenge
- USACO:Checker Challenge
- 1.5.4---Checker Challenge
- USACO-Checker Challenge
- hdu1358
- 数据库访问的性能问题与瓶颈问题
- RUP与敏捷与周末创业
- 序列最小优化算法(SMO)
- 栈的简单应用——四则运算(三)
- Checker Challenge
- 基于avd7181c解决视频输入效果差的问题<一>---驱动移植、调试手记
- poj 1005 I Think I Need a Houseboat(简单计算题)
- android_分享 图片 和 文字
- Working with user roles and permissions in SharePoint Object Model
- 关于学习新技术的方法和让自己不断进步的一点建议
- 在HBase上查询地理信息系统(HBase in Action)
- Json数据异步绑定到界面的Table并且自动刷新
- Android--查找程序根目录下所有文件/Java IO操作