「一本通 1.3 练习 2」平板涂色

#include<bits/stdc++.h>
#define lop(x,m,n) for(int x=m;x<=n;x++)
using namespace std;
int n;
struct node{
    int x1,y1,x2,y2,se;
}sq[20];
int used[20]={0};
int maxx,maxy;
bool b[40][40]={0};
void init()
{
    maxx=-1,maxy=-1;
    scanf("%d",&n);
    lop(i,1,n) 
    {
        scanf("%d%d%d%d%d",&sq[i].x1,&sq[i].y1,&sq[i].x2,&sq[i].y2,&sq[i].se);
        maxx=max(maxx,sq[i].x2);
        maxy=max(maxy,sq[i].y2);
                
    }
    
}
int ans=15;
bool check(int x)
{
    if(sq[x].x1==0) return true;
    lop(i,sq[x].y1+1,sq[x].y2)
    {    
        if(b[sq[x].x1][i]==0)
        {
            return false;
        }
    }
    return true;
}
void ss(int x)
{
    lop(i,sq[x].x1+1,sq[x].x2)
     lop(j,sq[x].y1+1,sq[x].y2)
      b[i][j]=1;
}
void zz(int x)
{
    lop(i,sq[x].x1+1,sq[x].x2)
     lop(j,sq[x].y1+1,sq[x].y2)
      b[i][j]=0;
}
void dfs(int k,int p,int num)//第k次拿笔,第p块方板 ,累积num块方板 
{
    if(k>ans) return;
    if(num==n)
    {
        ans=min(ans,k);
    }
    lop(i,1,n)
    {
        if(!used[i]&&check(i))
        {
            used[i]=1;
            ss(i);
            num++;
            if(sq[i].se!=sq[p].se)
            {
                k++;
                dfs(k,i,num);
                
                k--;
            }
            else{
                dfs(k,i,num);
            }
            zz(i);
            num--;
            used[i]=0;
        }
    }
}
int main()
{
    init();
    dfs(0,0,0);
    cout<<ans;
} 
原文地址:https://www.cnblogs.com/719666a/p/9508418.html