The Pilots Brothers' refrigerator
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.
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.
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.
-+-- ---- ---- -+--题意:这个题和那个翻棋子差不多,思路也一样,不过要输出路径,翻得时候是i行和j列,第(i,j)点应该翻奇数倍,具体的看代码。
#include<stdio.h>
#include<string.h>
char map1[5][5];
int a[1000][2],min1;
int b[1000][2],flag;
int min(int t1,int t2)
{
return t1<t2?t1:t2;
}
int check()
{
for(int i=0; i<4; i++)
for(int j=0; j<4; j++)
if(map1[i][j]=='+')
return 0;
return 1;
}
void turn(int x,int y)
{
for(int i=0; i<4; i++)
{
if(map1[x][i]=='-')
map1[x][i]='+';
else map1[x][i]='-';
if(map1[i][y]=='-')
map1[i][y]='+';
else map1[i][y]='-';
}
if(map1[x][y]=='+')
map1[x][y]='-';
else map1[x][y]='+';
}
void dfs(int x,int y,int step)
{
if(step>=min1)
return ;
if(check())
{
flag=1;
min1=step;
for(int i=0; i<step; i++)
for(int j=0; j<2; j++)
b[i][j]=a[i][j];
return ;
}
if(x>3)
return ;
turn(x,y);
if(y>=3)
{
a[step][0]=x;
a[step][1]=y;
dfs(x+1,0,step+1);
}
else
{
a[step][0]=x;
a[step][1]=y;
dfs(x,y+1,step+1);
}
turn(x,y);
if(y>=3)
dfs(x+1,0,step);
else dfs(x,y+1,step);
}
int main()
{
while(~scanf("%s",map1[0]))
{
flag=0;
min1=1000;
for(int i=1; i<4; i++)
scanf("%s",map1[i]);
dfs(0,0,0);
if(flag)
{
printf("%d\n",min1);
for(int i=0; i<min1; i++)
printf("%d %d\n",b[i][0]+1,b[i][1]+1);
}
}
return 0;
}