The Sultan's Successors UVA 167(八皇后问题)

说说:

其实这道题本质上就是一个八皇后问题。唯一的区别就是每个棋盘的格子都对应一个数字。最后要求输出,对应的解占据的格子的和的最大值。这只要在最后求出解的时候统计一下就可以了。下面就简单的说说八皇后问题,其实解法也不难。因为要求每行每列都要有棋子。因此只要确定每一行对应的棋子的列数就可以了。而对于每个棋子的所放的位置,同列上和对角线上不能有其他棋子,这个只要设一个访问数组保存一下就可以了。(注意要记得回溯)。至于对角线的表示方法,例如所在位置为(x,y),那么一条对角线可以用x+y表示,另一条对角线可以用x-y表示,但是由于数组的下标非负,因此可以将第二条对角线的值设为x-y+7,这样就可以用一个二维数组来表示,一个相应的位置上能不能放棋子啦。具体的分析,请参见刘汝佳的《算法竞赛入门经典》P123
八皇后问题。

源代码:

#include <stdio.h>
#include <string.h>

char vis[3][17];
int c[9];//存放每行的棋子所在的列数
int val[9][9];
int max,num=0;

void search(int);

int main(){
  int k,i,j;
//  freopen("data","r",stdin);
  scanf("%d",&k);

  while(k--){
    for(i=1;i<=8;i++)
      for(j=1;j<=8;j++)
        scanf("%d",&val[i][j]);
    memset(vis,0,sizeof(vis));

    max=0;
    search(1);
    printf("%5d\n",max);

  }
  return 0;
}

void search(int cur){
  int i,sum;

  if(cur>8){
    sum=0;
    for(i=1;i<=8;i++)
      sum+=val[i][c[i]];
    num++;
    max=sum>max?sum:max;
  }
  else
   for(i=1;i<=8;i++)
     if(!vis[0][i]&&!vis[1][cur+i]&&!vis[2][cur-i+7]){//同列,同对角线都没有其他棋子
       vis[0][i]=vis[1][cur+i]=vis[2][cur-i+7]=1;
       c[cur]=i;
       search(cur+1);
       vis[0][i]=vis[1][cur+i]=vis[2][cur-i+7]=0;//注意回溯
     }
 return ;
}

The Sultan's Successors UVA 167(八皇后问题)

时间: 2024-08-27 15:30:51

The Sultan's Successors UVA 167(八皇后问题)的相关文章

The Sultan&#39;s Successors UVA - 167

the squares thus selected sum to a number at least as high as one already chosen by the Sultan. (For those unfamiliar with the rules of chess, this implies that each row and column of the board contains exactly one queen, and each diagonal contains n

Don&#39;t Get Rooked UVA 639(八皇后问题变形)

说说: 这道题目类似于八皇后问题.有n*n的正方形棋盘,且n<=4.例如在n=4时,有下图所示的棋盘,其中每两个棋子不能放在同一行或者同一列,除非有围墙(黑色的格子)将它们隔开.求给定的棋盘,能放下的最多的棋子数目. 分析: 在八皇后问题中,我们对整个棋盘分成八行考虑的,每行插入一个棋子.所以对于这道题目解决方案也类似,同样是一行一行插入.但是与八皇后问题不同的是,本题中棋盘一行可能插入多个棋子,也可能没有棋子.所以在递归函数中,不仅要给出所要处理的行的信息,也要给出所要处理的列的信息,其实就是

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

这道题是典型的八皇后问题,刘汝佳书上有具体的解说. 代码的实现例如以下: #include <stdio.h> #include <string.h> #include <stdlib.h> int vis[100][100];//刚開始wrong的原因就是这里数组开小了,开了[8][8],以为可以.后来看到[cur-i+8]意识到可能数组开小了.改大之后AC. int a[8][8]; int C[10]; int max_,tot; void search_(int

UVA The Sultan&#39;s Successors (八皇后问题)

 The Sultan's Successors  The Sultan of Nubia has no children, so she has decided that the country will be split into up to k separate parts on her death and each part will be inherited by whoever performs best at some test. It is possible for any in

Uva 167 The Sultan&#39;s Successors

题目链接:Uva 167 思路:     八皇后问题,采用回溯法解决问题. 代码: #include <iostream> #include <string.h> using namespace std; const int MAX_N = 10; int A[MAX_N]; int M[MAX_N][MAX_N]; int num, Max = 0; int is_safe( int row, int col ) { for ( int i = 0; i < row; ++

UVa 167(八皇后)、POJ2258——记两个简单回溯搜索

UVa 167 题意:八行八列的棋盘每行每列都要有一个皇后,每个对角线上最多放一个皇后,让你放八个,使摆放位置上的数字加起来最大. 参考:https://blog.csdn.net/xiaoxiede_wo/article/details/79973171 1 #include <iostream> 2 #include <cstring> 3 #include <iomanip> 4 using namespace std; 5 int pic[9][9]; 6 in

UVA The Sultan&#39;s Successors

题目如下: The Sultan's Successors  The Sultan of Nubia has no children, so she has decided that thecountry will be split into up to k separate parts on her death andeach part will be inherited by whoever performs best at some test. Itis possible for any

笨办法解决 八皇后问题

八皇后问题,是一个古老而著名的问题,是回溯算法的典型案例.该问题是国际西洋棋棋手马克斯·贝瑟尔于1848年提出:在8×8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行.同一列或同一斜线上,问有多少种摆法. 高斯认为有76种方案.1854年在柏林的象棋杂志上不同的作者发表了40种不同的解,后来有人用图论的方法解出92种结果.计算机发明后,有多种计算机语言可以解决此问题. 本人以数学坐标为依据进行计算: 假设棋盘的左下角空格坐标为  x:1,y:1 ,右上角 空格坐标

[OpenJudge] 百练2754 八皇后

八皇后 Description 会下国际象棋的人都很清楚:皇后可以在横.竖.斜线上不限步数地吃掉其他棋子.如何将8个皇后放在棋盘上(有8 * 8个方格),使它们谁也不能被吃掉!这就是著名的八皇后问题. 对于某个满足要求的8皇后的摆放方法,定义一个皇后串a与之对应,即a=b1b2...b8,其中bi为相应摆法中第i行皇后所处的列数.已经知道8皇后问题一共有92组解(即92个不同的皇后串).给出一个数b,要求输出第b个串.串的比较是这样的:皇后串x置于皇后串y之前,当且仅当将x视为整数时比y小. I