题目:在三角形的棋盘上放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. Windows10+VS2013+caffe+Python2.7+CUDA8.0 部署配置

    所需环境工具: 1. Windows 10 2. VS2013 3. Windows版本的caffe工具包,地址:https://github.com/Microsoft/caffe 4. Anaco ...

  2. luogu 3393 逃离僵尸岛

    题目描述 小a住的国家被僵尸侵略了!小a打算逃离到该国唯一的国际空港逃出这个国家. 该国有N个城市,城市之间有道路相连.一共有M条双向道路.保证没有自环和重边. K个城市已经被僵尸控制了,如果贸然闯入 ...

  3. BZOJ 3503 高斯消元

    思路: 高斯消元就好啦 注意每个格子最多只能和4个相邻 所以是 n*m*n*m*5 的 并不会TLE //By SiriusRen #include <cstdio> #include & ...

  4. jsp输出当前时间

    在jsp页面中输出完整的时间,格式为"年 月 日  时:分:秒" <% Date date = new Date(); SimpleDateFormat t = new Si ...

  5. Fiddler 故障

    如果只要打开 Fiddler 就没法进网页,重启 Fiddler 问题依旧.卸载并重装 Fiddler 后,提示 8888 端口被占,错误弹窗中包含“ipoverusbsvc:2492”,说明有设备在 ...

  6. 有关 IE11

    IE10/IE11 中 99% 的 CSS3 属性不需要写 -ms- 前缀: IE9/IE10/IE11 默认开启 GPU 加速,效果比同期 Chrome 版本(如 35)流畅: IE11 的字体渲染 ...

  7. UVa 12661 Funny Car Racing【 dijkstra 】

    题意:给出n个点,m条路,每条路用5个整数表示u,v,a,b,t u表示这条路的起点,v表示终点,a表示打开时间,b表示关闭时间,t表示通过这条道路需要的时间 看的紫书,因为边权不再仅仅是路上的时间, ...

  8. B-Boxes

    http://agc010.contest.atcoder.jp/tasks/agc010_b Problem Statement There are N boxes arranged in a ci ...

  9. NOIP 2017 小凯的疑惑(同余类)

    题意 给出两个互质的数a,b问最大的不能被xa+yb(x,y>=0)表示的数.(a,b<=109) 题解 NOIPday1T1一道数论题,不知埋葬了多少人的梦想. 用同余类去解释. 我们依 ...

  10. 使用U盘作为启动盘安装ubuntu系统

     一.使用U盘刻录镜像 1.安装之后我们打开软件,点击文件打开,找到我们刚才进行下载的Ubuntu的ISO文件,然后点击打开,完成ISO文件的加载.接着我们插入U盘,点击UltraISO启动选项,然后 ...