[算法竞赛入门经典]UVa1603 Square Destroyer 破坏正方形 详细注解

https://vjudge.net/problem/UVA-1603

// https://github.com/aoapc-book/aoapc-bac2nd/blob/master/ch7/UVa1603.cpp
// 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 n不大于5
const int maxm = 60; // number of matches: 2*5*(5+1)=60

// exists ==1,有该编号火柴 ==0,无该编号火柴
int n, exists[maxm]; // matches 火柴
// maxs 正方形块的总数
// contains 某一正方形块有哪些编号的火柴
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;
}

inline int col_match(int x, int y) {
  return (2*n+1)*x+n+y;
}

// 一个n*n的满格,拥有的火柴数
// 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;
  // contains[maxs][maxm]
  memset(contains, 0, sizeof(contains));
  // 边长 [1, n]
  for(int i = 1; i <= n; i++) // side length
    // 第s个正方形的x,y 坐标
    for(int x = 0; x <= n-i; x++)
      for(int y = 0; y <= n-i; y++) {
        // 第s个正方形有几条边
        size[s] = 0;
        // 第s个正方形的周长(满边的火柴数)
        fullsize[s] = 4*i; // number of matches in a complete square 围一圈
        // 总共可能会有4*i条边,所以j从0~i-1,每个循环内match4次
        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]; // number of matches now
        }
        ++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();
  if(k == -1) {
    best = dep;
    return;
  }

  // remove a match in that square
  // 一个n*n的满格,拥有的火柴数
  for(int i = 0; i < match_count(n); i++)
    // 如果第k个正方形存在编号为i的火柴
    if(contains[k][i]) {
      // 移掉一根火柴,影响一组正方形
      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;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值