惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cisco Talos Blog
Cisco Talos Blog
Cyberwarzone
Cyberwarzone
T
Tenable Blog
Security Latest
Security Latest
NISL@THU
NISL@THU
V
Vulnerabilities – Threatpost
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
W
WeLiveSecurity
罗磊的独立博客
Stack Overflow Blog
Stack Overflow Blog
云风的 BLOG
云风的 BLOG
Martin Fowler
Martin Fowler
Engineering at Meta
Engineering at Meta
T
Tor Project blog
H
Heimdal Security Blog
Microsoft Security Blog
Microsoft Security Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
雷峰网
雷峰网
L
LINUX DO - 热门话题
The GitHub Blog
The GitHub Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Recorded Future
Recorded Future
Hugging Face - Blog
Hugging Face - Blog
P
Privacy & Cybersecurity Law Blog
F
Full Disclosure
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
PCI Perspectives
PCI Perspectives
MyScale Blog
MyScale Blog
B
Blog RSS Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
K
Kaspersky official blog
Attack and Defense Labs
Attack and Defense Labs
H
Hackread – Cybersecurity News, Data Breaches, AI and More
有赞技术团队
有赞技术团队
Know Your Adversary
Know Your Adversary
Hacker News - Newest:
Hacker News - Newest: "LLM"
Scott Helme
Scott Helme
The Last Watchdog
The Last Watchdog
博客园 - 【当耐特】
S
Security Affairs
The Cloudflare Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
人人都是产品经理
人人都是产品经理
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
N
News and Events Feed by Topic
AI
AI
H
Help Net Security
美团技术团队
T
Threatpost
Project Zero
Project Zero

博客园 - zealsoft

洛谷 P1816 忠诚题解 洛谷 P2384 最短路题解 洛谷 P2725 邮票题解 洛谷 P2722 总分题解 洛谷 P2921 在农场万圣节Trick or Treat on the Farm题解 洛谷 P1162 填涂颜色题解 洛谷 P1330 封锁阳光大学题解 洛谷 P1032 字串变换题解 洛谷 P1443 马的遍历题解 洛谷 P1280 尼克的任务题解 洛谷 P1020导弹拦截题解 洛谷 P1141 01迷宫题解 VisualSVNServer无法卸载也无法安装,报告不是有效的MOF文件(0x8004401e)错误 视频捕捉的格式问题 一个VxWorks源代码网站 找个轻量级的Log库还挺难 TAU G2中的BitString和OctetString W32.Downadup.autorun病毒的清除 如何用Visual Studio 2005编译Wireshark的插件
洛谷 P1219 八皇后题解
zealsoft · 2019-08-26 · via 博客园 - zealsoft

题目描述

检查一个如下的6 x 6的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行、每列有且只有一个,每条对角线(包括两条主对角线的所有平行线)上至多有一个棋子。

上面的布局可以用序列2 4 6 1 3 5来描述,第i个数字表示在第i行的相应位置有一个棋子,如下:

行号 1 2 3 4 5 6

列号 2 4 6 1 3 5

这只是跳棋放置的一个解。请编一个程序找出所有跳棋放置的解。并把它们以上面的序列方法输出。解按字典顺序排列。请输出前3个解。最后一行是解的总个数。

//以下的话来自usaco官方,不代表洛谷观点

特别注意: 对于更大的N(棋盘大小N x N)你的程序应当改进得更有效。不要事先计算出所有解然后只输出(或是找到一个关于它的公式),这是作弊。如果你坚持作弊,那么你登陆USACO Training的帐号删除并且不能参加USACO的任何竞赛。我警告过你了!

输入格式

一个数字N (6 <= N <= 13) 表示棋盘是N x N大小的。

输出格式

前三行为前三个解,每个解的两个数字之间用一个空格隔开。第四行只有一个数字,表示解的总数。

输入输出样例

输出 #1

2 4 6 1 3 5
3 6 2 5 1 4
4 1 5 2 6 3
4

说明/提示

题目翻译来自NOCOW。

USACO Training Section 1.5


题解

此题是标准的DFS题目。有一个非常朴素的想法,就是用一个二维数组vis表示棋子放置后受到影响的格子。每放置一个棋子侯将所有受到影响的格子+1,DFS结束后将这些格子-1。

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <string.h>

using namespace std;

const int    MAXN = 1005;
int        n, s = 0, cnt = 0, cnt2 = 0;
int        vis[MAXN][MAXN], ans[MAXN][MAXN], map[MAXN][MAXN];

void dfs( int x )
{
    if ( x > n )
    {
        s++;
        cnt++;
        if ( cnt <= 3 )
        {
            cnt2 = 0;
            for ( int i = 1; i <= n; i++ )
            {
                for ( int j = 1; j <= n; j++ )
                {
                    if ( map[i][j] == 1 )
                    {
                        cnt2++;
                        ans[cnt][cnt2] = j;
                    }
                }
            }
        }
        return;
    }
    for ( int i = 1; i <= n; i++ )
    {
        if ( vis[x][i] == 0 )
        {
        //    cout << x << ", " << i << endl;
            vis[x][i]++;
            map[x][i] = 1;
            for ( int j = 1; j <= n; j++ )
            {
                vis[x][j]++;
                if ( j >= x )
                {
                    vis[j][i]++;
                }
                if ( x + j <= n && i >= j )
                {
                    vis[x + j][i - j]++;
                }
                if ( x + j <= n && i + j <= n )
                {
                    vis[x + j][i + j]++;
                }
            }
            dfs( x + 1 );
            vis[x][i]--;
            map[x][i] = 0;
            for ( int j = 1; j <= n; j++ )
            {
                vis[x][j]--;
                if ( j >= x )
                {
                    vis[j][i]--;
                }
                if ( x + j <= n && i >= j )
                {
                    vis[x + j][i - j]--;
                }
                if ( x + j <= n && i + j <= n )
                {
                    vis[x + j][i + j]--;
                }
            }
        }
    }
}


int main()
{
    cin >> n;
    dfs( 1 );
    for ( int i = 1; i <= 3; i++ )
    {
        for ( int j = 1; j <= n; j++ )
        {
            cout << ans[i][j] << " ";
        }
        cout << endl;
    }
    cout << cnt << endl;

    return(0);
}

本来以为这个代码会TLE,但是很幸运的是代码AC了。最后一个测试点用了800+ms。

这个代码是可以被优化的,可以用3个一维数组代替二维数组。一个一维数组代表所有列,只要有一个棋子布在某列,则这个数组列对应的元素就置1。类似的2个一维数组代表和2条对角线平行的线。