The question ： There are four ways to flip , Ask if you can make all the pieces into 0, Find the minimum number of steps .

Answer key ： Construct enumeration in turn to get the minimum value .

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
#include <ctime>
using namespace std;
const int inf=0x3f3f3f;
const int maxn=;
// Yes equ An equation ,var Variables . The number of rows of the augmented matrix is equ, The number of columns is var+1, Respectively 0 To var
int equ,var;
int a[maxn][maxn]; // Augmented matrix
int x[maxn]; // Solution set
int free_x[maxn];// To store free variables （ Multi solution enumeration of free variables can be used ）
int free_num;// The number of free variables
// The return value is -1 There is no solution , by 0 It's the only solution , Otherwise, the number of free variables is returned
int gauss()
{
int max_r,col,k;
free_num=;
for(k=,col=; k<equ&&col<var; k++,col++)
{
max_r=k;
for(int i=k+; i<equ; i++)
if(abs(a[i][col])>abs(a[max_r][col]))
max_r=i;
if(!a[max_r][col])
{
k--;
free_x[free_num++]=col;
continue;
}
if(max_r!=k)
for(int j=col; j<var+; j++)
swap(a[k][j],a[max_r][j]);
for(int i=k+; i<equ; i++)
{
if(a[i][col])
{
for(int j=col; j<var+; j++)
a[i][j]^=a[k][j];
}
}
}
for(int i=k; i<equ; i++)
if(a[i][col])
return -;
if(k<var) return var-k;
for(int i=var-; i>=; i--)
{
x[i]=a[i][var];
for(int j=i+; j<var; j++)
x[i]^=(a[i][j]&&x[j]);
}
return ;
}
int n,m;
// Be sure to distinguish the bottom left from the top right ！！！
void init1()
{
memset(a,,sizeof(a));
memset(x,,sizeof(x));
equ=n*m;
var=n*m;
for(int i=; i<n; i++)
for(int j=; j<m; j++)
{
int t=i*m+j;
a[t][t]=;
if(i>) a[(i-)*m+j][t]=; // On
if(i<(n-)) a[(i+)*m+j][t]=; // Next
if(j>) a[i*m+j-][t]=; // Left
if(j<(m-)) a[i*m+j+][t]=; // Right
}
}
void init2()
{
memset(a,,sizeof(a));
memset(x,,sizeof(x));
equ=n*m;
var=n*m;
for(int i=; i<n; i++)
for(int j=; j<m; j++)
{
int t=i*m+j;
a[t][t]=;
if(i>) a[(i-)*m+j][t]=; // On
//if(i<n-1) a[(i+1)*m+j][t]=1; // Next
if(j>) a[i*m+j-][t]=; // Left
if(j<(m-)) a[i*m+j+][t]=; // Right
if(i>&&j>) a[(i-)*m+j-][t]=; // Top left
//if(i>0&&j<(m-1)) a[(i-1)*m+j+1][t]=1; // The upper right
if(i<n-&&j>) a[(i+)*m+j-][t]=; // The lower left
//if(i<n-1&&j<m-1) a[(i+1)*m+j+1][t]=1; // The lower right
}
}
void init3()
{
memset(a,,sizeof(a));
memset(x,,sizeof(x));
equ=n*m;
var=n*m;
for(int i=; i<n; i++)
for(int j=; j<m; j++)
{
int t=i*m+j;
a[t][t]=;
if(i>) a[(i-)*m+j][t]=; // On
if(i<(n-)) a[(i+)*m+j][t]=; // Next
if(j>) a[i*m+j-][t]=; // Left
if(j<(m-)) a[i*m+j+][t]=; // Right
//if(i>0&&j>0) a[(i-1)*m+j-1][t]=1; // Top left
if(i>&&j<m-) a[(i-)*m+j+][t]=; // The upper right
//if(i<(n-1)&&j>0) a[(i+1)*m+j-1][t]=1; // The lower left
//if(i<n-1&&j<m-1) a[(i+1)*m+j+1][t]=1; // The lower right
}
}
void init4()
{
memset(a,,sizeof(a));
memset(x,,sizeof(x));
equ=n*m;
var=n*m;
for(int i=; i<n; i++)
for(int j=; j<m; j++)
{
int t=i*m+j;
a[t][t]=;
if(i>) a[(i-)*m+j][t]=; // On
if(i<(n-)) a[(i+)*m+j][t]=; // Next
if(j>) a[i*m+j-][t]=; // Left
//if(j<m-1) a[i*m+j+1][t]=1; // Right
//if(i>0&&j>0) a[(i-1)*m+j-1][t]=1; // Top left
if(i>&&j<m-) a[(i-)*m+j+][t]=; // The upper right
//if(i<(n-1)&&j>0) a[(i+1)*m+j-1][t]=1; // The lower left
if(i<(n-)&&j<(m-)) a[(i+)*m+j+][t]=; // The lower right
}
}
int solve()
{
int t=gauss();
if(t==-)
{
return inf;
}
else if(t==)
{
int ans=;
for(int i=; i<n*m; i++)
ans+=x[i];
return ans;
}
else
{
// Enumerate free variables
int ans=0x3f3f3f3f;
int tot=(<<t);
for(int i=; i<tot; i++)
{
int cnt=;
for(int j=; j<t; j++)
{
if(i&(<<j)) // Notice that it's not &&
{
x[free_x[j]]=;
cnt++;
}
else x[free_x[j]]=;
}
for(int j=var-t-; j>=; j--)
{
int idx;
for(idx=j; idx<var; idx++)
if(a[j][idx])
break;
x[idx]=a[j][var];
for(int l=idx+; l<var; l++)
if(a[j][l])
x[idx]^=x[l];
cnt+=x[idx];
}
ans=min(ans,cnt);
}
return ans;
}
}
char data[][];
int main()
{
while(scanf("%d%d",&n,&m)&&n&&m)
{
//getchar();
for(int i=; i<n; i++)
cin>>data[i];
init1();
for(int i=; i<n; i++)
for(int j=; j<m; j++)
if(data[i][j]=='') a[i*m+j][n*m]=;
int ans1=solve();
init2();
for(int i=; i<n; i++)
for(int j=; j<m; j++)
if(data[i][j]=='') a[i*m+j][n*m]=;
int ans2=solve();
init3();
for(int i=; i<n; i++)
for(int j=; j<m; j++)
if(data[i][j]=='') a[i*m+j][n*m]=;
int ans3=solve();
init4();
for(int i=; i<n; i++)
for(int j=; j<m; j++)
if(data[i][j]=='') a[i*m+j][n*m]=;
int ans4=solve();
int ans=min(min(ans1,ans2),min(ans3,ans4));
if(ans==inf) puts("Impossible");
else
{
if(ans==ans1) printf("1 %d\n",ans);
else if(ans==ans2) printf("2 %d\n",ans);
else if(ans==ans3) printf("3 %d\n",ans);
else if(ans==ans4) printf("4 %d\n",ans);
}
}
return ;
}

