zoukankan      html  css  js  c++  java
  • uva 167

    就是八皇后问题 。。

    题目:

     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 individual to inherit more than one or indeed all of the portions. To ensure that only highly intelligent people eventually become her successors, the Sultan has devised an ingenious test. In a large hall filled with the splash of fountains and the delicate scent of incense have been placed k chessboards. Each chessboard has numbers in the range 1 to 99 written on each square and is supplied with 8 jewelled chess queens. The task facing each potential successor is to place the 8 queens on the chess board in such a way that no queen threatens another one, and so that the numbers on 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



    代码:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <iomanip>
     4 using namespace std;
     5 
     6 int board[10][10];
     7 
     8 int MAX  = 0;
     9 
    10 int COL[10];
    11 void  dfs(int u,int M)
    12 {
    13     if(u == 8)
    14     {
    15         if(M > MAX)
    16         {
    17             MAX = M;
    18         }
    19         return;
    20     }
    21 
    22     for(int i=0;i<8;i++)
    23     {
    24         int  ok = 1;
    25         COL[u] = i;
    26 
    27         for(int j=0;j<u;j++)
    28         {
    29 
    30             if(COL[u]==COL[j] || u-COL[u] == j-COL[j] || u+COL[u]==j+COL[j])
    31             {
    32                 ok = 0;
    33                 break;
    34             }
    35 
    36         }
    37         if(ok) dfs(u+1,M+board[u][COL[u]]);
    38     }
    39 
    40 
    41 }
    42 int main()
    43 {
    44     freopen("in","r",stdin);
    45     int k;
    46     cin>>k;
    47 
    48     while(k--)
    49     {
    50         for(int i=0;i<8;i++)
    51         {
    52             for(int j=0;j<8;j++)
    53             {
    54                 scanf("%d",&board[i][j]);
    55             }
    56         }
    57 
    58 
    59         for(int i=0;i<8;i++)
    60         {
    61             COL[0] = i;
    62             dfs(1,board[0][i]);
    63         }
    64         cout<<setw(5)<<MAX<<endl;
    65         MAX=0;
    66     }
    67 
    68 
    69     return 0;
    70 }
  • 相关阅读:
    asp.net mvc中ViewData、ViewBag和TempData的详解
    在asp.net WebForms中使用路由Route
    Sql Server批量删除指定表
    MongoDB安装并设置为windows服务以使其开机自启
    NPOI操作excel之写入数据到excel表
    NPOI操作excel之读取excel数据
    SQL Server 定时自动备份数据库
    如何用按钮的click事件去触发a标签的click事件
    C# Asp.net Quartz.NET作业调度之创建、安装、卸载、调试windows服务的简单事例
    c#中浅拷贝和深拷贝的理解
  • 原文地址:https://www.cnblogs.com/doubleshik/p/3484508.html
Copyright © 2011-2022 走看看