题目链接:https://vjudge.net/problem/ZOJ-3209
Your boss once had got many copies of a treasure map. Unfortunately, all the copies are now broken to many rectangular pieces, and what make it worse, he has lost some of the pieces. Luckily, it is possible to figure out the position of each piece in the original map. Now the boss asks you, the talent programmer, to make a complete treasure map with these pieces. You need to make only one complete map and it is not necessary to use all the pieces. But remember, pieces are not allowed to overlap with each other (See sample 2).
Input
The first line of the input contains an integer T (T <= 500), indicating the number of cases.
For each case, the first line contains three integers n m p (1 <= n, m <= 30, 1 <= p <= 500), the width and the height of the map, and the number of pieces. Then p lines follow, each consists of four integers x1 y1 x2 y2 (0 <= x1 < x2 <= n, 0 <= y1 < y2 <= m), where (x1, y1) is the coordinate of the lower-left corner of the rectangular piece, and (x2, y2) is the coordinate of the upper-right corner in the original map.
Cases are separated by one blank line.
Output
If you can make a complete map with these pieces, output the least number of pieces you need to achieve this. If it is impossible to make one complete map, just output -1.Sample Input
3 5 5 1 0 0 5 5 5 5 2 0 0 3 5 2 0 5 5 30 30 5 0 0 30 10 0 10 30 20 0 20 30 30 0 0 15 30 15 0 30 30
Sample Output
1 -1 2
Hint
For sample 1, the only piece is a complete map.
For sample 2, the two pieces may overlap with each other, so you can not make a complete treasure map.
For sample 3, you can make a map by either use the first 3 pieces or the last 2 pieces, and the latter approach one needs less pieces.
题解:
题意:有p个矩形,每个矩形的坐标均已知,问能否找到若干个矩形(不能有重叠),组成一个n*m的大矩形?如果能?找出最小值。
两个注意点:
1.一开始以为覆盖的对象是“点”,结果发现在拼接处会重复覆盖。后来才知道覆盖的对象是一个“小格”,即单位正方形。这样才是以面积覆盖掉n*m的大矩形。代码中以(x,y)这个点代表了((x-1,y-1) (x,y))这个单位正方形,所以这个大矩形的横纵坐标从1开始。
2.在写Dance()函数时,忘了修改代码。由于此题要求求出最小值,所以即使当前找到某个值,也不一定是最小值,还要继续回溯。所以Dance()函数的类型是 void, 而不是bool。
代码如下:
#include
#include
#include
#include
#include
#include
#include
#include
#include