zoj 2778 - Triangular N-Queens Problem
题目:在三角形的棋盘上放n皇后问题。
分析:找规律题目。依照题目的输出,能够看出构造法则;
先填奇数,后填偶数。以下我们仅仅要证明这样的构造的存在性就可以。
解法:先给出集体构造方法,从(1。n-f(n)+1) 開始填充奇数点;
填充全部的(1+2k。n-f(n)+1+k){当中f(n)就是最大填充数。1+2k<=n-f(n)+1+k} 。
之后開始从(2。n-f(n)+1+k+1)開始填充偶数点,因为奇数点仅仅能攻击奇数点。
偶数点仅仅能攻击偶数点,所以仅仅要保证每行一个皇后就能够了。
证明:我们仅仅须要证明从第n-f(n)+1行開始。每行都能够放一个皇后就能够了;
首先。依照上面的构造可知,如此构造。皇后是不能够互相攻击的。
然后,因为第i行有i个元素。所以有 1+2k<=n-f(n)+1+k。
解得。k <= n-f(n)>= f(n)/2,因此至少有一半是奇数点;
偶数点仅仅要插入到奇数点之间就能够构造了。构造成功。
说明:(2011-09-19 01:28)。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
bool M[ 1001 ][ 1001 ];
int F[ 1005 ];
int A[ 668 ];
int B[ 668 ]; int main()
{
/* 递推公式
memset( F, 0, sizeof( F ) );
F[ 0 ] = 0;F[ 1 ] = 1;F[ 2 ] = 1;
for ( int i = 3 ; i <= 1000 ; ++ i )
F[ i ] = F[ i-3 ] + 2;
*/
for ( int i = 1 ; i <= 1000 ; ++ i )
F[ i ] = (2*i+1)/3;
int c,n;
while ( scanf("%d",&c) != EOF )
for ( int t = 1 ; t <= c ; ++ t ) {
memset( M, 0, sizeof( M ) ); scanf("%d",&n);
printf("%d %d %d\n",t,n,F[ n ]); int y = n-F[ n ]+1;
int x = 1;
for ( int i = 0 ; i < F[ n ] ; ++ i ) {
A[ i ] = y;B[ i ] = x;
M[ y ][ x ] = 1;
y += 1;x += 2;
if ( x > y ) x = 2;
}
/* 画图部分
for ( int p = 1 ; p <= n ; ++ p ) {
for ( int q = 0 ; q < n-p ; ++ q )
printf(" ");
for ( int q = 1 ; q <= p ; ++ q )
if ( M[ p ][ q ] )
printf("* ");
else
printf("@ ");
printf("\n");
}
*/
printf("[%d,%d]",A[ 0 ],B[ 0 ]);
for ( int i = 1 ; i < F[ n ] ; ++ i ) {
if ( i%8 == 0 ) printf("\n");
else printf(" ");
printf("[%d,%d]",A[ i ],B[ i ]);
}
printf("\n\n");
}
return 0;
}
zoj 2778 - Triangular N-Queens Problem的更多相关文章
- 【算法】N Queens Problem
/* ** 目前最快的N皇后递归解决方法 ** N Queens Problem ** 试探-回溯算法,递归实现 */ #include "stdafx.h" #include & ...
- ZOJ 3686 A Simple Tree Problem
A Simple Tree Problem Time Limit: 3 Seconds Memory Limit: 65536 KB Given a rooted tree, each no ...
- zoj 2818 Root of the Problem
Root of the Problem Time Limit: 2 Seconds Memory Limit: 65536 KB Given positive integers B and ...
- zoj 3686 A Simple Tree Problem (线段树)
Solution: 根据树的遍历道的时间给树的节点编号,记录下进入节点和退出节点的时间.这个时间区间覆盖了这个节点的所有子树,可以当做连续的区间利用线段树进行操作. /* 线段树 */ #pragma ...
- zoj 2818 Root of the Problem(数学思维题)
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2818 题目描述: Given positive integer ...
- ZOJ 3686 A Simple Tree Problem(线段树)
Description Given a rooted tree, each node has a boolean (0 or 1) labeled on it. Initially, all the ...
- Jeff Somers's N Queens Solutions 最快的n皇后算法
/* Jeff Somers * * Copyright (c) 2002 * * jsomers@alumni.williams.edu * or * allagash98@yahoo.com * ...
- Pat1128:N Queens Puzzle
1128. N Queens Puzzle (20) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The & ...
- PAT 1128 N Queens Puzzle
1128 N Queens Puzzle (20 分) The "eight queens puzzle" is the problem of placing eight ch ...
随机推荐
- zzulioj--1828-- 贪心的小猫咪(贪心模拟)
1828: 贪心的小猫咪 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 14 Solved: 4 SubmitStatusWeb Board Des ...
- HDU4825:Xor Sum 解题报告(0/1 Trie树)
Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数. 随后 Prometheus 将向 Ze ...
- spring boot自动配置之jdbc
1.DataSource配置 1.1 默认配置application.xml spring.datasource.url=jdbc:mysql://localhost/test spring.data ...
- SecondaryNameNode合并元信息过程
- php 扩展模块安装过程
安装php模块,不需要重新编译php,可以动态扩展php模块. 以安装msgpack模块为例:(此处php5.6是编译安装的) 1.下载msgpack模块源码包 # wget http://pecl. ...
- [SCOI2008]着色方案 递推 记忆化搜索
我们发现 $c_{i}$ 和 $k$ 的规模非常小我们还发现每种颜色的位置是不必知道的,只要这种颜色和相邻的颜色种类不同即可.定义状态 $f[a][b][c][d][e][last]$,代表有 $a$ ...
- 删除小脚本 srm
提示:只能删除当前路径下的目录或文件 #!/bin/bash #将测试好的脚本,拷贝到 $PATH 能够搜索到目录下.并且改名 例如: /usr/local/bin cp /test/srm.sh / ...
- minixml的安装教程
最近想要实现对xml的解析,上网看到有很多库可以直接调用,例如minixml,tinyxml等,我选择了minixml(没有原因,纯属是因为这个找到了中文文档- -) 附上中文文档链接:https:/ ...
- 国庆 day 6 下午
1.数组异或 (xorarray.pas/c/cpp) (xorarray.in/out) 时间限制:2s/空间限制:256M [题目描述] xor——异或,和 and 与or 一样,是一种重要的逻辑 ...
- 9 hbase源码系列(九)StoreFile存储格式
hbase源码系列(九)StoreFile存储格式 从这一章开始要讲Region Server这块的了,但是在讲Region Server这块之前得讲一下StoreFile,否则后面的不好讲下去 ...