The Pilots Brothers’ refrigerator
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 27917 Accepted: 10810 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 row i and all handles in column j.
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
Source
Northeastern Europe 2004, Western Subregion
飞行员兄弟的冰箱
原题链接
时间限制: 1000MS 内存限制: 65536K
提交总数: 27917 接受: 10810 特别法官
描述
游戏“飞行员兄弟:跟随有条纹的大象”有玩家需要打开冰箱的任务。
冰箱门上有16个把手。每个把手可以处于以下两种状态之一:打开或关闭。冰箱仅在所有把手打开时才打开。把手表示为矩阵4х4。您可以在任何位置[i,j](1≤i,j≤4)更改句柄的状态。但是,这也会改变第i行所有句柄的状态和第j列所有句柄的状态。
其任务是确定打开冰箱所需的最少手柄开关次数。
输入
输入包含四行。四行中的每一行都包含四个描述适当句柄初始状态的字符。符号“+”表示手柄处于关闭状态,而符号“ - ”表示“打开”。至少有一个手柄最初是关闭的。
输出
输入的第一行包含N - 最小切换次数。其余的N行描述开关顺序。每行包含由一个或多个空格分隔的矩阵的行号和列号。如果有几个解决方案,你可以给他们中的任何一个。
示例输入
-+--
----
----
-+--
示例输出
6
1 1
1 3
1 4
4 1
4 3
4 4
资源
东北欧2004年,西部分区域
题解
这题和上次的POJ-1753 Flip Game一题差不多,大致想法就是矩阵转化成一个二进制数,那么 0 到 217−1 2 17 − 1 之间,每个数字都是一种状态。说白了总共就只有这么点状态,所以刷 BFS 就可以了。
当然,也可以刷DFS,每个点最多会被选中一次(因为选中两次等于没选中),这样一来,我们可以把选中的点看作 +,没选中的看作 -,然后枚举这个矩阵,最多也是 217 2 17 次就可以了。
代码
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn=(1<<17)+3;
int L[6],R[6],vis[maxn],lst[maxn],q[maxn],X[maxn],Y[maxn],til,hea;
int readch()
{
char ch=getchar();while (ch!='+'&&ch!='-')ch=getchar();return (int)(ch=='+');
}
void dfs(int stp)
{
if (lst[stp]<0) return;
printf("%d %d\n",X[stp]+1,Y[stp]+1);
dfs(lst[stp]);
}
int main()
{
int sum=0;
for (int i=0;i<4;i++)
for (int j=0;j<4;j++) L[i]|=1<<(i<<2)+j,R[j]|=1<<(i<<2)+j,sum|=(1<<(i<<2)+j)*readch();
lst[1]=-1;vis[sum]=1;q[til=1]=sum;
while (til!=hea)
{
int x=q[++hea];
for (int i=0;i<4;i++)
for (int j=0;j<4;j++)
{
int y=x^L[i]^R[j]^1<<(i<<2)+j;if (vis[y]) continue;
q[++til]=y;lst[til]=hea;X[til]=i;Y[til]=j;vis[y]=vis[x]+1;
if (!y) {printf("%d\n",vis[x]);dfs(til);return 0;}
}
}
return 0;
}