CSU 1855:shut The Box-like pressure BFS (mid-central USA regional Contest) __ State compression

Source: Internet
Author: User
Tags time limit

1855:shut the Box Submit Page    Summary    time limit:1 sec     Memory Limit : 256 mb     submitted:37     solved:9     Description


Shut the Box is a one-player game, begins with a set of n pieces labeled from 1 to N. All pieces are initially ' unmarked ' (in the ' right ', the unmarked pieces are in the those upward). In the version we consider, a player is allowed up to T turns, with each turn defined by a independently chosen value V ( Typically determined by rolling one or more dice). During a turn, the player must designate a set of currently unmarked pieces, whose numeric labels add precisely to V, and M Ark them. The game continues either until the player runs out of turns, or until a single turn when it becomes impossible to find a Set of unmarked pieces summing to the designated value V (in which case it and all further turns are). The goal is to mark as many pieces as possible; Marking all pieces is known as "shutting the box." Your goal is to determine the maximum number of pieces so can be marked by a fixed sequence of turns.

As a example, consider a game with 6 pieces and the following sequence of Turns:10, 3, 4, 2. The best outcome for tha T sequence is to mark a total of four pieces. This can is achieved by using the value of the pieces 1+4+5, and then using the value of 3 to mark piece 3. At "Point", the game would end as there was no way to precisely use the turn with value 4 (the final turn of value 2 mus T be forfeited as OK. An alternate strategy for achieving the same number of marked pieces would is to use the value to mark four pieces 1+2+ 3+4, with the game ending in the turn with value 3. But There does not exist any way to mark five or more pieces with that sequence. Input

Each game begins with a line containing two integers, N, T where 1≤n≤22 represents the number of pieces, and 1≤t≤n Represents the maximum number of turns that would be allowed. The following line contains T integers designating the sequence of turn values for the game; Each such value V would satisify 1≤v≤22. You must read this entire sequence from the input, even though a particular game-might end on a unsuccessful turn prior t o The end of the sequence. The data set ends with a line containing 0 0. Output

Should output a single to each game, as shown below, reporting the ordinal for the game and the maximum number O F pieces that can is marked during that game. Sample Input

6 4
3 4 2 6 5
ten
2 4 5 3 1 1 3 4 5 6 7 8 9 22 21 20 19 18 17 16 9 8 7 6 5 4 3 2 1 0 0

Sample Output
Game 1:4
Game 2:6
Game 3:1
Game 4:22

In the fourth game, the individual thinks he is playing well, on the spot a has this shape pressure. If not the team-mates because of environmental problems and WA's problem, we should be able to line up to the top five ...

At that time in the scene to knock on this problem when the idea is not clear, interrupted two times (write half to let others write other questions first), finally wrote out. Now after the game after the idea of a half hour to knock out direct A, the fact that the idea clear, the algorithm is clear than the blind writing important Ah, although finally found this problem is not difficult ~

This is a look at the data range 22, and then each box and only two states and down, the logical direct thought of the shape of pressure. With the first box of the binary number of the first I, 1 for the box is pressed down, 0 for the box has not been pressed down. In the end we just need to find 1 of the states that appear most often.

The first reaction is to think in the direction of DP, enumerate each state, and finally find the maximum number of box that is pressed down in all feasible states. In this case, however, the total state number is 1<<22, and with 22 numbers to split, the complexity of space-time is soaring (I'm stuck here for the first time). Then I found that many states were completely unnecessary to enumerate. There is only one state at the beginning (all 0), and the back state is expanded step by bit, that is, the extra steps do not need to be enumerated, just keep moving in these expanded points. That's about as much as the monkey of the second school game. Use a BFS to continue to expand the state, until finally unable to expand. For each state, the condition that can be transferred is the bitwise and 0 of the previous state and the current scheme, that is, there is no location that makes the scheme and the original state all 1, and requires that the state not appear (pruning.) Extra. )。

A further refinement is the pretreatment of the scheme (very shameless to say that I am in this card for the second time). The solution is to split a number, save all the split schemes in a vector, and add a weight (the number of 1 added to the scheme). This is a simple search, but I wrote in the first time actually think that can not be pruning, the results of decisive tle, even if the data is small also need to add these basic pruning ... Search is the basis, specifically how to cut I will not say.

At the end of the game, the long manager said the problem is the shortest. In fact, it is the same, the first thing to do is to each state of the edge, and then the number of 1 increase is the corresponding edge of the weight, the map after the longest road is the result, essentially the same, the BFS process is to ask the longest. Finally, the last code, this is the version of the replay after the game, than the original version to be clear:

#include <bits/stdc++.h> #define MAXN using namespace std;
	struct node {int ans,x,y;

Node () {} node (int _x,int _y,int _ans): X (_x), Y (_y), ans (_ans) {}};
Vector<int> G[maxn],sta;

Deque<node> que;
int m,n,w[maxn][100];

BOOL v[1<<maxn];
		inline void dfs (int x,int i,int b) {if (!x) {int num=0;
		for (int j=0;j<sta.size (); j + +) num|=1<<sta[j];
		W[i][g[i].size ()]=sta.size (); G[i].push_back (num);
	Return } if (x<b| |
	B>n) return;
	Sta.push_back (b);
	DFS (X-B,I,B+1);
	Sta.pop_back ();
DFS (X,I,B+1);
	int main () {int t=0;
		while (~SCANF ("%d%d", &n,&m) &&n&&m) {for (int i=0;i<n;i++) g[i].clear ();
		Memset (W,0,sizeof (w)); 
			for (int i=0;i<m;i++) {int x;
			scanf ("%d", &x);
		DFS (x,i,1);
		int Ans=0;que.clear ();
		memset (v,0,sizeof (v));
		Que.push_back (Node (0,0,0));
			while (!que.empty ()) {node I=que.front ();
			Que.pop_front (); for (int j=0;j<g[i.y].size (); j + +) if ((I.X&AMP;G[I.Y)[j])
					==0 &&!v[i.x|g[i.y][j]]) {node K=node (i.x|g[i.y][j],i.y+1,i.ans+w[i.y][j]);
					Que.push_back (k);
					Ans=max (Ans,k.ans);
				V[i.x|g[i.y][j]]=1;
	} printf ("Game%d:%d\n", ++t,ans); 
 }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.