To strengthen the concept of DP+bitmasks
SPOJ(6072. Chocolate)
Chocolate is a problem from the world final ACM-ICPC 2010. You can see the text here
My solution: Well first we can see that the n <= 15 and x,y <= 100, There is 2^n way of get a subset of the all pieces. For example: n = 4; pieces:6,3,2,1; In the binary representation of the mark a 0 meant that don't take this piece and a 1 that take this piece.
We can do a function that take a mask ,x ,y and return true or false if with the piece of this mask we can get a rectangle of (x,y) bool Solve(int mask , int x , int y) { if(Sum[mask]!= x*y) return false; if(ActivePieces(mask)==1) return true; foreach(sub)//that sub is a subset of mask mean that all bit that is 1 in sub have to be 1 in mask and mask > sub { sub1 = mask - sub; //now we can to break the chocolate of (x,y), for can break it parallel to X, y have to divide to Sum[sub] and Sum[sub1]. if(Sum[sub]%y == 0 and Sum[sub1]%y == 0) { if(Solve(sub,Sum[sub]/y,y) and Solve(sub1,Sum[sub1]/y,y)) return true; } // now the same parallel to Y if(Sum[sub]%x == 0 and Sum[sub1]%x == 0) { if(Solve(sub,Sum[sub]/x,x) and Solve(sub1,Sum[sub1]/x,x)) return true; } } return false; } Now see that we can to have 2^n*x*y state. for the max number of that can be 327680000 so it is big but we can notice that x or y are linked.If I have the mask and x I can calculate the y so we have only 3276800. so my idea for save is (mask,max(x,y)) and I can use memorization for do it faster. The other problem is: How can I do the cycle of sub ?? if you have this problem you have to see this tutorial from topcoder. My Code: |
No comments:
Post a Comment