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 ...
随机推荐
- MySql-Error: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
MySql-Error: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) 标签( ...
- vue computed自动计算
<!DOCTYPE html> <html> <head> <title>vue</title> <meta charset=&quo ...
- LVS+keepalived均衡nginx配置
如果单台LVS发生突发情况,例如宕机.发生不可恢复现象,会导致用户无法访问后端所有的应用程序.避免这种问题可以使用HA故障切换,也就是有一台备用的LVS,主LVS 宕机,LVS VIP自动切换到从,可 ...
- 仿分词统计的MapReduce 程序。
HDFS 数据格式 : 举例单条数据:02-26 08:01:56 [qtp512249001-42] INFO async-statistics - class com.spring.aop.S ...
- Android笔记---Intent实现Activity跳转
学了之前的Android控件以及布局,我们就能够做一些UI的设计了,这里我结合之前的知识.以一个小的登录项目来解说下Activity之间跳转. 先看下效果图: 1.登录界面: 2.点击登录按钮跳转到另 ...
- [Python] Indexing An Array With Another Array with numpy
NumPy Reference: Indexing Integer array indexing: Select array elements with another array def index ...
- hdu2795Billboard(线段树,找第一个大于w的点)
Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- rgba
正反两面展示效果 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head& ...
- 逆波兰表达式解数学运算(c#)
逆波兰表达式解数学运算 感谢作者 http://blog.csdn.net/liuyuxusuixiang/article/details/25289715 public class TCalcula ...
- PasswordHelper 对user对象的password进行加密重设
public class PasswordHelper { private RandomNumberGenerator randomNumberGenerator = new SecureRandom ...