uva 167 - The Sultan'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 cur)
{
    int i,j;
    if(cur==8)
    {
        int ans=0;
        tot++;
        for(int k=0;k<8;k++)
        {
            ans+=a[k][C[k]];
        }
        if(ans>max_)
            max_=ans;
    }
    else for(i=0;i<8;i++)
    {
        if(!vis[0][i]&&!vis[1][cur+i]&&!vis[2][cur-i+8])
        {
            C[cur]=i;
            vis[0][i]=vis[1][cur+i]=vis[2][cur-i+8]=1;
            search_(cur+1);
            vis[0][i]=vis[1][cur+i]=vis[2][cur-i+8]=0;
        }
    }
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        for(int i=0;i<8;i++)
        {
            for(int j=0;j<8;j++)
            {
                scanf("%d",&a[i][j]);
            }
        }
        max_=-1;
        tot=0;
        memset(vis,0,sizeof(vis));
        search_(0);
        printf("tot = %d\n",tot);
        printf("%5d\n",max_);
    }
    return 0;
}

作为一仅仅菜鸟,应该在发现错误的时候多去找一找自己在哪些方面考虑的还不够好,而不是去抱怨这个抱怨那个。

你再抱怨,你依旧是那个什么都不会的菜鸟,而你抱怨的人也依旧会是那个大神。

不管遇到什么样奇怪无语的情况,都要冷静下来。多思考,不抱怨。

uva 167 - The Sultan's Successors(典型的八皇后问题)

时间: 2024-08-16 07:02:51

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

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; ++

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

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

说说: 其实这道题本质上就是一个八皇后问题.唯一的区别就是每个棋盘的格子都对应一个数字.最后要求输出,对应的解占据的格子的和的最大值.这只要在最后求出解的时候统计一下就可以了.下面就简单的说说八皇后问题,其实解法也不难.因为要求每行每列都要有棋子.因此只要确定每一行对应的棋子的列数就可以了.而对于每个棋子的所放的位置,同列上和对角线上不能有其他棋子,这个只要设一个访问数组保存一下就可以了.(注意要记得回溯).至于对角线的表示方法,例如所在位置为(x,y),那么一条对角线可以用x+y表示,另一条对

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(八皇后)、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

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

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

深度搜索(dfs)+典型例题(八皇后)

深度优先搜索简称深搜,从起点出发,走过的点要做标记,发现有没走过的点,就随意挑一个往前走,走不了就回退,此种路径搜索策略就称为“深度优先搜索”,简称“深搜”. 如上面的图所示:加入我们要找一个从V0到V6的一条最短的路径.我们可以看到有许多的路我们可以走. V0——V3——V5——V6: V0——V3——V1——V4: V0——V3——V1——V2——V6: V0——V1——V4: V0——V1——V3——V5——V6: V0——V1——V2——V6: V0——V2——V6: 前两组,是从节点V

UVA 1397 - The Teacher&amp;#39;s Side of Math(高斯消元)

UVA 1397 - The Teacher's Side of Math 题目链接 题意:给定一个x=a1/m+b1/n.求原方程组 思路:因为m*n最多20,全部最高项仅仅有20.然后能够把每一个此项拆分.之后得到n种不同无理数,每一项为0.就能够设系数为变元.构造方程进行高斯消元 一開始用longlong爆了.换成分数写法也爆了,又不想改高精度.最后是机智的用了double型过的,只是用double精度问题,所以高斯消元的姿势要正确,而且最后输出要注意-0的情况 代码: #include

UVA - 10239 The Book-shelver&amp;#39;s Problem

Description Problem D The Book-shelver's Problem Input: standard input Output: standard output Time Limit: 5 seconds Memory Limit: 32 MB You are given a collection of books, which must be shelved in a library bookcase ordered (from top to bottom in t