POJ 1084 Square Destroyer A*,BFS

这篇博客讲述了作者使用A*算法解决POJ 1084 Square Destroyer问题的过程。最初尝试用正方形数量作为估价函数失败,后受到刘汝佳BFS算法的启发,采用最优性剪枝策略,以每个正方形为对象,从小正方形开始删除边。同时,文章提到了正方形判断的巧妙算法,并表示未来会学习Dancing Link算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目地址:http://poj.org/problem?id=1084

看到很多用Dancing Link X 算法,但没接触过,而且是为了练习A*,所以就想A*

一开始用 还有几个正方形数目代表h()估价函数,但是答案错了,因为有的火柴能破坏3个,有的2个,有的1 个....不能代表估价函数,并没有相容性

错误代码如下:

#include<iostream>
#include<cstdio>
#include<set>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long LL;
const int maxn=60+5;
int n,tot;
struct Node{
	bool Gird[maxn];
	int f,g,h;
	Node(bool *G,int g,int h):g(g),h(h){memcpy(Gird,G,sizeof(bool)*(tot+2));f=g+h;}
	bool operator < (const Node& n) const {
		return f>n.f;
	}
};
int SquareNum(bool *G,int i,int t){  //i为上边 ,一共有几个正方形 
	int cnt=0;
	int L,R;
	bool ok;
	for(int j=0;j<=t-i;j++)
	{
		L=(i+n)+(2*n+1)*j,R=i+j;
		if(G[L]&&G[R]&&L<=tot)
		{
			L+=n+1,R+=n+1; ok=true;
			for(int k=0;k<=j&&ok;k++)
			{
				if(!G[L]||!G[R]||L>tot) ok=false;
				L+=1,R+=2*n+1; 
			}
			if(ok) cnt++;
		}
		else break;
	}
	return cnt;
}
int H(bool *G){
	int cnt=0;
	for(int j=n;j<tot;j+=2*n+1)  //有几个正方形就是至少需要几个步骤 
	for(int i=j-n+1;i<=j;i++)	
		cnt+=SquareNum(G,i,j);
	return cnt;
}
void Switch(bool *Gird,int n){
	Gird[n]^=true; //取反 
}
set<LL> closed;
bool inClosed(bool *Gird){
	LL code=0;
	for(int i=1;i<=tot;i++){  //最多60根 所以状态压缩 long long 就能保存 
		if(Gird[i]) code|=1;
		code<<=1;
	}
	if(closed.count(code)) return true;
	closed.insert(code);
	return false;
} 
int A_Star(Node s)
{
	closed.clear();
	priority_queue<Node> open;
	open.push(s);
	inClosed(s.Gird);
	while(!open.empty())
	{
		Node u=open.top(); open.pop();
		if(u.h==0) return u.g;
		for(int i=1;i<=tot;i++)
		{
			if(!u.Gird[i]) continue;
			bool G[maxn]; memcpy(G,u.Gird,sizeof(G));
			Switch(G,i);
			if(inClosed(G)) continue;
			open.push(Node(G,u.g+1,H(G))); 
		}
	}
}
int main()
{
	int T; bool Gird[maxn]; int m;
	cin>>T;
	while(T--)
	{
		cin>>n>>m;
		tot=2*n*n+2*n;  //n*n总共有2*n*(n+1)个火柴 
		memset(Gird,true,sizeof(Gird));
		while(m--){
			int x; cin>>x;
			Switch(Gird,x);
		}
		cout<<A_Star(Node(Gird,0,H(Gird)))<<endl;
	}
	return 0;
}

一直在思考估价函数怎么表示...


看了刘汝佳大神算法,BFS用最优性剪枝就搞定了

思路如下:以每一个正方形为对象,并且必须要以从小正方形到大的顺序,dfs拿去正方形的一条边

而且判断正方形的算法特别巧妙,要事先给原图存在的正方形全部标号,给每个正方形标记存在的边和应该存在边,如1号小正方形存在3条边,而应该存在4条边,所以1号无正方形

代码如下:

// 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; //给矩形编号,size[s]指第s个矩形实际有几条边存在,fullsize[s]指s矩形应该存在条边
//best是答案  // contain[s][e] if true ,代表s矩形含有e这条边 

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;
}

// 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]; // 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;  //404: not found
    return;
  }

  // remove a match in that square
  for(int i = 0; i < match_count(n); i++)
    if(contains[k][i]) {
      for(int j = 0; j < s; j++)
        if(contains[j][i]) size[j]--; //delete 
      dfs(dep + 1);
      for(int j = 0; j < s; j++)      //restore 
        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;
}


Dancing Link 以后学好再写


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值