POJ 1222 EXTENDED LIGHTS OUT(反转)

EXTENDED LIGHTS OUT
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 12616   Accepted: 8002

Description

In an extended version of the game Lights Out, is a puzzle with 5 rows of 6 buttons each (the actual puzzle has 5 rows of 5 buttons each). Each button has a light. When a button is pressed, that button and each of its (up to four) neighbors above, below, right and left, has the state of its light reversed. (If on, the light is turned off; if off, the light is turned on.) Buttons in the corners change the state of 3 buttons; buttons on an edge change the state of 4 buttons and other buttons change the state of 5. For example, if the buttons marked X on the left below were to be pressed,the display would change to the image on the right. 
POJ 1222 EXTENDED LIGHTS OUT(反转)
The aim of the game is, starting from any initial set of lights on in the display, to press buttons to get the display to a state where all lights are off. When adjacent buttons are pressed, the action of one button can undo the effect of another. For instance, in the display below, pressing buttons marked X in the left display results in the right display.Note that the buttons in row 2 column 3 and row 2 column 5 both change the state of the button in row 2 column 4,so that, in the end, its state is unchanged. 
POJ 1222 EXTENDED LIGHTS OUT(反转)
Note: 
1. It does not matter what order the buttons are pressed. 
2. If a button is pressed a second time, it exactly cancels the effect of the first press, so no button ever need be pressed more than once. 
3. As illustrated in the second diagram, all the lights in the first row may be turned off, by pressing the corresponding buttons in the second row. By repeating this process in each row, all the lights in the first 
four rows may be turned out. Similarly, by pressing buttons in columns 2, 3 ?, all lights in the first 5 columns may be turned off. 
Write a program to solve the puzzle.

Input

The first line of the input is a positive integer n which is the number of puzzles that follow. Each puzzle will be five lines, each of which has six 0 or 1 separated by one or more spaces. A 0 indicates that the light is off, while a 1 indicates that the light is on initially.

Output

For each puzzle, the output consists of a line with the string: "PUZZLE #m", where m is the index of the puzzle in the input file. Following that line, is a puzzle-like display (in the same format as the input) . In this case, 1's indicate buttons that must be pressed to solve the puzzle, while 0 indicate buttons, which are not pressed. There should be exactly one space between each 0 or 1 in the output puzzle-like display.

Sample Input

2
0 1 1 0 1 0
1 0 0 1 1 1
0 0 1 0 0 1
1 0 0 1 0 1
0 1 1 1 0 0
0 0 1 0 1 0
1 0 1 0 1 1
0 0 1 0 1 1
1 0 1 1 0 0
0 1 0 1 0 0

Sample Output

PUZZLE #1
1 0 1 0 0 1
1 1 0 1 0 1
0 0 1 0 1 1
1 0 0 1 0 0
0 1 0 0 0 0
PUZZLE #2
1 0 0 1 1 1
1 1 0 0 0 0
0 0 0 1 0 0
1 1 0 1 0 1
1 0 1 1 0 1

Source

 

题意分析

给出t组数据,每组数据为一个5×6的矩阵,矩阵中元素为0或1。在矩阵中选择每次点击一个点,可以将其相邻的(上下左右)的元素异或,即1变为0,变为1。求如何点击才能使得矩阵全部变为0。 
输出一个5×6的矩阵,其中1表示点击该位置,0表示不点击。

 #include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#define inf 0x3f3f3f3f
