求优化/卡常
查看原帖
求优化/卡常
396994
Winston12321_楼主2022/7/27 17:11
#include <iostream>
#include <cstring>
using namespace std;
int n;
int map[7][10][7];
int checkM[7][10];
int xx[7],yy[7],ww[7],xxx[7],yyy[7],www[7];
bool flag;
int ans=7;
inline void swap(int &a,int &b)
{
	a=a^b;
	b=a^b;
	a=a^b;
}
inline int read()
{
	int s=0;
	char ch=getchar();
	while(!isdigit(ch)) ch=getchar();
	while(isdigit(ch))
	{
		s=(s<<3)+(s<<1)+(ch^'0');
		ch=getchar();
	}
	return s;
}
void In()
{
	n=read();
	int k,GET;
	for(int i=1;i<=5;++i)
	{
		k=0;
		GET=read();
		while(GET)
		{
			++k;
			map[i][k][0]=GET;
			GET=read();
		}
	}
}
void check(int ste)
{
	for(int i=1;i<=5;++i)
	{
		int k=0; 
		for(int j=1;j<=7;++j)
		{
			if(!map[i][j][ste]) ++k;
			else if(k!=0) map[i][j-k][ste]=map[i][j][ste],map[i][j][ste]=0;
		}
	}
	bool b=0;
	memset(checkM,0,sizeof(checkM));
	flag=1;
	for(int i=1;i<=5;++i)
		for(int j=1;j<=7;++j)
		{
			int color=map[i][j][ste];
			if(color!=0)
			{
				flag=0;
				if(i<=3 && map[i+1][j][ste]==color && map[i+2][j][ste]==color)
					b=1,checkM[i][j]=checkM[i+1][j]=checkM[i+2][j]=1;
				if(j<=5 && map[i][j+1][ste]==color && map[i][j+2][ste]==color)
					b=1,checkM[i][j]=checkM[i][j+1]=checkM[i][j+2]=1;
			}
			else break;
		}
	if(b)
	{
		for(int i=1;i<=5;++i)
			for(int j=1;j<=7;++j)
				if(checkM[i][j])
					map[i][j][ste]=0;
		check(ste);
	}
}
inline void swi(int x,int y,int way,int ste)
{
	for(int i=1;i<=5;++i)
		for(int j=1;j<=7;++j)
			map[i][j][ste+1]=map[i][j][ste];
	swap(map[x+way][y][ste+1],map[x][y][ste+1]);
	xxx[ste+1]=x,yyy[ste+1]=y,www[ste+1]=way;
}
void print()
{
	if(ans>n)
	{
		putchar('-');
		putchar('1');
	}
	else for(int i=1;i<=ans;++i)
	{
		putchar((xx[i]-1)+'0');
		putchar(' ');
		putchar((yy[i]-1)+'0');
		putchar(' ');
		if(ww[i]==1) putchar('1');
		else
		{
			putchar('-');
			putchar('1');
		}
		putchar('\n');
	}
}
void dfs(int step)
{
	check(step);
	if(step>n || step>=ans) return;
	if(flag)
	{
		if(step<ans)
		{
			ans=step;
			for(int i=1;i<=ans;++i) xx[i]=xxx[i],yy[i]=yyy[i],ww[i]=www[i];
		}
		return;
	}
	for(int i=1;i<=5;++i)
		for(int j=1;j<=7;++j)
			if(map[i][j][step])
			{
				if(i<=4 && map[i][j][step]!=map[i+1][j][step])
				{
					swi(i,j,1,step);
					dfs(step+1);
				}
				if(i>=2 && !map[i-1][j][step])
				{
					swi(i,j,-1,step);
					dfs(step+1);
				}
			}
			else break;
}
int main()
{
	In();
	dfs(0);
	print();
	return 0;
}
2022/7/27 17:11
加载中...