题目链接: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)的更多相关文章

  1. 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 ...

  2. uva 167 - The Sultan&#39;s Successors(典型的八皇后问题)

    这道题是典型的八皇后问题,刘汝佳书上有具体的解说. 代码的实现例如以下: #include <stdio.h> #include <string.h> #include < ...

  3. uva167 The Sultan's Successors

    The Sultan's Successors Description The Sultan of Nubia has no children, so she has decided that the ...

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

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

  5. UVA.839 Not so Mobile ( 二叉树 DFS)

    UVA.839 Not so Mobile ( 二叉树 DFS) 题意分析 给出一份天平,判断天平是否平衡. 一开始使用的是保存每个节点,节点存储着两边的质量和距离,但是一直是Runtime erro ...

  6. 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 ...

  7. UVa 12118 检查员的难题(dfs+欧拉回路)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  8. 开篇,UVA 755 && POJ 1002 487--3279 (Trie + DFS / sort)

    博客第一篇写在11月1号,果然die die die die die alone~ 一道不太难的题,白书里被放到排序这一节,半年前用快排A过一次,但是现在做的时候发现可以用字典树加深搜,于是乐呵呵的开 ...

  9. UVA - 524 Prime Ring Problem(dfs回溯法)

    UVA - 524 Prime Ring Problem Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & % ...

随机推荐

  1. 关于css的默认宽度

    <div class="boxa"> <div class="boxb">我是div</div> </div> ...

  2. hud 2089 不要62 (数位dp)

    #include<stdio.h> #include<string.h> #include<math.h> #define max 10 ]; int number ...

  3. CRM中的一个函数,保存一下,别系统被ぅ崩坏就麻烦了.

    CREATE OR REPLACE function UXQLCRM.GET_WEI_XIU(htfid in varchar2) ); CURSOR cr_bg_jl is select " ...

  4. 6.PHP 教程_PHP数据类型

    PHP数据类型 String(字符串),Integer(整型),Float(浮点型),Boolean(布尔型),Array(数组),Object(对象), NULL(空值). PHP字符串 一个字符串 ...

  5. c语言编写经验逐步积累4

    寥寥数笔,记录我的C语言盲点笔记,仅仅为以前经历过,亦有误,可交流. 1.逻辑表达式的使用 取值 = 表达式 ? 表达式1:表达式2: 比方x = y > z ? y:z 2."+,- ...

  6. C++设计模式之状态模式(四)

    4.状态模式总结 状态模式将一个对象在不同状态下的不同行为封装在一个个状态类中,通过设置不同的状态对象能够让环境对象拥有不同的行为.而状态转换的细节对于client而言是透明的.client不直接操作 ...

  7. Flex中神奇的快速辅助 Ctrl+1

    Adobe Flash Builder 中的快速辅助功能提供基于上下文的辅助,有助于您快速执行任务.通过快速辅助,可以在适用于当前代码段的操作列表中选择一个操作. 要调用快速辅助,请在编辑器的上下文菜 ...

  8. Oracle存储过程Procedure语法及案例

    create or replace procedure replace(desstr in varchar2, replacestr in varchar2, tablename in varchar ...

  9. Intellij IDEA 常用 设置 及 快捷键 (持续更新)

    Transparent native-to-ascii conversion以下设置都可以通过 设置中的搜索框 进行关键字搜索 0, 打开Project 设置 Command + ; 1, 打开Mod ...

  10. HTML5 按字母顺序排列的标签列表 new : HTML5 中的新标签。

    标签 描述 <!--...--> 定义注释. <!DOCTYPE>  定义文档类型. <a> 定义超链接. <abbr> 定义缩写. <acron ...