Problem description
There is an onion on the m*m square, you need to fix some fences to surround them. A fence is a closed pattern that is built along the grid (that is, a circle). Each fence should be disjoint, non-overlapping, and not included with each other. If you fix a maximum of K fences, what is the minimum length of all fences?
Input format
The first row of three integers n,m,k.
The next n rows of two integers per line represent the position of a single onion.
Output format
An integer on one line represents the answer.
Sample Input 1
6 1 4
1 3
4 2
4 4
6 4
Sample Output 1
18
Sample Input 2
6 2 4
1 3
4 2
4 4
6 4
Sample Output 2
16
Sample explanation
Guess what's on the tree.
Search + Wonderful card when unexpectedly can pass.
You can tell by drawing that the cost of enclosing an irregular pattern and enclosing a rectangular flower is the same, which reduces the code difficulty much.
#include <iostream> #include <cmath> #include <cstring> #include <cstdio> #include <
algorithm> #include <queue> #include <ctime> #define INF 1000000000 #define LL Long long using namespace std;
int x[50],y[50],n,m,k;
int ans;
int SX[50],SY[50],MX[50],MY[50],A[50];
void Dfs (int t)//search each onion in which fence {if (clock () >1970)//Plus card, inexplicably over, then has updated the answer.
{printf ("%d", ans);
Exit (0);
} if (t>n) {int sum=0;
for (int i=1;i<=k;i++) {if (A[i]) sum+= (mx[i]-sx[i]+1) *2+ (my[i]-sy[i]+1) * *;
} ans=min (Ans,sum);
Return
} int sum=0;
for (int i=1;i<=k;i++) {if (A[i]) sum+= (mx[i]-sx[i]+1) *2+ (my[i]-sy[i]+1) * *;
if (sum>=ans) return;//Optimization Pruning} for (int i=1;i<=k;i++) {int qx=sx[i],qy=sy[i],px=mx[i],py=my[i];
Sx[i]=min (Sx[i],x[t]);
Sy[i]=min (Sy[i],y[t]);
Mx[i]=max (Mx[i],x[t]); My[i]=max (my[i],y[t]);
a[i]++;
DFS (T+1);
a[i]--;
sx[i]=qx;sy[i]=qy;
Mx[i]=px;my[i]=py;
}} int main () {freopen ("dan.in", "R", stdin);
Freopen ("Dan.out", "w", stdout);
memset (sx,127,sizeof (SX));
memset (sy,127,sizeof (SY));
scanf ("%d%d%d", &m,&k,&n);
for (int i=1;i<=n;i++) scanf ("%d%d", &x[i],&y[i]);
Ans=inf;
DFS (1);
printf ("%d", ans);
return 0; }