poj 1691 Painting a Board(状态压缩DP)

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector> 
using namespace std;

struct sq{
       int lx, ly, rx, ry;
       int col;
       void get(){
            scanf("%d %d %d %d %d", &ly, &lx, &ry, &rx, &col);
            col--;
       } 
       vector<int> upsq;
}a[20];

int n;
#define MAX 32769
int d[MAX][20];
int des;
const int inf = 1000; 

void pre(){
     for (int i = 0; i < n; i++){
         for (int j = 0; j < n; j++){
             if (j == i)continue;
             if (a[j].ry == a[i].ly && ((a[j].rx > a[i].lx && a[j].lx < a[i].rx))){
                         a[i].upsq.push_back(j);
             } 
         }
     }
}

bool check(int st, int cur){
     for (int i = 0; i < a[cur].upsq.size(); i++){
         if (((1<<a[cur].upsq[i])&st) ==0)return false;
     }
     return true;
} 

int doit(int st, int col){
    if (d[st][col] != -1)return d[st][col];
    if (st == des)return d[st][col] = 0;
    d[st][col] = inf; 
    for (int i = 0; i < n; i++)if (((1 << i) & st) == 0 && check(st, i)){
         if (col == a[i].col){
                 d[st][col] = min(doit((st | (1<<i)), col), d[st][col]);
         }
         else {
                 d[st][col] = min(doit((st | (1<<i)), a[i].col) + 1, d[st][col]);
         }
    }
    return d[st][col];
}
 
                  
         
     

int main(){
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    int t;
    scanf("%d", &t);
    while (t--){
          scanf("%d", &n);
          for (int i = 0; i < n; i++){ 
               a[i].get();
               a[i].upsq.clear();
          } 
          pre();
          des = 1;
          int ii = 0; 
          while (ii < n)des *= 2, ii++;
          des--;
          //memset(d, -1, sizeof(d)); 
          for (int i = 0; i <= des; i++)
          for (int j = 0; j < 20; j++)
              d[i][j] = -1; 
          int ans = 100; 
         // for (int i = 0; i < n; i++)printf("%d %d $", a[i].upsq.size(), a[i].upsq.size()?a[i].upsq[0]:-1);puts(""); 
          for (int i = 0; i < n; i++){
              doit(0, a[i].col);
              ans = min(ans, d[0][a[i].col]);
          }
          ans++; 
          printf("%d\n", ans);
    }
    return 0;
} 
               
           

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值