这题用的是暴力枚举第一排的情况,然后出解,听discuss里说能用高斯消去发,有空再试
#include <stdio.h>
#include <string.h>
int n;
int data[10][10], t[10][10], da[10][10];
int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
void turn(int i, int j)
{
t[i][j]=!t[i][j];
int x, y;
for ( int k = 0; k < 4; k++ )
{
y=i+dir[k][0];
x=j+dir[k][1];
if ( 0 <= x && x < 6 && 0 <= y && y < 5) t[y][x]=!t[y][x];
}
}
int work()
{
int i, j;
for ( i = 0; i < 5; i++ )
for ( j = 0; j <6; j++ )
t[i][j]=data[i][j];
for ( j = 0; j < 6; j++ )
if ( da[0][j] ) turn(0, j);
for ( i = 1; i < 5; i++ )
{
for ( j = 0; j <6; j++ )
{
if (t[i-1][j])
{
turn(i, j);
da[i][j]=1;
}
}
}
for ( i = 0; i < 5; i++ )
for ( j = 0; j < 6; j++ )
if ( t[i][j] ) return 0;
return 1;
}
void print()
{
for ( int i = 0; i < 5; i++ )
{
printf("%d", da[i][0]);
for ( int j = 1; j < 6; j++ )
printf(" %d", da[i][j]);
putchar(10);
}
}
int main()
{
scanf("%d", &n);
int m, i, j, k, num;
for ( m = 1; m <= n; m++ )
{
for ( i = 0; i < 5 ; i++ )
for ( j = 0; j < 6 ; j++ )
scanf("%d", data[i]+j);
printf("PUZZLE #%d\n", m);
for ( k = 0; k < 64; k++ )
{
num= k;
memset(da, 0, sizeof(da));
for ( j = 0; j < 6 && num; j++ )
{
da[0][j]=num&1;
num=num>>1;
}
if (1 == work())
print();
}
}
return 0;
}