The Pilots Brothers' refrigerator
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 33842 | Accepted: 13107 | 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
/***
特殊解法,也就是'+'的行列元素7个操作一次,相当于'+'只操作一次
***/
#include <iostream>
#include <cmath>
#include <stdio.h>
using namespace std;
int map1[4][4];
bool vis[4][4];
int flip(int x,int y) {
for(int i=0;i<4;i++) {
vis[x][i]=!vis[x][i];
vis[i][y]=!vis[i][y];
}
vis[x][y]=!vis[x][y];
return 0;
}
int main()
{
char temp;
int c=0,pos[16][2];
for(int i=0;i<4;i++) {
for(int j=0;j<4;j++) {
cin>>temp;
if(temp=='-') {
map1[i][j]=0;
}
else map1[i][j]=1;
vis[i][j]=false;
}
}
for(int i=0;i<4;i++) {
for(int j=0;j<4;j++) {
if(map1[i][j]==1) {
//flip cow raw
flip(i,j);
}
}
}
for(int i=0;i<4;i++) {
for(int j=0;j<4;j++) {
if(vis[i][j]==true) {
pos[c][0]=i+1;
pos[c][1]=j+1;
c++;
}
}
}
cout<<c<<endl;
for(int i=0;i<c;i++) {
cout<<pos[i][0]<<" "<<pos[i][1]<<endl;
}
return 0;
}
/***
枚举+深搜dfs
***/
#include <iostream>
#include <cmath>
#include <stdio.h>
using namespace std;
int map1[4][4],ans=33;
bool vis[4][4];
int c=0,pos[16][2];
int pos_temp[16][2];
int flip(int s) {
int x=s/4;
int y=s%4;
for(int i=0;i<4;i++) {
map1[x][i] = (map1[x][i]==0?1:0);
map1[i][y] = (map1[i][y]==0?1:0);
}
map1[x][y]=(map1[x][y]==0?1:0);
return 0;
}
//panduan
bool isopen() {
for(int i=0;i<4;i++) {
for(int j=0;j<4;j++) {
if(map1[i][j]==1) return false;
}
}
return true;
}
//dfs
void dfs(int s,int c) {
//if(x>=4 || x<0 || y>=4 || y<0) return;
if(isopen()) {
if(ans>c) {
ans=c;
for(int i=1;i<=ans;i++) {
pos[i][0]=pos_temp[i][0];
pos[i][1]=pos_temp[i][1];
}
}
//cout<<"c= "<<c<<endl;
return;
}
if(s>=16) return;
dfs(s+1,c);
flip(s);
pos_temp[c+1][0]=s/4+1;
pos_temp[c+1][1]=s%4+1;
//flip
dfs(s+1,c+1);
//huisu
flip(s);
//Ëĸö·½Ïòfind
}
int main()
{
char temp;
for(int i=0;i<4;i++) {
for(int j=0;j<4;j++) {
cin>>temp;
if(temp=='-') {
map1[i][j]=0;
}
else map1[i][j]=1;
vis[i][i]=false;
}
}
dfs(0,0);
printf("%d\n",ans);
for(int i=1;i<=ans;i++) {
printf("%d %d\n",pos[i][0],pos[i][1]);
}
return 0;
}
/***
位运算和dfs
4x4开关映射为2^16种状态,32位无符号整数,高16位表示操作的开关index,地16位表示此操作
对应的状态
如0x0001111F,操作第一个开关,状态变为111F
***/