题目:在三角形的棋盘上放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. 16.允许重复的multimap

    #include <iostream> #include <map> #include <cstring> using namespace std; void ma ...

  2. <Three.js>(第二节)添加长方体

    一.实验内容 上一节已经搭好了实验的框架.这一节我们将在屏幕上显示一些几何图形.如下图所示,我们将在屏幕上显示一个正方体. 二.实验步骤 1.创建场景 正像上一节所说,首先我们需要建一个场景,场景就是 ...

  3. @synchronized 再考察

    核心是:将标示对象与锁建立关联. 线程 锁 标识: 异常: NSString *test = @"test"; @try {     // Allocates a lock for ...

  4. hdu 5412 CRB and Queries(整体二分)

    题意 动态区间第k大 (n<=100000,m<=100000) 题解 整体二分的应用. 与静态相比差别不是很大.(和CDQ还有点像)所以直接上代码. #include<iostre ...

  5. Kubernetes本地私有仓库配置

    实验环境 master 10.6.191.181 node1 10.6.191.182 node2 10.6.191.183 本地私有仓库 10.6.191.184 一.安装本地私有仓库 1.安装do ...

  6. ECNUOJ 2855 贪吃蛇

    贪吃蛇 Time Limit:1000MS Memory Limit:65536KBTotal Submit:480 Accepted:109 Description  相信很多人都玩过这个游戏,当然 ...

  7. 解决Python 插查 MySQL 时中文乱码问题

    首先找到这里的解决方法, count = cursor.fetchall() for i in count: idc_a = i[0] if isinstance(idc_a, unicode): i ...

  8. Project Euler:Problem 58 Spiral primes

    Starting with 1 and spiralling anticlockwise in the following way, a square spiral with side length ...

  9. hdu5336XYZ and Drops

    题意:给出r*c的网格,有的网格为空.有的有水.再给出一个爆炸点,从这个点向四周爆出四个水滴,若碰到水则融为一体,若碰到其它水滴直接跑过去互不影响.每秒可跑一格,若水中水滴数量超过4则爆开.问T秒后网 ...

  10. UVa 112 树求和

    题意:给定一个数字,以及一个描写叙述树的字符序列,问存不存在一条从根到某叶子结点的路径使得其和等于那个数. 难点在于怎样处理字符序列.由于字符间可能有空格.换行等. 思路:本来想着用scanf的(后发 ...