题意:八皇后问题的扩展。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. 嵌入式 Linux线程同步读写锁rwlock示例

    读写锁比mutex有更高的适用性,可以多个线程同时占用读模式的读写锁,但是只能一个线程占用写模式的读写锁.1. 当读写锁是写加锁状态时,在这个锁被解锁之前,所有试图对这个锁加锁的线程都会被阻塞:2. ...

  2. JQuery实现分页程序代码,源码下载

    Web开发,分页在所难免的,微软GridView.AspPager等设置分页数据可以自动分页,但是这里浏览器会闪动,用户体验不是很友好,在此我整理了JQuery实现分页,并且使用 JQuery模板显示 ...

  3. 14、NFC技术:使用Android Beam技术传输文本

    Android Beam的基本理念 Android Beam的基本理念就是两部(只能是两部)NFC设备靠近时(一般是背靠背),通过触摸一部NFC设备的屏幕,将数据推向另外一部NFC设备.在传递数据的过 ...

  4. hdu 1541 Stars(线段树单点更新,区间查询)

    题意:求坐标0到x间的点的个数 思路:线段树,主要是转化,根据题意的输入顺序,保证了等级的升序,可以直接求出和即当前等级的点的个数,然后在把这个点加入即可. 注意:线段树下标从1开始,所以把所有的x加 ...

  5. bzoj1036 树的统计Count

    第一次写链剖,于是挑了个简单的裸题写. 以下几点要注意: 1.链剖中的height是从根到该店经过的轻边个数 2.分清num与sum..... #include<cstdio> #incl ...

  6. Ubuntu14.04下安装QQ 国际版

    在/etc/apt/source.list文件中添加: deb http://packages.linuxdeepin.com/deepin trusty main non-free universe ...

  7. es6转码器-babel

    babel 基本使用 安装转码规则 # ES2015转码规则 $ npm install --save-dev babel-preset-es2015 # react转码规则 $ npm instal ...

  8. Cocos2d-JS v3.0 alpha

    Cocos2d-JS是整合了Cocos2d-html5 v3.0 alpha和Cocos2d-x JSBinding的新JS引擎仓库.整合之后的核心优势在于Html5和JSB的开发流程及API现在变得 ...

  9. 企业部署Linux应用将拥有更低的整体拥有成本

    企业部署Linux应用将拥有更低的整体拥有成本     使用Linux能为企业的IT解决方案降低TCO(整体拥有成本Total Cost of The Ownership)吗?在面临这个问题时,很多企 ...

  10. 第三百二十二天 how can I 坚持

    昨晚好像一直在做梦,模模糊糊的,其实很难受. 真不知道该怎么办了,是没有勇气,还是怕什么,总之, 不知道该咋办了. 搞不懂. 回来第二天了,节奏又一点的开始了,一年又会很快过去. 刘松说要来北京,整天 ...