Uva 167 The Sultan's Successors(dfs)
题目链接:Uva 167
思路分析:八皇后问题,采用回溯法解决问题。
代码如下:
#include <iostream>
#include <string.h>
using namespace std; const int MAX_N = ;
int A[MAX_N];
int M[MAX_N][MAX_N];
int num, Max = ; int is_safe( int row, int col )
{
for ( int i = ; i < row; ++i )
{
if ( A[i] == col )
return false;
if ( A[i] + i == row + col )
return false;
if ( i - A[i] == row - col )
return false;
} return true;
} void dfs( int row )
{
if ( row == )
{
for ( int i = ; i < ; ++i )
num += M[i][A[i]];
if ( num > Max )
Max = num; num = ;
return;
}
else
{
for ( int i = ; i < ; ++i )
{
if ( is_safe( row , i ) )
{
A[row] = i;
dfs( row+ );
} }
}
} int main( )
{
int n; cin >> n;
while ( n-- )
{
num = Max = ;
memset( M, , sizeof( M ) );
memset( A, , sizeof( A ) ); for ( int i = ; i <= ; ++i )
for ( int j = ; j <= ; ++j )
cin >> M[i][j]; dfs( );
printf( "%5d\n", Max );
} return ;
}
Uva 167 The Sultan's Successors(dfs)的更多相关文章
- 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 ...
- uva 167 - The Sultan's Successors(典型的八皇后问题)
这道题是典型的八皇后问题,刘汝佳书上有具体的解说. 代码的实现例如以下: #include <stdio.h> #include <string.h> #include < ...
- 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(八皇后)、POJ2258 The Settlers of Catan——记两个简单回溯搜索
UVa 167 题意:八行八列的棋盘每行每列都要有一个皇后,每个对角线上最多放一个皇后,让你放八个,使摆放位置上的数字加起来最大. 参考:https://blog.csdn.net/xiaoxiede ...
- UVA.839 Not so Mobile ( 二叉树 DFS)
UVA.839 Not so Mobile ( 二叉树 DFS) 题意分析 给出一份天平,判断天平是否平衡. 一开始使用的是保存每个节点,节点存储着两边的质量和距离,但是一直是Runtime erro ...
- 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 12118 检查员的难题(dfs+欧拉回路)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 开篇,UVA 755 && POJ 1002 487--3279 (Trie + DFS / sort)
博客第一篇写在11月1号,果然die die die die die alone~ 一道不太难的题,白书里被放到排序这一节,半年前用快排A过一次,但是现在做的时候发现可以用字典树加深搜,于是乐呵呵的开 ...
- UVA - 524 Prime Ring Problem(dfs回溯法)
UVA - 524 Prime Ring Problem Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & % ...
随机推荐
- CSS文字样式
font-family:通常文章的正文使用的是易读性较强的serif字体,用户长时间阅读下不easy疲劳.而标题和表格则採用较醒目的sans-serif字体.Web设计及浏览器设置中也推荐遵循此原则. ...
- linux多线程示例
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <pthread.h& ...
- BZOJ 1189: [HNOI2007]紧急疏散evacuate( BFS + 二分答案 + 匈牙利 )
我们可以BFS出每个出口到每个人的最短距离, 然后二分答案, 假设当前答案为m, 把一个出口拆成m个表示m个时间, 点u到出口v的距离为d, 那么u->v的[d, m]所有点连边, 然后跑匈牙利 ...
- Apache与Nginx网络模型
Nginx的高并发得益于其采用了epoll模型,与传统的服务器程序架构不同,epoll是linux内核2.6以后才出现的.下面通过比较Apache和Nginx工作原理来比较. 传统Apache都是多进 ...
- html加载与脚本运行中,由于html未完全加载而导致脚本找不到dom元素无法执行事件
问题: 如题,就是说alert后出现一个对话框,我没有点关闭,其他js文件或html会被加载吗,我遇到一个问题就是在页面加载时调用一个div的click事件,却没有被执行,但是在调用事件前面加上ale ...
- MyEclipse 在loading workbench 启动卡死
解决方法: 找到Workspace目录,在.metadata(Mac 下是在 .metadata/.plugins)中删掉以下两个文件 org.eclipse.ui.workbench org.ecl ...
- SIF与CIF
SIF 动态图像专家组(MPEG)在1992年推出的MPEG-1标准中首次定义了SIF(Source Input Format,源输入格式).CCIR 601标准(现改为ITU-R BT.601标准) ...
- 高质量程序设计指南C/C++语言——C++/C常量(2)
- [转] IOS中AppDelegate中的生命周期事件的调用条件
IOS中AppDelegate中的生命周期事件的调用条件 //当应用程序将要进入非活动状态执行,在此期间,应用程序不接受消息或事件,比如来电 - (void)applicationWillResign ...
- 自定义标签(TagSupport )
转载:http://zhuhuide2004.iteye.com/blog/555737 这个图太好了,拿下来,标注一下: