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 题解
题目大意: 有若干模式串,将某些模式串拼接起来(一个可以使用多次)形成一个长模式串,判断能否有两种或更多种不同的拼法拼成相同的模式串. 思路: 神奇的构图,暴力的求解. 可以发现,若有不同的拼法,则一 ...
随机推荐
- Private Bytes,Working Set,Virtual Size的区别
http://aigo.iteye.com/blog/1930209 http://stackoverflow.com/questions/1984186/what-is-private-bytes- ...
- YII 配置文件
用YIIFramework的库开发 .... Yii::createWebApplication($config); //没有run Yii::import(class1,true),在将class1 ...
- Python 异常相关参考
Python所有的异常都是从BaseException类派生的,常见的错误类型和继承关系如下: BaseException +-- SystemExit +-- KeyboardInterrupt + ...
- python27+django调用数据库
我用的mysql版,先上无模板的版本,在views里加上 import MySQLdb 然后在下方写函数 def use_databases(request): db = MySQLdb.connec ...
- mac配置appium进行自动化测试
先去brew.sh这个网站,拷贝最新的下载安装命令,黏贴到终端里,回车,按提示下载安装. 安装完毕后,运行brewupdate 看看是否有更新,然后输入brew ios-webkit-debug-pr ...
- scanf()/getchar()和gets()深入分析
C/C++学习笔记1 - 深入了解scanf()/getchar()和gets()等函数 ---------------------------------------------------- | ...
- poj 3006 Dirichlet's Theorem on Arithmetic Progressions
题目大意:a和d是两个互质的数,则序列a,a+d,a+2d,a+3d,a+4d ...... a+nd 中有无穷多个素数,给出a和d,找出序列中的第n个素数 #include <cstdio&g ...
- 一排下去再上来的div
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- linux硬件时间修改与查看
linux修改时间和日期.查看修改硬件时间 Linux时钟分为系统时钟(System Clock)和硬件(Real Time Clock,简称RTC)时钟.系统时钟是指当前Linux Kernel中的 ...
- mysql cluster 名词概念解读
Node Group [number_of_node_groups] = number_of_data_nodes / NoOfReplicas Partition When using ndbd, ...