USACO1.5 Checker Challenge(类n皇后问题)
2015-08-01 16:22
316 查看
B - B
Time Limit:1000MS Memory Limit:16000KB 64bit IO Format:%lld & %llu
Description
Examine the $6\times 6$ 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.)
The solution shown above is described by the sequence
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.
Input
A single line that contains a single integer $N$ ($6\leq N\leq 13$) that is the dimension of the $N\times N$ checkerboard.
Output
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 Input
6
Sample Output
2 4 6 1 3 5
3 6 2 5 1 4
4 1 5 2 6 3
4
题解:雷同于八皇后问题。。只是增加了输出摆放的前三种和摆放办法
Time Limit:1000MS Memory Limit:16000KB 64bit IO Format:%lld & %llu
Description
Examine the $6\times 6$ 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.)
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.
Input
A single line that contains a single integer $N$ ($6\leq N\leq 13$) that is the dimension of the $N\times N$ checkerboard.
Output
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 Input
6
Sample Output
2 4 6 1 3 5
3 6 2 5 1 4
4 1 5 2 6 3
4
题解:雷同于八皇后问题。。只是增加了输出摆放的前三种和摆放办法
#include<iostream> #include<stdio.h> #include<string.h> using namespace std; int n,num,s[30],vis[30][30];; void search(int cur) { int i; if (cur>n) { num++; if (num<=3) { for (i=1; i<n; i++) printf("%d ",s[i]); printf("%d\n",s ); } return; } for (i=1; i<=n; i++) { if(!vis[0][i]&&!vis[1][cur+i]&&!vis[2][cur-i+n]) { s[cur]=i; vis[0][i]=vis[1][cur+i]=vis[2][cur-i+n]=1; search(cur+1); vis[0][i]=vis[1][cur+i]=vis[2][cur-i+n]=0; } } } int main() { scanf("%d",&n); memset(s,0,sizeof(s)); num=0; search(1); printf("%d\n",num); return 0; }
相关文章推荐
- mac下终端命令汇总(持续更新中)
- c# 判断输入的号码是否是合法的手机号码
- ssh三大框架:SSH三大框架的作用
- uva 11817(球面距离)
- 选择排序
- HDU 5336(2015多校4)-XYZ and Drops(bfs)
- 小贝_redis学习目标
- HDU 2473 Junk-Mail Filter (并查集节点删除)
- Bootstrap (导航、标签、面包屑导航)
- YUV播放器
- 树链剖分
- uva 10763 Foreign Exchange 交换生
- apt-get命令详解
- OC_Block
- POJ 2369 - Permutations 【置换群】
- 第二篇T语言代码编写技巧
- ArrayList练习 将学生对象添加到集合中,并遍历输出
- LeetCode OJ 之 Valid Anagram
- 整数划分
- Jsoncpp使用详解以及链接问题解决