hdu 3220 Alice’s Cube 逆向搜索+二进制压缩

本文介绍了一道关于16个灯泡状态变化的编程题,通过使用逆向搜索算法,从目标状态出发寻找可达初始状态的有效路径,解决了正向搜索效率低下的问题。

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

这题教会了我逆向搜索

 

题意:

给你一个16个点的立方体,16个点表示16个灯,0表示灯灭,1表示灯亮。给你16个数,这16个数分别表示每个灯的状态,经过n步变换之后变成目标状态,求n

变换规则:

每次变换相邻的两个点(这两个点的状态必须不一样        注意!!!!!!!!!!!!之前我就没注意看   原文  You are allowed to switch the states of two adjacent light bulbs with different states (“on” to “off”, and “off” to “on”; specifically, swap their states) in one operation   with different states这个我之前没看到)

 注意:该题的测试数据有13000组 所以这题刚开始我用的是月神教我的bfs做的,每个测试数据都搜索到第三步,找到就输出步数,否则输出more

但是这样做由于测试数据过多,所以我果断TLE了 (┬_┬)。。。  最后优化了很多次。还是TLE

这时候我才觉得自己的方法有问题啊。。。

最终状态为255(用二进制表示),立方体共32条边,用逆向搜索可以得到:以终点为起点,搜索三步内到达的状态(顶多32*32*32=32768种 ),打表记录下来即可

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<limits.h>
using namespace std;
	int q[55][2]={{0,0},
				   {1,2},{1,3},{1,5},{1,9},
				        {2,4},{2,6},{2,10},
				        {3,4},{3,7},{3,11},
				        	  {4,8},{4,12},
				        {5,6},{5,7},{5,13},
							  {6,8},{6,14},
						      {7,8},{7,15},
							  	  	{8,16},
						{9,10},{9,11},{9,13},
							   {10,12},{10,14},
							   {11,12},{11,15},
							           {12,16},
								{13,14},{13,15},
										{14,16},
										{15,16},			   				  						
				};// 记录该立方体的32条边 
struct node
{
	int a;
	int step;
	int s1,s2;
}list[55555];
int ci[20];
int hash[70000];// 记录每一种状态到达255所需的步数 
void bfs(int x)
{
	//	FILE *p;
	//	p = fopen("text.txt","w");
		//fprintf(p,"1 2 3\n");
		list[0].a= x;
		hash[x]= 0;
		list[0].step= 0;
		int rear,front;
		rear= front= 0;	
		while(rear<= front)
		{
			if(list[rear].step<=2)
			{
				for(int i=1; i<= 32; i++)
				{
					int x= q[i][0];
					int y= q[i][1];
					list[front+1].step= list[rear].step+ 1;
					list[front+1].a= list[rear].a;
					int t1= list[rear].a>>(16-x);
					int t2= list[rear].a>>(16-y);
					if(t1%2!=t2%2)
					{
						if(t1%2==0)
							list[front+1].a+= ci[16-x]; 
						else
							list[front+1].a-= ci[16-x];
						if(t2%2==0)
							list[front+1].a+= ci[16-y]; 
						else
							list[front+1].a-= ci[16-y];	
						front++;
						if(hash[list[front].a]==-1)
						{
							hash[list[front].a]= list[front].step;
						//	fprintf(p,"%d %d\n",list[front].a,hash[list[front].a]);
						}
					}
				}
			}
			rear++;	
		}
	//fclose(p);	
		//printf("%d\n",rear);	
}
int main()
{
	ci[0]= 1;
	for(int i=1; i<= 16; i++)
		ci[i]= ci[i-1]*2;
	memset(hash,-1,sizeof(hash));
	bfs(255);
	int m;
	scanf("%d",&m);
	for(int T= 1; T<= m; T++)
	{	
		list[0].a= 0;
		int t;		
		for(int j=16,i=1; i<= 16; i++,j--)
		{
			scanf("%d",&t);
			list[0].a+=t* ci[j-1];	
		}
		printf("Case #%d: ",T);
		if(hash[list[0].a]!=-1)
			printf("%d\n",hash[list[0].a]);
		else
			printf("more\n");
	}
	return 0;
}


 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值