POJ 2965 The Pilots Brothers' refrigerator 枚举

本文介绍了一种用于解决“Pilots Brothers: following the stripy elephant”游戏中冰箱门开关问题的算法。该算法通过全组合模板有效节省时间和内存,避免了冗余操作,实现了最小开关次数的目标。实例分析及AC代码展示了算法的具体应用。

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


Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 15784 Accepted: 5950 Special Judge

Description

The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to open a refrigerator.

There are 16 handles on the refrigerator door. Every handle can be in one of two states: open or closed. The refrigerator is open only when all handles are open. The handles are represented as a matrix 4х4. You can change the state of a handle in any location[i, j] (1 ≤ i, j ≤ 4). However, this also changes states of all handles in rowi and all handles in columnj.

The task is to determine the minimum number of handle switching necessary to open the refrigerator.

Input

The input contains four lines. Each of the four lines contains four characters describing the initial state of appropriate handles. A symbol “+” means that the handle is in closed state, whereas the symbol “−” means “open”. At least one of the handles is initially closed.

Output

The first line of the input contains N – the minimum number of switching. The rest N lines describe switching sequence. Each of the lines contains a row number and a column number of the matrix separated by one or more spaces. If there are several solutions, you may give any one of them.

Sample Input

-+--
----
----
-+--

Sample Output

6
1 1
1 3
1 4
4 1
4 3
4 4

这道题和上面我发的那道POJ1753很像,应该都算枚举吧,和POJ1753一样,这道题如果一个冰箱门被打开大于2次是没有意义的,具体做法就是先直接判断,当前冰箱门是否全开,如果是,输出0,否则就要利用全组合模板进行运算了,充分运用全组合的模板可以有效节省时间和内存,防止TLE,在每次全组合模板得到结果的时候对当前序列进行开门关门处理,然后判断是否全开,全开的时候记录当前序列,然后退出全组合函数,输出当前判断的开门数,以及在全组合里面记录下的数列对应的坐标,当然,毕竟是简单粗暴,依然还是很费时间的,时间达到了719MS,再有300MS也TLE了。

具体的AC代码如下:

#include<cstdio>
#include<iostream>
using namespace std;
struct st
{
	int x;
	int y;
}point[25];
int pa[20];
char a[5][5],b[5][5];
void init()
{
	int i,j,k=1;
	for(i=1;i<=4;i++)
		for(j=1;j<=4;j++)
		{
			point[k].x=i;
			point[k].y=j;
			k++;
		}
}
int judge()
{
	int i,j;
	for(i=1;i<=4;i++)
		for(j=1;j<=4;j++)
			if(b[i][j]=='+')
				return 0;
	return 1;
}
void change(int x,int y)
{
	int i;
	for(i=1;i<=4;i++)
	{
		if(b[x][i]=='+')
			b[x][i]='-';
		else
			b[x][i]='+';
		if(b[i][y]=='+')
			b[i][y]='-';
		else
			b[i][y]='+';
	}
	if(b[x][y]=='+')
		b[x][y]='-';
	else
		b[x][y]='+';
}
void ret()
{
	int i,j;
	for(i=1;i<=4;i++)
		for(j=1;j<=4;j++)
			b[i][j]=a[i][j];
}
int createfab(int m,int n) //全组合
{
     int i,j,lcount,wa[20];
     for(i=1;i<=n;i++)     wa[i]=i;
     wa[n+1]=m+1;
	 for(i=1;i<=n;i++)
	 change(point[wa[i]].x,point[wa[i]].y);
	 if(judge()==1)
	 {
		 for(i=1;i<=n;i++)
			 pa[i]=wa[i];
		 return n;
	 }
	 ret();
     lcount=1;
     while(wa[1]<m-n+1) {
          for(i=n;i>0;i--) {
               if(wa[i]<wa[i+1]-1) {
                    wa[i]++;
                    for(j=i;j<n;j++)     
						wa[j+1]=wa[j]+1;
					for(i=1;i<=n;i++)
	                    change(point[wa[i]].x,point[wa[i]].y);
	                if(judge()==1)
					{
		                for(i=1;i<=n;i++)
			                pa[i]=wa[i];
		                return n;
					}
					ret();
                    lcount++;
                    break;
               }
          }
     }
	 return -1;
}
int main()
{
	int i,j,k;
	char ch;
	init();
	for(i=1;i<=4;i++)
		scanf("%s",a[i]+1);
	ret();
	if(judge()==1)
	{
		printf("0\n");
		return 0;
	}
	for(i=1;i<=16;i++)
	{
		j=createfab(16,i);
		if(j!=-1)
		{
			printf("%d\n",j);
			for(k=1;k<=j;k++)
				printf("%d %d\n",point[pa[k]].x,point[pa[k]].y);
			return 0;
		}
    }
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值