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 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
题意:冰箱门上有16个开关,当开关同时为开的时候,门才能打开, - 为开,+为关;
当改变一个开关的时候,它所在行和列的开关都改变,问至少几步,把每一步输出;
思路:1.枚举+dfs;
2.当把i行和j列除了s[i][j]的开关都改变一次,那么除了s[i][j]会改变,其他的都不改变;
所以,一开始就把所有关的开关翻转过来,翻转之后开关为关的位置都是需要翻转的;
1.枚举+dfs
这个时间复杂度比较高,如果用字符来判断的话会超时;
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int s[10][10];
int count;
bool flag;
struct node
{
int x,y;
};
node f[20];
void is()
{
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
if(!s[i][j])
{
flag=false;
return;
}
}
}
flag=true;
}
void change(int x,int y)
{
s[x][y]=(s[x][y]+1)%2;
for(int i=0; i<4; i++)
{
s[x][i]=(s[x][i]+1)%2;
}
for(int i=0; i<4; i++)
{
s[i][y]=(s[i][y]+1)%2;
}
}
void bfs(int x,int y,int n)
{
if(n==count)
{
is();
return;
}
if(x>=4||flag) return;
change(x,y);
f[n].x=x;
f[n].y=y;
if(y==3)
{
bfs(x+1,0,n+1);
change(x,y);
bfs(x+1,0,n);
}
else
{
bfs(x,y+1,n+1);
change(x,y);
bfs(x,y+1,n);
}
}
int main()
{
char ss[10][10];
while(scanf("%s",ss[0])!=EOF)
{
flag=false;
for(int i=1; i<4; i++) scanf("%s",ss[i]);
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
if(ss[i][j]=='-') s[i][j]=1;
else s[i][j]=0;
}
}
for(int i=0; i<=16; i++)
{
count=i;
bfs(0,0,0);
if(flag) break;
}
printf("%d\n",count);
for(int i=0; i<count; i++)
{
printf("%d %d\n",(f[i].x+1),(f[i].y+1));
}
}
return 0;
}
2.技巧
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char s[10][10];
int f[10][10],ans;
int main()
{
while(scanf("%s",&s[0])!=EOF)
{
memset(f,0,sizeof(f));
ans=0;
for(int i=1; i<4; i++) scanf("%s",&s[i]);
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
if(s[i][j]=='+')
{
f[i][j]=(f[i][j]+1)%2;
for(int a=0; a<4; a++)
{
f[a][j]=(f[a][j]+1)%2;
}
for(int a=0; a<4; a++)
{
f[i][a]=(f[i][a]+1)%2;
}
}
}
}
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
if(f[i][j]) ans++;
}
}
printf("%d\n",ans);
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
if(f[i][j])
{
printf("%d %d\n",(i+1),(j+1));
}
}
}
}
return 0;
}