zoukankan      html  css  js  c++  java
  • The Sultan's Successors UVA

    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 no more than one.) Write a program that will read in the number and details of the chessboards and determine the highest scores possible for each board under these conditions. (You know that the Sultan is both a good chess player and a good mathematician and you suspect that her score is the best attainable.) Input Input will consist of k (the number of boards), on a line by itself, followed by k sets of 64 numbers, each set consisting of eight lines of eight numbers. Each number will be a positive integer less than 100. There will never be more than 20 boards. Output Output will consist of k numbers consisting of your k scores, each score on a line by itself and right justified in a field 5 characters wide.

    Sample Input

    1

    1 2 3 4 5 6 7 8

    9 10 11 12 13 14 15 16

    17 18 19 20 21 22 23 24

    25 26 27 28 29 30 31 32

    33 34 35 36 37 38 39 40

    41 42 43 44 45 46 47 48

    48 50 51 52 53 54 55 56

    57 58 59 60 61 62 63 64

    Sample Output

    260

    #include <algorithm>
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <cmath>
    using namespace std;
    int c[150],vis[3][150],tot,n=8,sum_max;
    int mapn[9][9];
    void search(int cur)
    {
        if(cur==n)//递归边界,只要走到了这里,所有皇后必然不冲突
        {
            if(sum_max<tot)
                sum_max = tot;
        }
        else for(int i=0;i<n;i++)
        {
            if(!vis[0][i]&&!vis[1][cur+i]&&!vis[2][cur-i+n])//利用二维数组直接判断
            {//0为竖行,1为副对角线,2为主对角线
                c[cur] = i;//保存下每行皇后的位置
                tot += mapn[cur][i];
                vis[0][i] = vis[1][cur+i] = vis[2][cur-i+n] = 1;
                search(cur+1);
                vis[0][i] = vis[1][cur+i] = vis[2][cur-i+n] = 0;//记得改回来
                tot -= mapn[cur][i];
            }
        }
    }
    int main()
    {
        int T;
        cin >> T;
        while(T--)
        {
            sum_max = 0,tot = 0;
            memset(vis,0,sizeof(vis));
            for(int i=0;i<8;i++)
                for(int j=0;j<8;j++)
                {
                    cin >> mapn[i][j];
                }
            search(0);
            printf("%5d
    ",sum_max);
        }
        return 0;
    }
    彼时当年少,莫负好时光。
  • 相关阅读:
    Yii Framework 开发教程: 总结
    code first 如何创建索引字段
    Entity Framework 使用 Left Join
    如何查看IIS并发连接数
    c# List<int> 转 string 以及 string [] 转 List<int>
    linq中如何在join中指定多个条件
    关于学习的感悟
    c# 的访问修饰符是private 还是 internal?
    C# 与 .Net 3.5 高级程序设计(第四版) 读书笔记(一)
    解决Excel 提示向程序发送命令时出现错误
  • 原文地址:https://www.cnblogs.com/l609929321/p/6906122.html
Copyright © 2011-2022 走看看