Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 24341 | Accepted: 9395 | Special Judge |
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
Source
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<string>
#include<ctype.h>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<vector>
#include<algorithm>
#include<iostream>
#include<functional>
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const double EPS=1e-8;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
char str[5];
int state=0;
int main(){
for(int i=0;i<4;++i){
scanf("%s",str);
for(int j=0;j<4;++j){
if(str[j]=='-') state|=1<<(i*4+j);
}
}
int ans=INF;
int MinState;
for(int i=0;i<(1<<16);++i){
int t=state;
int num=0;
for(int j=0;j<16;++j){
if(i&(1<<j)){
num+=1;
if(num==ans) break;
int raw=j/4,col=j%4;
for(int k=0;k<4;++k){
t^=1<<(raw*4+k);
t^=1<<(k*4+col);
}
t^=1<<j;
}
}
if(t==(1<<16)-1){
ans=min(ans,num);
MinState=i;
if(ans==1) break;
}
}
printf("%d\n",ans);
for(int i=0;i<16;++i){
if(MinState&(1<<i)) printf("%d %d\n",i/4+1,i%4+1);
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<string>
#include<ctype.h>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<vector>
#include<algorithm>
#include<iostream>
#include<functional>
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const double EPS=1e-8;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
int chessboard[4][5];
int path[16][2];
void turn(int row,int col,int now){
path[now][0]=row+1;
path[now][1]=col+1;
chessboard[row][col]^=1;
for(int i=0;i<4;++i){
chessboard[row][i]^=1;
chessboard[i][col]^=1;
}
}
bool dfs(int row,int col,int n,int now){
if(now==n){
for(int i=0;i<4;++i){
for(int j=0;j<4;++j){
if(chessboard[i][j]==0) return false;
}
}
return true;
}
turn(row,col,now);
for(int i=row*4+col+1;i<=16-(n-now-1);++i){
if(dfs(i/4,i%4,n,now+1)) return true;
}
turn(row,col,now);
return false;
}
int main(){
char str[5];
for(int i=0;i<4;++i){
scanf("%s",str);
for(int j=0;j<4;++j){
if(str[j]=='-') chessboard[i][j]=1;
}
}
int flag=0;
for(int i=0;i<=16;++i){
for(int j=0;j<=16-i;++j){
if(dfs(j/4,j%4,i,0)){
printf("%d\n",i);
for(int j=0;j<i;++j) printf("%d %d\n",path[j][0],path[j][1]);
flag=1;
break;
}
}
if(flag) break;
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<string>
#include<ctype.h>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<vector>
#include<algorithm>
#include<iostream>
#include<functional>
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const double EPS=1e-8;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
int open[4][4];
int path[16][2];
int main(){
char str[5];
for(int i=0;i<4;++i){
scanf("%s",str);
for(int j=0;j<4;++j){
if(str[j]=='+'){
open[i][j]-=1;
for(int k=0;k<4;++k){
open[i][k]+=1;
open[k][j]+=1;
}
}
}
}
int num=0;
for(int i=0;i<4;++i){
for(int j=0;j<4;++j){
if(open[i][j]%2){
path[num][0]=i+1;
path[num++][1]=j+1;
}
}
}
printf("%d\n",num);
for(int i=0;i<num;++i){
printf("%d %d\n",path[i][0],path[i][1]);
}
return 0;
}
第四种方法:高斯消元,推荐使用这种方法,也是解决开关问题普遍使用的一种方法。前两种方法在n过大时容易超时,而第三种方法不适用于所有开关问题,如POJ 1753。
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<string>
#include<ctype.h>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<vector>
#include<algorithm>
#include<iostream>
#include<functional>
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const double EPS=1e-8;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
int A[16][17];
int Gauss(){
for(int i=0;i<16;++i){
int j=i;
for(;j<16;++j){
if(A[j][i]) break;
}
for(int k=0;k<17;++k) swap(A[i][k],A[j][k]);
for(int j=0;j<16;++j){
if(j!=i && A[j][i]){
for(int k=0;k<17;++k) A[j][k]^=A[i][k];
}
}
}
int sum=0;
for(int i=0;i<16;++i){
if(A[i][16]) ++sum;
}
return sum;
}
int main(){
for(int i=0;i<4;++i){
for(int j=0;j<4;++j){
A[i*4+j][i*4+j]^=1;
for(int k=0;k<4;++k){
A[i*4+j][i*4+k]^=1;
A[i*4+j][k*4+j]^=1;
}
}
}
char str[5];
for(int i=0;i<4;++i){
scanf("%s",str);
for(int j=0;j<4;++j){
if(str[j]=='+') A[i*4+j][16]=1;
}
}
printf("%d\n",Gauss());
for(int i=0;i<16;++i){
if(A[i][16]) printf("%d %d\n",i/4+1,i%4+1);
}
return 0;
}