## NEFU 506&&ZOJ 3353 Chess Board ( Four kinds of constructed Gaussian elimination ) More articles about

1. 「ZOJ 1354」Extended Lights Out「 Gauss elimination 」

The question : Given a $$5\times 6$$ Of the chessboard $$01$$ state , Each operation can reverse itself from the four grid states around it , How to operate , Output one $$01$$ matrix Answer key : This can be done by enumerating the states of the first line and then leaving the recursion , ...

2. NEFU 505 Super red and black ( Gauss elimination )

Topic link Chinese questions , Change the template structure, you can pass , The data is a little bit watery , But you still need free argument enumeration . #include <iostream> #include <cstdio> #includ ...

3. NEFU 504 new Flip Game ( Gauss elimination )

Topic link Answer key : and  poj1753Filp game almost , The difference lies in t When group data is flipped, there is an additional upper left corner . #include <iostream> #include <cstdio ...

4. NEFU 503 Matrix solving ( Not 01 Gauss elimination of XOR )

Topic link Chinese questions , Gauss elimination template problem . #include <iostream> #include <cstdio> #include <cmath> #include ...

5. ZOJ 3645 BiliBili（ Gauss elimination ）

Shirai Kuroko is a Senior One student. Almost everyone in Academy City have super powers, and Kuroko ...

6. ZOJ 3645 BiliBili Gauss elimination difficulty :1

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4835 From the meaning of the title , Yes : (x1-x11)^2 + (x2-x12)^2 ... = ...

7. Four ways to exchange two variables （Java）

For the exchange of two variables , I found four ways , Next I use Java Let's demonstrate . 1. Use the third variable to exchange values , Simple method . ( Code demonstration ) class TestEV // Create a class { public static ...

8. Android Development of basic controls and detailed explanation of four layout

Android How to use and iOS The control is used in the same way , It's all event driven . There are also interface callbacks and proxy ways to add events to a control . Today's blog summarizes Android The basic controls and layout methods commonly used in . When it comes to layout ...

9. TCP Four kinds of timers

TCP There are four types of timers , Retransmission timer . Stick to the timer . Keep alive timer and time waiting timer  . The main features of these timers are as follows :      1. Retransmission timer        When TCP When sending a message segment , Create the retransmission timing of the specific message segment ...

## Random recommendation

1. ZeroMQ Interface function ：zmq_poll - I/O Multiplex technology

ZeroMQ Official address :http://api.zeromq.org/4-0:zmq_poll zmq_poll(3)    ØMQ Manual - ØMQ/4.1.0 Name zmq_poll  ...

2. A platform BUG, ok , Found a new way , The same effect

The source of the problem , I see people doing this , I just saw the video of this program running , Specifically, when you choose the mode of communication 1  COM ,  2  so : Ask for in combobox Choose from ,selectindex by 0 Time display COM The option to , by 1 ...

3. java Sum up the fourth // Common classes

6、 ... and . Common classes primary coverage :Object class .String class .Date class . Wrapper class ( One )Object class 1.Object Class is all Java The root parent of class 2. If it is not used in the declaration of a class extends Keyword indicates its parent class , be ...

4. java Sum up the second time （ Remaining content ）// Classes and objects 1

7. Member variables and local variables Member variables : Defined in class , Used to describe what the object will have local variable : Defined in a method of a class , Save temporary data in method difference : Different scopes The scope of a local variable is limited to the methods that define it The scope of a member variable is within the entire class ...

5. Use ASP.Net WebAPI structure REST service （ 5、 ... and ）—— client

WebAPI It's standard Http agreement , Support Http Protocol client ( Such as browser ) All accessible . however , Sometimes if we want to use it in our own programs WebAPI when , Now it's time to implement your own client . I've talked about it before .Net 4.5 in ...

6. powershell samples

1, Export to EXCEL $arr =New-Object System.Collections.ArrayList$i = 1 $pstablelist = @();$array =get-user ...