题目:在三角形的棋盘上放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的更多相关文章

  1. 【算法】N Queens Problem

    /* ** 目前最快的N皇后递归解决方法 ** N Queens Problem ** 试探-回溯算法,递归实现 */ #include "stdafx.h" #include & ...

  2. ZOJ 3686 A Simple Tree Problem

    A Simple Tree Problem Time Limit: 3 Seconds      Memory Limit: 65536 KB Given a rooted tree, each no ...

  3. zoj 2818 Root of the Problem

    Root of the Problem Time Limit: 2 Seconds      Memory Limit: 65536 KB Given positive integers B and ...

  4. zoj 3686 A Simple Tree Problem (线段树)

    Solution: 根据树的遍历道的时间给树的节点编号,记录下进入节点和退出节点的时间.这个时间区间覆盖了这个节点的所有子树,可以当做连续的区间利用线段树进行操作. /* 线段树 */ #pragma ...

  5. zoj 2818 Root of the Problem(数学思维题)

    题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2818 题目描述: Given positive integer ...

  6. 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 ...

  7. Jeff Somers's N Queens Solutions 最快的n皇后算法

    /* Jeff Somers * * Copyright (c) 2002 * * jsomers@alumni.williams.edu * or * allagash98@yahoo.com * ...

  8. Pat1128:N Queens Puzzle

    1128. N Queens Puzzle (20) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The & ...

  9. PAT 1128 N Queens Puzzle

    1128 N Queens Puzzle (20 分)   The "eight queens puzzle" is the problem of placing eight ch ...

随机推荐

  1. if语句练习

    输入年月日,首先判断该年是平年闰年并且计算该天是该年的第几天: 判断男女体重是否标准: 体重判断里边出现一个问题:如果性别输入的不是男也不是女,那么会执行输出“请输入正确的性别”:然后底下会继续输出“ ...

  2. 51Nod 不重叠的线段(贪心)

    X轴上有N条线段,每条线段有1个起点S和终点E.最多能够选出多少条互不重叠的线段.(注:起点或终点重叠,不算重叠). 例如:[1 5][2 3][3 6],可以选[2 3][3 6],这2条线段互不重 ...

  3. 解决高版本vm打开虚拟机报错

    问题: 打开虚拟机的文件目录,找到.vmx 文件 用记事本打开重命名后的“.vmx.txt”文件 找到行:policy.vm.mvmtid = "52 10 08 ed ff 34 ed d ...

  4. CF85E Guard Towers(二分答案+二分图)

    题意 已知 N 座塔的坐标,N≤5000 把它们分成两组,使得同组内的两座塔的曼哈顿距离最大值最小 在此前提下求出有多少种分组方案 mod 109+7 题解 二分答案 mid 曼哈顿距离 >mi ...

  5. Xshell6连接Ubuntu18.04

    1.首先在自己windows10电脑上安装了xshell6,安装过程不叙述了 2.打开xshell 3.执行新建命令.打开Xshell软件后找到左上角第一个“文件”菜单并单击,弹出来一个下拉框,点击选 ...

  6. cygwin下调用make出现的奇怪现象

    <lenovo@root 11:48:03> /cygdrive/d/liuhang/GitHub/rpi_linux/linux$make help 1 [main] make 4472 ...

  7. Vue2.0组件实现动态搜索引擎(一)

    原文链接:https://blog.csdn.net/qwezxc24680/article/details/74550556 从github上看到一个不错的开源项目:https://github.c ...

  8. BNUOJ 4049 四叉树

    四叉树 Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class name: Ma ...

  9. centos7 安装rsyslog

    http://blog.csdn.net/u011630575/article/details/50896781 http://blog.chinaunix.net/uid-21142030-id-5 ...

  10. 【转】CentOS下firefox安装flash说明

    http://www.cnblogs.com/lamper/archive/2013/01/16/2862254.htm CentOS下自带了firefox,但没有flash插件的,按它自己的提示安装 ...