using namespace std;
int a[][][];
int d[][] = { {,},{,-},{-,},{,},{,} };
int b[][], c[][];
int map[][]; int get(int x, int y)//获得周围的转了几次+原本的数
{
int i;
int s = map[x][y];
for (i = ; i < ; i++)
{
int xx = x + d[i][];
int yy = y + d[i][];
if (xx < || y < || x >= || y >= ) continue;
s += b[xx][yy];
}
return s % ;
} int cal()//返回总共需要转几次
{
int i, j;
int res = ;
for (i = ; i < ; i++)
{
for (j = ; j < ; j++)
{
if (get(i - , j) == )//如果返回的是奇数,说明要通过i层来改变i-1层
{
res++;
b[i][j] = ;
}
}
}
for (j = ; j < ; j++)
{
if (get(, j) == ) return -;
}
return res;
} int solve(int t)
{
int i,j;
int ans = inf;
for (i = ; i < << ; i++)//二进制枚举这一层第一排的每一种情况
{
memset(b, , sizeof(b));
for (j = ; j < ; j++)
{
b[][j] = i >> j & ;
}
int res = cal();
if (res != - && ans > res)
{
ans =res;
memcpy(c, b, sizeof(b));
}
}
printf("PUZZLE #%d\n", t);
for (int i = ; i<; i++)
for (int j = ; j<; j++)
printf("%d%c", c[i][j], j == ? '\n' : ' ');
} int main()
{
int n;
scanf("%d", &n);
int i,j,k;
for (i = ; i <=n; i++)
for (j = ; j < ; j++)
for (k = ; k < ; k++)
scanf("%d", &a[i][j][k]);
for (i = ; i <= n; i++)//一层层来解决
{
for (j = ; j < ; j++)
for (k = ; k < ; k++)
map[j][k] = a[i][j][k];
solve(i);
}
}

高斯消元的

 #include <iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn=;
