Uva--317(回溯 / 规律)
2014-07-20 11:55:19
Hexagon |
Consider a game board consisting of 19 hexagonal fields, as shown in the figure below. We can easily distinguish three main directions in the shape of the board: from top to bottom, from top-left to bottom-right, and from top-right to bottom-left. For each of these primary directions, the board can be viewed as a series of rows, consisting of 3, 4, 5, 4, and 3 fields, respectively.
The game board has to be completely covered using a set of hexagonal pieces. Each piece carries three numbers, one for every primary board direction. Only three different numbers are used for each direction. Every possible combination of three numbers for all three directions is assigned to a piece, leading to a set of 27 unique pieces. (The board in the above figure is still in the process of being covered.)
The score of a board is calculated as the sum of all 15 row scores (5 rows for each primary direction). The row scores are calculated as follows: if all pieces in a row carry the same number for the direction of the row, the row score is this number multiplied by the number of pieces in the row. Otherwise (the pieces carry different numbers in the row direction) the row score is zero. Note that the pieces may not be rotated. For example, the score of the leftmost row in the figure is , the score of the row to its right is .
While in the real game the pieces are chosen randomly and the set of pieces is fixed, we are interested in the highest possible score for a given set of numbers for each direction, when all pieces in a row carry the same number for the direction of the row. This means you have to choose those 19 pieces that result in the highest score.
Input
The first line of the input file contains an integer n which indicates the number of test cases. Each test case consists of three lines containing three integers each. Each of these three line contains the numbers for a single primary direction. From these numbers the set of pieces is generated.
Output
For each test case output a line containing the number of the case (`Test #1', `Test #2', etc.), followed by a line containing the highest possible score for the given numbers. Add a blank line after each test case.
Sample Input
1 9 4 3 8 5 2 7 6 1
Sample Output
Test #1 308
思路:首先这题是可以回溯做的,不过判断冲突的条件有点繁琐,所以想到了找规律。神奇的是竟然被找到了。。。- -!
首先考虑某一个方向,一个数最多可以放两列,最多放9个位置,最少放3个位置。想想就知道如果把最大数放9个位置,结果会最大,但遗憾的是一旦放了9个,后面必然会冲突(可以画图证明)
而画图证明一个数最多可放8个位置(两列4个相连的),那么我们就把最大的数放8个位置,这样后面的放法最大的情况为6和5。所以对一个方向最大可能放法:8,6,5。其他两个方向最大可能放法均为
7,7,5。所以达到max的放法就是:8,6,5 ; 7,7,5 ; 7,7,5。然后把三组数降序排序,三种放法映射到三组数,共有9钟情况,全部考虑一遍取最大值即可。
1 /************************************************************************* 2 > File Name: Uva317.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Sun 20 Jul 2014 10:17:22 AM CST 6 ************************************************************************/ 7 8 #include<iostream> 9 #include<cmath> 10 #include<algorithm> 11 #include<cstring> 12 #include<cstdio> 13 #include<fstream> 14 using namespace std; 15 16 int main(){ 17 int dir[2][3] = {{8,6,5},{7,7,5}}; 18 int t,Case,sum,tmax; 19 int d[3][3]; 20 scanf("%d",&Case); 21 for(int t = 1; t <= Case; ++t){ 22 for(int i = 0; i < 3; ++i){ 23 for(int j = 0; j < 3; ++j){ 24 scanf("%d",&d[i][j]); 25 } 26 } 27 sort(d[0],d[0] + 3); 28 sort(d[1],d[1] + 3); 29 sort(d[2],d[2] + 3); 30 tmax = 0; 31 for(int k = 0; k < 3; ++k){ 32 sum = 0; 33 for(int i = 0; i < 3; ++i){ 34 for(int j = 0; j < 3; ++j){ 35 if(i == k) sum += d[i][j] * dir[0][j]; 36 else sum += d[i][j] * dir[1][j]; 37 } 38 } 39 tmax = max(tmax,sum); 40 } 41 printf("Test #%d\n%d\n",t,tmax); 42 } 43 return 0; 44 }