题目大意:八皇后问题,每个格子有分数,皇后放置在格子上则得到分数,问怎么放置八皇后总分数最高。

解题思路:回溯,小紫里讲的挺清楚的,只要在达成条件时,将八个位置的分数加起来最后得到最高分输出就可以了。

ac代码:

#include <iostream>
using namespace std;
int n, m, pos[8], sum, score[10][10];
void solve(int cur)
{int temp=0, flag;if (cur == 8){for (int i=0; i<8; i++)temp += score[i][pos[i]];if (temp > sum)sum = temp;}elsefor (int i=0; i<8; i++){pos[cur] = i;flag = 1;for (int j=0; j<cur; j++)if (pos[cur] == pos[j] || cur-pos[cur] == j-pos[j]|| cur+pos[cur] == j+pos[j]){flag = 0;                break;      }if (flag)solve(cur+1);}
}
int main()
{scanf("%d", &n);while (n--){sum = 0;for (int i=0; i<8; i++)for (int j=0; j<8; j++)scanf("%d", &score[i][j]);solve(0);printf("%5d\n", sum);}
return 0;
}

UVA 167 - The Sultan‘s Successors相关推荐

  1. uva 167 The Sultan's Successors

    题目地址: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108& ...

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

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

  3. 167 The Sultan's Successors

    原题: The Sultan of Nubia has no children, so she has decided that the country will be split into up t ...

  4. UVA The Sultan's Successors

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

  5. UVa 167(八皇后)、POJ2258 The Settlers of Catan——记两个简单回溯搜索

    UVa 167 题意:八行八列的棋盘每行每列都要有一个皇后,每个对角线上最多放一个皇后,让你放八个,使摆放位置上的数字加起来最大. 参考:https://blog.csdn.net/xiaoxiede ...

  6. UVA167【The Sultan‘s Successors】(递归与回溯、8皇后问题)

    链接:UVA167[The Sultan's Successors] 题目描述: The Sultan of Nubia has no children, so she has decided tha ...

  7. The Sultan's Successors (八皇后)DFS

    The Sultan's Successors 原题链接https://vjudge.net/contest/345248#problem/F 八皇后问题,既在8*8的棋盘中放置8个皇后,每行,每列, ...

  8. The Sultan's Successors (八皇后)

    The Sultan's Successors(八皇后问题) The Sultan of Nubia has no children, so she has decided that the coun ...

  9. The Sultan's Successors UVA - 167

    //很久以前做的了,当时WA,一直没找到错误...然后今天看了下...发现就是每次开始新一轮年的时候没哟初始化....... The Sultan of Nubia has no children, ...

最新文章

  1. CVPR 2021 双目图像压缩最新进展
  2. 没有找到 html 模板,Spring Boot and Thymeleaf:找不到HTML模板
  3. Python入门100题 | 第006题
  4. pl0源码(可在delphi7中运行)
  5. java设计模式建造_Java设计模式——建造模式(Builder Pattern)
  6. win10 下pycharm+anaconda 编译生成pyd文件
  7. 基于Chrome浏览器的前端调试
  8. ubuntu安装rpm的方法
  9. 微软建了个内部AI大学:“我们很缺人!是时候增强自我造血能力了”
  10. JAVA Cannot find class [×××] for bean with name ‘×ב defined in class path resource [×××] 问题解决方法
  11. 机器学习1-线性回归、Ridge回归、LASSO回归
  12. AM335X 3款核心板比较
  13. mina mysql_SpringBoot集成Socket通讯之Mina框架
  14. 计算机类专业及行业认识,我对于计算机行业的看法
  15. java语言中的变量有哪2种_变量类型有哪些-在C语言中的实型变量分为2种类型,它们是()和()-电气资讯 - 电工屋...
  16. Stata | 缺失值处理标签
  17. 微信小程序—刷脸实名认证
  18. Python 安装wheel.whl文件
  19. 大数据平台下的数据治理
  20. 在dick字典里找最大值并返回key的办法

热门文章

  1. 家庭“好用”优化师:每一件好物都是在积攒生活闪光
  2. android 锯齿
  3. 长发变短发,卷发变直发,坚持每天梳头
  4. 使用Canvas + Path和“曲面细分”算法实现钢笔笔锋效果
  5. android设备连接win10,win10手机连接Android设备、iphone 操作方法
  6. 计算机语言与语法,编程语言中语法和语义有什么区别?
  7. STM32F407的时钟
  8. 机器学习【吴恩达|周志华|李宏毅|算法】清单
  9. 浅谈面向对象的编程思想:如何优雅地把大象装进冰箱?
  10. 论文开题报告要查重吗?