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;
}
时间: 2025-01-13 01:09:18

zoj 2778 - Triangular N-Queens Problem的相关文章

【算法】N Queens Problem

/* ** 目前最快的N皇后递归解决方法 ** N Queens Problem ** 试探-回溯算法,递归实现 */ #include "stdafx.h" #include "iostream" #include <math.h> using namespace std; #include "time.h" // sum用来记录皇后放置成功的不同布局数:upperlim用来标记所有列都已经放置好了皇后. long sum = 0,

poj 3100 &amp;&amp; zoj 2818 ( Root of the Problem ) (睡前一水)

链接:click here 题意: Given positive integers B and N, find an integer A such that AN is as close as possible to B. (The result A is an approximation to the Nth root of B.) Note that AN may be less than, equal to, or greater than B. .给你B和N,求个整数A使得A^n最接近B

ZOJ 3686 A Simple Tree Problem(线段树)

A Simple Tree Problem Time Limit: 3 Seconds      Memory Limit: 65536 KB Given a rooted tree, each node has a boolean (0 or 1) labeled on it. Initially, all the labels are 0. We define this kind of operation: given a subtree, negate all its labels. An

zoj 2818 Root of the Problem

Root of the Problem Time Limit: 2 Seconds      Memory Limit: 65536 KB Given positive integers B and N, find an integer A such that AN is as close as possible to B. (The result A is an approximation to the Nth root of B.) Note that AN may be less than

zoj 3686 A Simple Tree Problem (线段树)

Solution: 根据树的遍历道的时间给树的节点编号,记录下进入节点和退出节点的时间.这个时间区间覆盖了这个节点的所有子树,可以当做连续的区间利用线段树进行操作. /* 线段树 */ #pragma comment(linker, "/STACK:102400000,102400000") #include <iostream> #include <cstdio> #include <cstring> #include <cmath>

[Aizu] ALDS1_13_A: 8 Queens Problem

Solution Analysis 很经典的8皇后问题, 题目中已给出了几个皇后的位置并且不允许更改这几个皇后的位置 要求的输出是8*8的棋盘 可以使用递归的思路来求解 Design 使用了1个int数组来存储每行皇后的位置 使用了3个bool数组来判断当前列, 主对角线, 副对角线是否有冲突 设计求解函数 使用栈来存储中间的数据,(例如正在处理第3行, 然后放置到了第4列, 之后要去第4行放置皇后, 此时需要将第三行的行号, 以及处理到了哪个位置先存储起来) 处理的过程中, 如果遇到了不能更改

惊叹计算机运行速度的提升---以n Queens 问题为例

1 介绍 实现了书<Data Structures and Program design in C++>(Robert L. Kruse and Alexander J. Ryba, 2000)中的188页的基于回溯策略的递归算法solve_from,该算法能够计算n Queens问题的解.选择不同的n作为棋盘大小,能够得出不同棋盘大小的Queens问题的解即执行时间. 该书出版时间为2000年,那么使用的计算机大概为1999年左右的.该书给出了执行的结果数据.我在我的电脑上採用相同的代码和算

Jeff Somers&#39;s N Queens Solutions 最快的n皇后算法

1 /* Jeff Somers 2 * 3 * Copyright (c) 2002 4 * 5 * [email protected] 6 * or 7 * [email protected] 8 * 9 * April, 2002 10 * 11 * Program: nq 12 * 13 * Program to find number of solutions to the N queens problem. 14 * This program assumes a twos compl

PAT A1128 N Queens Puzzle (20 分)

The "eight queens puzzle" is the problem of placing eight chess queens on an 8×8 chessboard so that no two queens threaten each other. Thus, a solution requires that no two queens share the same row, column, or diagonal. The eight queens puzzle