记录编号 329188 评测结果 AAAAAAAAAA
题目名称 [NOIP 2011]玛雅游戏 最终得分 100
用户昵称 GravatarFoolMike 是否通过 通过
代码语言 C++ 运行时间 2.857 s
提交时间 2016-10-24 21:45:32 内存使用 0.29 MiB
显示代码纯文本
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<queue>
using namespace std;
inline void swap(char &x,char &y){char z=x;x=y;y=z;}
struct sit{
	char s[7][5],ans[5][3];int tmp;
	bool empty(){
		for (int i=0;i<7;i++)
		for (int j=0;j<5;j++)
		if (s[i][j]) return 0;
		return 1;
	}
	void check();//判断消除 
	void go_down(){//判断掉落 
		bool re=0;
		for (int i=0;i<5;i++){
			int cnt=0,p=0;
			for (int j=0;j<7;j++)
			if (s[j][i]) cnt++;
			for (int j=0;j<7;j++)
			if (s[j][i]){
				if (i!=p) re=1;
				int z=s[j][i];
				s[j][i]=0;s[p++][i]=z;
			}
		}
		if (re) check();
	}
}Fir;
void sit::check(){
	bool re=0;char ans[7][5];
	memcpy(ans,s,sizeof ans);
	for (int i=0;i<5;i++)
	for (int j=1;j<6;j++)
	if (s[j-1][i]==s[j][i]&&s[j][i]&&s[j][i]==s[j+1][i])
		ans[j-1][i]=ans[j][i]=ans[j+1][i]=0,re=1;
	for (int i=0;i<7;i++)
	for (int j=1;j<4;j++)
	if (s[i][j-1]==s[i][j]&&s[i][j]&&s[i][j]==s[i][j+1])
		ans[i][j-1]=ans[i][j]=ans[i][j+1]=0,re=1;
	if (re) memcpy(s,ans,sizeof ans),go_down();
}
int n,sum;char ans[5][3];bool OK;
void dfs(sit x){
	x.go_down();x.check();
	if (x.tmp==n-1&&x.empty()){
		for (int i=0;i<n;i++,puts(""))
		for (int j=0;j<3;j++) printf("%d ",(int)x.ans[i][j]);
		exit(0);
	}
	if (x.tmp==n-1) return;
	x.tmp++;
	for (int i=0;i<5;i++)
	for (int j=0;j<7;j++)
	if (x.s[j][i]){
		if (i<4&&x.s[j][i+1]!=x.s[j][i]){
			sit t=x;
			swap(t.s[j][i],t.s[j][i+1]);
			t.ans[t.tmp][0]=i;
			t.ans[t.tmp][1]=j;
			t.ans[t.tmp][2]=1;
			dfs(t);
		}
		if (i&&x.s[j][i-1]!=x.s[j][i]&&!x.s[j][i-1]){
			sit t=x;
			swap(t.s[j][i],t.s[j][i-1]);
			t.ans[t.tmp][0]=i;
			t.ans[t.tmp][1]=j;
			t.ans[t.tmp][2]=-1;
			dfs(t);
		}
	}
}
int main()
{
	freopen("mayan.in","r",stdin);
	freopen("mayan.out","w",stdout);
	scanf("%d",&n);
	for (int i=0,p=0,j;i<5;p=0,i++)
	while (1){
		scanf("%d",&j);
		if (!j) break;
		Fir.s[p++][i]=j;
	}
	Fir.tmp=-1;dfs(Fir);
	puts("-1");
	return 0;
}