题意:八皇后问题的扩展。8*8棋盘上每个格子都有一个整数,要求8个皇后所在格子的数字之后最大

解法一,回溯:

用vis数组记录 列,主对角(y-x), 副对角(y+x) 访问情况

#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int C[50], vis[3][50], tot = 0, n = 8, nc = 0;
int board[8][8];
int max_score; void search(int cur)
{
nc++;
if(cur==n)
{
tot++;
int score=0;
for(int i=0;i<8;i++)
score+=board[i][C[i]];
max_score=max(max_score, score);
}
else for(int i=0;i<n;i++)
{
if(vis[0][i] || vis[1][i-cur+n] || vis[2][i+cur])
continue;
C[cur]=i;
vis[0][i]=vis[1][i-cur+n]=vis[2][i+cur]=1;
search(cur+1);
vis[0][i]=vis[1][i-cur+n]=vis[2][i+cur]=0;
}
} int main()
{
int k;
scanf("%d", &k);
while(k--)
{
for(int i=0;i<8;i++)
for(int j=0;j<8;j++)
{
scanf("%d", &board[i][j]);
}
memset(vis, 0, sizeof(vis));
max_score=0;
search(0);
printf("%5d\n", max_score);
}
return 0;
}

 

解法二, STL next_permutation 遍历所有列排列

#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int v[8][8], P[8]; bool judge() {
for(int i=0;i<8;i++)
for(int j=0;j<i;j++)
if(j-P[j]==i-P[i] || j+P[j]==i+P[i])
return false;
return true;
}
int main()
{
int T;
scanf("%d", &T);
while(T--) {
for(int i=0;i<8;i++) {
P[i]=i;
for(int j=0;j<8;j++)
scanf("%d", &v[i][j]);
}
int ans=0;
do
{
if(!judge())
continue;
int score=0;
for(int i=0;i<8;i++) score+=v[i][P[i]];
ans=max(ans, score);
}while(next_permutation(P, P+8));
printf("%5d\n", ans);
}
return 0;
}

uva167 - The Sultan's Successors的更多相关文章

  1. uva167 The Sultan's Successors

    The Sultan's Successors Description The Sultan of Nubia has no children, so she has decided that the ...

  2. UVA 167 R-The Sultan's Successors

    https://vjudge.net/contest/68264#problem/R The Sultan of Nubia has no children, so she has decided t ...

  3. The Sultan's Successors UVA - 167

    the squares thus selected sum to a number at least as high as one already chosen by the Sultan. (For ...

  4. Uva 167 The Sultan's Successors(dfs)

    题目链接:Uva 167 思路分析:八皇后问题,采用回溯法解决问题. 代码如下: #include <iostream> #include <string.h> using n ...

  5. 备战NOIP每周写题记录(一)···不间断更新

    ※Recorded By ksq2013 //其实这段时间写的题远远大于这篇博文中的内容,只不过那些数以百记的基础题目实在没必要写在blog上; ※week one 2016.7.18 Monday ...

  6. UVA The Sultan&#39;s Successors

    题目例如以下: The Sultan's Successors  The Sultan of Nubia has no children, so she has decided that thecou ...

  7. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  8. uva 167 - The Sultan&#39;s Successors(典型的八皇后问题)

    这道题是典型的八皇后问题,刘汝佳书上有具体的解说. 代码的实现例如以下: #include <stdio.h> #include <string.h> #include < ...

  9. UVA - 11604 General Sultan 题解

    题目大意: 有若干模式串,将某些模式串拼接起来(一个可以使用多次)形成一个长模式串,判断能否有两种或更多种不同的拼法拼成相同的模式串. 思路: 神奇的构图,暴力的求解. 可以发现,若有不同的拼法,则一 ...

随机推荐

  1. Bootstrap初级用户谈谈网页在手机上的显示效果优化

    本人之前已经使用Bootstrap有一段时间了,但是之前做出的网站都只是在电脑端使用,没有注意过手机端的显示效果.这两天自己使用Bootstrap做了一个简单的Web个人日志系统,想在手机端也使用,桌 ...

  2. 从IT方法论来谈RUP

    在<从IT方法论来谈Scrum>中我谈到了6Ways方法框架,本篇仍用6Ways方法框架来概括的谈谈RUP方法. 软件开发过程描述了软件构造.部署和维护的一种方法.统一过程(Unified ...

  3. Web Developer可以做得更多

    美国雅虎前端工程师Hedger Wang.这位原雅虎奇摩的第一位Web Developer,非常慷慨的与我们分享了他丰富的经验.现身说法,比空洞的理论更有感染力,我们发现现在遇到的很多问题也都是他曾经 ...

  4. oracle sql语句跟踪

    select b.SQL_TEXT,b.FIRST_LOAD_TIME,b.SQL_FULLTEXT    from v$sqlarea b    order by  b.FIRST_LOAD_TIM ...

  5. CURL --- 命令行浏览器CURL

    CURL --- 命令行浏览器CURL   CURL --- 命令行浏览器   CURL? 嗯,说来话长了~~~~ 这东西现在已经是苹果机上内置的命令行工具之一了,可见其魅力之一斑 1)二话不说,先从 ...

  6. top命令 Linux查看CPU和内存使用情况

    一.top命令 top命令是一个功能十分强大的监控系统的工具,对于系统管理员而言尤其重要.但是,它的缺点是会消耗很多系统资源. 在系统维护的过程中,随时可能有需要查看 CPU 使用率,并根据相应信息分 ...

  7. delphi 712 Word 2

    //导出Wordprocedure TFrm_Computing.ExportWord(aFileName: string; aFileType: string);var wordApp, WordD ...

  8. Building nginx from Sources(从源代码安装nginx)

    Building nginx from Sources(从源代码安装nginx) The build is configured using the configure command.  安装用配置 ...

  9. 基于vagrant工具在win7下免密登录linux

    一.SSH加密方式 SSH采用的是"非对称密钥系统",即耳熟能详的公钥私钥加密系统,其安全验证又分为两种级别. 1. 基于口令的安全验证 这种方式使用用户名密码进行联机登录,一般情 ...

  10. MFC学习20160718(GetModuleFileName&amp;&amp;GetAppDataPath)

    1.标题栏设置 一.对话框标题栏内容为静态 直接在对话框属性“General”的“Caption”中修改. 二.对话框标题栏内容为动态生成的 在对应对话框的初始化函数OnInitDialog()中添加 ...