uva167 - The Sultan's Successors
题意:八皇后问题的扩展。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的更多相关文章
- uva167 The Sultan's Successors
The Sultan's Successors Description The Sultan of Nubia has no children, so she has decided that the ...
- 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 ...
- 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 ...
- Uva 167 The Sultan's Successors(dfs)
题目链接:Uva 167 思路分析:八皇后问题,采用回溯法解决问题. 代码如下: #include <iostream> #include <string.h> using n ...
- 备战NOIP每周写题记录(一)···不间断更新
※Recorded By ksq2013 //其实这段时间写的题远远大于这篇博文中的内容,只不过那些数以百记的基础题目实在没必要写在blog上; ※week one 2016.7.18 Monday ...
- UVA The Sultan's Successors
题目例如以下: The Sultan's Successors The Sultan of Nubia has no children, so she has decided that thecou ...
- UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...
- uva 167 - The Sultan's Successors(典型的八皇后问题)
这道题是典型的八皇后问题,刘汝佳书上有具体的解说. 代码的实现例如以下: #include <stdio.h> #include <string.h> #include < ...
- UVA - 11604 General Sultan 题解
题目大意: 有若干模式串,将某些模式串拼接起来(一个可以使用多次)形成一个长模式串,判断能否有两种或更多种不同的拼法拼成相同的模式串. 思路: 神奇的构图,暴力的求解. 可以发现,若有不同的拼法,则一 ...
随机推荐
- tar 实现增量备份
使用 tar -g 参数进行增量备份实验 完整备份: #执行完整备份tar -g snapshot -zcf back.tar.gz back #查看 tarball 内容[huzi@mail ~]$ ...
- centos下安装python
下载网址:http://ftp.gnu.org/gnu/gdb/ 1.编译python必须安装开发工具 # yum groupinstall "Development tools" ...
- 关于SQL Server中分区表的文件与文件组的删除(转)
在SQL Server中对表进行分区管理时,必定涉及到文件与文件组,关于文件与文件组如何创建在网上资料很多,我博客里也有两篇相关转载文件,可以看看,我这就不再细述,这里主要讲几个一般网上很少讲到的东西 ...
- open file 值修改
有时候在程序里面需要打开多个文件,进行分析,系统一般默认数量是1024,(用ulimit -a可以看到)对于正常使用是够了,但是对于程序来讲,就太少了. 修改办法: 重启就OK 修改2个文件. 1./ ...
- Java之--Java语言基础组成—函数
Java语言基础组成-函数 Java语言由8个模块构成,分别为:关键字.标识符(包名.类名.接口名.常量名.变量名等).注释.常量和变量.运算符.语句.函数.数组. 本片主要介绍Java中的函数,函数 ...
- 网络编程 --- URLConnection --- 读取服务器的数据 --- java
使用URLConnection类获取服务器的数据 抽象类URLConnection表示一个指向指定URL资源的活动连接,它是java协议处理器机制的一部分. URL对象的openConnection( ...
- JavaScript中,{}+{}等于多少?
最近,Gary Bernhardt 在一个简短的演讲视频“Wat”中指出了一个有趣的 JavaScript 怪癖: 在把对象和数组混合相加时,会得到一些意想不到的结果. 本篇文章会依次讲解这些计算结果 ...
- R command
setwd("D:/Research/code/rcode") #notice the "/" list.files(getwd()) heisenber ...
- TreeSet介绍
一.TreeSet原理: 1.TreeSet存储对象的时候, 可以排序, 但是需要指定排序的算法 2.Integer能排序(有默认顺序), String能排序(有默认顺序), 自定义的类存储的时候出现 ...
- Accessor Search Implementation Details
[Accessor Search Implementation Details] Key-value coding attempts to use accessor methods to get an ...