UVa 167 - The Sultan's Successors 递归回溯

来源:互联网 发布:java第三方登录接口 编辑:程序博客网 时间:2024/06/05 12:46

 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 1617 18 19 20 21 22 23 2425 26 27 28 29 30 31 3233 34 35 36 37 38 39 4041 42 43 44 45 46 47 4848 50 51 52 53 54 55 5657 58 59 60 61 62 63 64

Sample output

  260
译文:

努比亞的蘇丹沒有子女,所以他要從一些有資格的繼承者中挑選一個出來繼承王位。他希望這個繼承者是夠聰明的,所以他決定用一個遊戲來測試這些人。

他準備了一個西洋棋盤,上面的每個格子中均有一個1到99的數字。他又準備了8個皇后棋子。每位參加遊戲的人必須將8個皇后放置到棋盤中,且各皇后彼此不可互相攻擊。可以想像,這樣有不只一種的放置方式。而蘇丹要挑選的繼承者就是那位可以放置8個皇后,並且放置皇后的8個位置中的數的和為最大的那一個人。

你的任務就是讀入棋盤上的數,幫蘇丹算出可以放置8個皇后的最大的和是多少。

Input

輸入的第一列有一個整數k(k<= 20),代表以下有幾組測試資料(就是幾個棋盤)。

每組測試資料有8列,每列有8個整數(介於0到99)。代表棋盤中格子的資料。請參考Sample Input。

Output

對每一組測試資料,輸出可以放置8個皇后的最大的和是多少。輸出長度為5,靠右對齊。請參考Sample Output。

#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int MAX=8+1;int map[MAX][MAX];int vis_col[MAX], vis_45[16], vis_135[16];int ans=0;void init(){     ans = 0;     memset(map, 0, sizeof(map));     memset(vis_col, 0, sizeof(vis_col));     memset(vis_45, 0, sizeof(vis_45));     memset(vis_135, 0, sizeof(vis_135));}void read(){  for(int i=1; i <= 8; i++)  {     for(int j=1; j <=8; j++)         {            cin >> map[i][j];         }       }}/*八皇后问题列出所有的可能性求最大的和 */void dfs(int row, int sum){    if(row > 8)    {       if(sum > ans) ans = sum;       return ;    }    // 遍历所有列的可能性     for(int col=1; col <= 8; col++)    {       if(!vis_col[col] && !vis_45[row+col-1] && !vis_135[col+8-row])       {          vis_col[col]=vis_45[row+col-1]=vis_135[col+8-row]=1;          sum += map[row][col];          dfs(row+1, sum);          sum -= map[row][col];          vis_col[col]=vis_45[row+col-1]=vis_135[col+8-row]=0;       }    }}void solve(){     dfs(1, 0);      // 没注意输出细节 居然PE 了一次,这是第一次出现PE    //  cout << ans << endl;     printf("%5d\n", ans);}int main(){//    freopen("in.txt","r",stdin);    int nCase;    cin >> nCase;    while(nCase--)    {       init();       read();       solve();    } return 0;}


1 0
原创粉丝点击