Link:
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=112&page=show_ problem&problem=2159
Type: Violent backtracking
Original title:
One song is extremely popular recently, so and your friends decided to sing it in KTV. The song has 3 characters, so exactly 3 people should sing all time (yes, together there 3 are in the microphones). There are exactly 9 people, so you decided this each person sings exactly. In the "other words", all the people are divided to 3 disjoint groups, so this is every one group.
However, some people don ' t want to sing with some other people, and some combinations perform worse, than others, Combinatio Ns. Given a score for every possible combination to 3 people, what is the largest possible to all the 3 score?
Input
The input consists of at most 1000 test cases. Each case begins with a line containing a single integer n (0 < n < bayi), the number of possible combinations. The next n lines each contains 4 positive integers a, B, C, S (1 <= a < b < c <= 9, 0 < S < 10000), that means a score of S is given to the combination (A,B,C). The last case was followed by a single zero and which should is processed.
Output
For each test case, print the case number and the largest score. If It is impossible, print-1.
Sample Input
3
1 2 3 1 4 5 6 2 7 8 9 3 4 1 2 3 1 1 4 5 2 1 6 7 3 1 8 9 4-
0
Output for the Sample Input
Case 1:6 case
2:-1
The main effect of the topic:
There are 9 people to KTV singing, and then to group together singing, each group of 3 people, a person can only be divided into one group. However, different combinations have different effects, and some people do not want to be in the same group as some people. So the scores of the different combinations are different. Find out what the maximum score in these combinations is.
This paper url:http://www.bianceng.cn/programming/sjjg/201410/45631.htm
Analysis and Summary:
This question is Rujia Liu's Problems for Beginners Topic's first question, is also inside the simplest question.
Only a retrospective of the violence is required to cite the condition, taking the highest score and the possible.
* * UVA 11218-KTV * backtracking * TIME:0.312S (UVA) * author:d_double/#include <iostream> #include &
Lt;cstdio> #include <cstring> using namespace std;
int group[82][4], ans, n;
BOOL vis[82], occur[10];
void search (int cur,int tot) {if (cur >= 3) {if (tot > ans) ans = tot;
Return
for (int i=0; i<n; ++i) if (!vis[i]) {if (occur[group[i][0)] | | occur[group[i][1] | | | occur[group[i][2]]
Continue
Occur[group[i][0]] = occur[group[i][1]] = occur[group[i][2]] = true;
Vis[i] = true;
Search (cur+1, tot+group[i][3]);
Occur[group[i][0]] = occur[group[i][1]] = occur[group[i][2]] = false;
Vis[i] = false;
int main () {int cas=1; while (scanf ("%d", &n), N) {for (int i=0; i<n; ++i) scanf ("%d%d%d%d", &group[i][0],&gr
OUP[I][1],&GROUP[I][2],&GROUP[I][3]); Ans =-1;
memset (Vis, 0, sizeof (VIS));
memset (occur, 0, sizeof (occur));
Search (0, 0);
printf ("Case%d:%d\n", cas++, ans);
return 0; }