Square Destroyer UVA - 1603 IDA*

4 篇文章 0 订阅

紫书的代码 写起来很麻烦 以后自己独立再写。 把完整图里面的正发形都枚举出来,算出每一个正方形是否差了边 然后ida*一直搜就好了    有意思的是 ida*的maxd是放在dfs()里面实现的 内附注释

// UVa1603 Square Destroyer
// Rujia Liu
// This code implements a variant of an algorithm presented in a book. It's simple yet efficient.
// Readers are encouraged to experiment on other algorithms.
// However, it's still slow for n=5 and m=0 (which is NOT in judge input)
// If you really want an efficient solution, learn DLX (Algorithm X with dancing links)
// DLX is well expained (with code) in my other book <<Beginning Algorithm Contests -- Training Guide>>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;

const int maxs = 60; // number of squares: 25+16+9+4+1=55
const int maxm = 60; // number of matches: 2*5*(5+1)=60

int n, exists[maxm]; // matches
int s, size[maxs], fullsize[maxs], contains[maxs][maxm]; // squares
int best;

inline int row_match(int x, int y) {   
  return (2*n+1)*x+y;//横着为n竖着为n+1所以为  (n*2+1)*x y是横向的平移值所以直接相加
}

inline int col_match(int x, int y) {// 比上面多n
  return (2*n+1)*x+n+y;
}

// number of matches in a full n*n grid
inline int match_count(int n) {
  return 2*n*(n+1);
}



void init() {
  int m, v;
  scanf("%d%d", &n, &m);
  for(int i = 0; i < match_count(n); ++i) exists[i] = 1;
  while(m--) {
    scanf("%d", &v);
    exists[v-1] = 0;
  }

  // collect full squares   
  s = 0;
  memset(contains, 0, sizeof(contains));
  for(int i = 1; i <= n; i++) // side length
    for(int x = 0; x <= n-i; x++)
      for(int y = 0; y <= n-i; y++) {
        size[s] = 0;
        fullsize[s] = 4*i; // number of matches in a complete square
        for(int j = 0; j < i; j++) {
          int a = row_match(x, y+j); // up  上边
          int b = row_match(x+i, y+j); // down  下边
          int c = col_match(x+j, y); // left 左
          int d = col_match(x+j, y+i); // right 右
          contains[s][a] = 1;
          contains[s][b] = 1;
          contains[s][c] = 1;
          contains[s][d] = 1;
          size[s] += exists[a] + exists[b] + exists[c] + exists[d]; // 匹配了的火柴数量
        }
        ++s;
      }
}

int find_square() {
  for(int i = 0; i < s; i++)
    if(size[i] == fullsize[i]) return i;  //返回已经完整的正方形编号
  return -1;
}

void dfs(int dep) {
  if(dep >= best) return;  

  int k = find_square(); // 找到一个正方形  因为是init是从小到大枚举的所以小的没了大的也有可能没了
  if(k == -1) {
    best = dep;
    return;
  }

  // remove a match in that square
  for(int i = 0; i < match_count(n); i++)
    if(contains[k][i]) { //如果这个k正方形和该i编号的边有关系则删除这条边 删除的时候需要把和这条边有关系的所有正方形已经匹配的边减1
      for(int j = 0; j < s; j++)
        if(contains[j][i]) size[j]--;
      dfs(dep + 1);
      for(int j = 0; j < s; j++)
        if(contains[j][i]) size[j]++;
    }
}

int main() {
  int T;
  scanf("%d", &T);
  while(T--) {
    init();
    best = n*n;//上限
    dfs(0);
    printf("%d\n", best);
  }
  return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值