int a[maxn][maxn+],x[maxn];//a 是系数矩阵和增广矩阵,x 是最后存放的解
// a[][maxn]中存放的是方程右面的值(bi)
int equ,var;//equ 是系数阵的行数,var 是系数矩阵的列数(变量的个数)
int free_num,ans=; int abs1(int num) //取绝对值
{
if (num>=) return num;
else
return -*num;
}
void Debug(void)
{
int i, j;
for (i = ; i < equ; i++)
{
for (j = ; j < var + ; j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
//调试输出,看消元后的矩阵值,提交时,就不用了
inline int gcd(int a, int b) //最大公约数
{
int t;
while (b != )
{
t = b;
b = a % b;
a = t;
}
return a;
} inline int lcm(int a, int b) //最小公倍数
{
return a * b / gcd(a, b);
} int dfs(int p) //枚举*解,只能取0-1,枚举完就回带,找到最小的
{
if (p<=free_num-) //深入到了主对角线元素非0 的行了
{
//下面就是回带的代码啊
for(int i = free_num-; i >= ; --i)
{
int tmp = a[i][var] % ;
for(int j = i+; j < var; ++j) //x[i]取决于x[i+1]--x[var]啊,所以后面的解对前面的解有影
//响。
if(a[i][j] != )
tmp = ( tmp - (a[i][j]*x[j])% + ) % ;
x[i] = (tmp/a[i][i]) % ; //上面的正常解
} //回带完成了
//计算解元素为1 的个数;
int sum=;
for(int i=; i<var; i++) sum+=x[i];
if (ans>sum) ans=sum;
return ;
}
x[p]=;
dfs(p-);
x[p]=;
dfs(p-);
}
void swap(int &a,int &b)
{
int temp=a; //交换 2 个数
a=b;
b=temp;
}
int Gauss()
{
int k,col = ;
//当前处理的列
for(k = ; k < equ && col < var; ++k,++col)
{
int max_r = k;
for(int i = k+; i < equ; ++i)
if(a[i][col] > a[max_r][col])
max_r = i;
if(max_r != k)
{
for(int i = k; i < var + ; ++i)
swap(a[k][i],a[max_r][i]);
}
if(a[k][col] == )
{
k--;
continue;
}
for(int i = k+; i < equ; ++i)
{
if(a[i][col] != )
{
int LCM = lcm(a[i][col],a[k][col]);
int ta = LCM/a[i][col], tb = LCM/a[k][col];
if(a[i][col]*a[k][col] < )
tb = -tb;
for(int j = col; j < var + ; ++j)
a[i][j] = ( (a[i][j]*ta)% - (a[k][j]*tb)% + ) % ;
// 0 和 1 两种状态
}
}
}
//a[i][j]只有 //上述代码是消元的过程,行消元完成
//解下来 2 行,判断是否无解
//注意 K 的值,k 代表系数矩阵值都为 0 的那些行的第 1 行
for(int i = k; i < equ; ++i)
if(a[i][col] != )
return -;
//Debug(); //唯一解或者无穷解,k<=var
//var-k==0 唯一解;var-k>0 无穷多解,*解的个数=var-k
//能执行到这,说明肯定有解了,无非是 1 个和无穷解的问题。
//下面这几行很重要,保证秩内每行主元非 0,且按对角线顺序排列,就是检查列
for(int i = ; i <equ; ++i)//每一行主元素化为非零
if(!a[i][i])
{
int j;
for(j = i+; j<var; ++j)
if(a[i][j])
break;
if(j == var)
break;
for(int k = ; k < equ; ++k)
swap(a[k][i],a[k][j]);
}
// ----处理保证对角线主元非 0 且顺序,检查列完成 free_num=k;
if (var-k>)
{
dfs(var-);
return ans;
//无穷多解,先枚举解,然后用下面的回带代码进行回带;
//这里省略了下面的回带的代码;不管唯一解和无穷解都可以回带,只不过无穷解
//回带时,默认为最后几个*变元=0 而已。
}
if(var-k<)
return -;
// 无解返回 -1
if (var-k==)//唯一解时
{
//下面是回带求解代码,当无穷多解时,最后几行为 0 的解默认为 0;
for(int i = k-; i >= ; --i) //从消完元矩阵的主对角线非 0 的最后 1 行,开始往
//回带
{
int tmp = a[i][var] % ; for(int j = i+; j < var; ++j) //x[i]取决于 x[i+1]--x[var]啊,所以后面的解对前面的解有影响。
if(a[i][j] != )
tmp = ( tmp - (a[i][j]*x[j])% + ) % ;
//if (a[i][i]==0) x[i]=tmp;//最后的空行时,即无穷解得
//else
x[i] = (tmp/a[i][i]) % ; //上面的正常解
}
int sum=;
for(int i=; i<var; i++)
sum+=x[i];
return sum; //回带结束了
}
} int main(void)
{
// freopen("Input.txt", "r", stdin);
int i, j,t,t1;
cin>>t;
t1=t;
equ=;
var=;
while (t--)
{
memset(a, , sizeof(a));
memset(x, , sizeof(x));
//memset(free_x, 1, sizeof(free_x)); // 一开始全是不确定的变元.
//下面要根据位置计算a[i][j];
for (i = ; i < ; i++)
{
for (j = ; j < ; j++)
{
/* for(int k=0;k<4;k++)
{
int ni=i+di[k];
int nj=j+dj[k];
if(inlim(ni,nj))
{
a[i*6+j][ni*6+nj]=1;
}
}
*/
if (i->=) a[i*+j][(i-)*+j]=; //计算上面的位置
if (i+<=) a[i*+j][(i+)*+j]=;//计算下面的位置
if (j->=) a[i*+j][i*+j-]=;//计算左面的位置
if (j+<=) a[i*+j][i*+j+]=; //计算右面的位置
a[i*+j][i*+j]=;//别忘了计算自己
cin>>a[i*+j][];
//scanf("%d", &a[i][j]);
}
}
//Debug();
//free_num = Gauss();
free_num=Gauss();
if (free_num == -) printf("无解!\n");
else if (free_num >= )
{
int na_num=;
printf("PUZZLE #%d\n",t1-t);
for (i = ; i < var; i++)
{
na_num++;
if (na_num%==)
{
printf("%d\n",x[i]);
}
else
printf("%d ",x[i]);
}
}
// printf("\n");
}
return ;
}
上一篇:第四篇、CSS选择器


下一篇:poj2686-Traveling by Stagecoach(状压dp)