Certainly! Below is a C++ program that solves the problem of arranging given rectangular fragments to form a square frame. The program reads the input, checks if the total area of the rectangles forms a perfect square, and then uses backtracking to count all possible valid arrangements.
Explanation:
-
Input and Normalization:
- The program first reads the number of rectangles
n and their dimensions.
- It normalizes each rectangle by ensuring that width
w is always less than or equal to height h.
-
Sorting:
- The rectangles are sorted in descending order based on area to optimize the backtracking process. This helps in placing larger rectangles first, potentially reducing the search space.
-
Orientation Handling:
- For each rectangle, the program precomputes possible orientations (original and rotated by 90 degrees if
w ≠ h). This allows the backtracking algorithm to try both orientations during placement.
-
Backtracking:
- The core of the solution is a recursive backtracking function that attempts to place each rectangle in the first available empty position in the grid (representing the square frame).
- It ensures that rectangles do not overlap and stay within the bounds of the square.
- The program avoids counting duplicate arrangements by ensuring that identical rectangles are placed in a fixed order.
-
Counting Valid Arrangements:
- Every time the entire grid is successfully filled with rectangles without overlaps, the count of valid arrangements is incremented.
-
Output:
- Finally, the program outputs the total number of valid arrangements.
C++ Implementation:
#include <bits/stdc++.h>
using namespace std;
struct Rectangle {
int w;
int h;
};
// Recursive backtracking function
long long search(vector<char> &grid, const vector<Rectangle> &rects, const vector<vector<Rectangle>> &orientations, vector<bool> &used, int s){
// Find the first empty cell
int pos = -1;
for(int i=0; i < s*s; ++i){
if(!grid[i]) { pos = i; break; }
}
if(pos == -1){
// All cells are filled
return 1;
}
int r = pos / s;
int c = pos % s;
long long total = 0;
for(int j=0; j < rects.size(); ++j){
if(!used[j]){
// Skip duplicates
if(j > 0 && rects[j].w == rects[j-1].w && rects[j].h == rects[j-1].h && !used[j-1]) continue;
// Try all possible orientations for the current rectangle
for(auto &ori : orientations[j]){
int rw = ori.w, rh = ori.h;
// Check if the rectangle fits horizontally and vertically
if(c + rw > s || r + rh > s) continue;
bool can_place = true;
// Check if the space is free
for(int dr=0; dr < rh && can_place; ++dr){
for(int dc=0; dc < rw && can_place; ++dc){
if(grid[(r + dr)*s + (c + dc)] ){
can_place = false;
}
}
}
if(can_place){
// Place the rectangle
for(int dr=0; dr < rh; ++dr){
for(int dc=0; dc < rw; ++dc){
grid[(r + dr)*s + (c + dc)] = 1;
}
}
used[j] = true;
// Recurse
total += search(grid, rects, orientations, used, s);
// Unplace the rectangle
for(int dr=0; dr < rh; ++dr){
for(int dc=0; dc < rw; ++dc){
grid[(r + dr)*s + (c + dc)] = 0;
}
}
used[j] = false;
}
}
}
}
return total;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<Rectangle> rects(n);
for(int i=0;i<n;i++){
int w, h;
cin >> w >> h;
if(w > h){
swap(w, h);
}
rects[i] = Rectangle{w, h};
}
// Sort rectangles: first by descending area, then by w descending, then h descending
sort(rects.begin(), rects.end(), [&](const Rectangle &a, const Rectangle &b) -> bool{
long long area_a = (long long)a.w * a.h;
long long area_b = (long long)b.w * b.h;
if(area_a != area_b) return area_a > area_b;
if(a.w != b.w) return a.w > b.w;
return a.h > b.h;
});
// Compute total area
long long A =0;
for(int i=0;i<n;i++) A += (long long)rects[i].w * rects[i].h;
// Check if A is a perfect square
double sqrtA_d = sqrt((double)A);
int s = round(sqrtA_d);
if((long long)s * s != A){
cout << "0";
return 0;
}
// Precompute possible orientations
vector<vector<Rectangle>> orientations(n);
for(int i=0;i<n;i++){
if(rects[i].w != rects[i].h){
orientations[i].emplace_back(rects[i]);
orientations[i].emplace_back(Rectangle{rects[i].h, rects[i].w});
}
else{
orientations[i].emplace_back(rects[i]); // Only one orientation if square
}
}
// Initialize grid and used array
vector<char> grid(s * s, 0);
vector<bool> used(n, false);
// Start the recursive search
long long total = search(grid, rects, orientations, used, s);
cout << total;
}
How It Works:
- The program first reads the number of fragments and their dimensions.
- It ensures each rectangle is stored with the smaller dimension first.
- It then sorts the rectangles to optimize the placement order.
- The
search function recursively tries to place each rectangle in the first available position, considering all possible orientations.
- It avoids counting duplicate arrangements by skipping over identical rectangles that have already been placed.
- Finally, the program prints the total number of valid arrangements.
Example Usage:
For the first sample input:
3
3 4
1 5
4 2
The output will be:
8
For the second sample input:
4
2 2
2 2
2 2
2 2
The output will be:
1
This solution ensures all possible valid arrangements are counted accurately, considering rectangle rotations and avoiding duplicates.