/*题意:
有一个4*4的正方形,每个格子上有一个符号,仅有“+”和“-”,要求将所有的符号都翻转成“-”。
规定:当翻转某个符号时,它所对应的行号和列号上符号都被翻转。
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
和poj 1753类似,给你一个由“+”和“-”构成的4*4矩阵,每动一次矩阵中的一个点,
它所在的行跟列的每个点都要取反,既“+”变“-”、“-”变“+”,现在输入一个这样的矩阵,
要求你求出要把它变成全“-”最少需要多少步,并且按顺序把要改变的点坐标输出*/
#include <stdio.h>
#include <string.h>
char map[4][4];
int book[4][4];
void change(int n)
{
int x=n/4;
int y=n%4;
int i;
for(i=0;i<4;i++)
{
map[i][y]=map[i][y]=='+'?'-':'+';
}
for(i=0;i<4;i++)
{
map[x][i]=map[x][i]=='+'?'-':'+';
}
map[x][y]=map[x][y]=='+'?'-':'+';
}
int judge()
{
int i,x,y;
for(i=0;i<16;i++)
{
x=i/4;
y=i%4;
if(map[x][y]!='-')
return 0;
}
return 1;
}
int dfs(int n,int i)
{
int j,x,y;
if(n==0)
{
if(judge())
return 1;
else
return 0;
}
for(j=i;j<16;j++)
{
x=j/4;
y=j%4;
change(j);
book[x][y]=1;
if(dfs(n-1,j+1))
return 1;
book[x][y]=0;
change(j);
}
return 0;
}
int main()
{
int flag;
memset(book,0,sizeof(book));
int i,j,k;
flag=0;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
scanf("%c",&map[i][j]);
}
getchar();
}
for(i=0;i<=16;i++)
{
if(dfs(i,0))
{
flag=1;
break;
}
}
if(flag)
{
printf("%d\n",i);
for(j=0;j<4;j++)
{
for(k=0;k<4;k++)
{
if(book[j][k])
printf("%d %d\n",j+1,k+1);
}
}
}
return 0;
}
The Pilots Brothers' refrigerator
最新推荐文章于 2021-02-22 23:03:54 发布