[POJ2965]The Pilots Brothers' refrigerator【枚举】

The Pilots Brothers’ refrigerator

Time Limit: 1000MS Memory Limit: 65536K

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

Northeastern Europe 2004, Western Subregion

题解

之前因为读入的事弄了我好久,最后觉得它的数据有问题,不能用手写的读入。
和1753这道题一样的想法,只不过翻转的方式不同罢了。
详见:http://blog.csdn.net/qq_41357771/article/details/79251885

代码如下:

#pragma GCC optimize(3)
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n=4,ans=1<<30,que[25],answer[25];
bool mp[10][10];
void change(int xx,int yy){
    mp[xx][yy]=!mp[xx][yy];
    for(int x=0;x^n;x++) mp[x][yy]=!mp[x][yy];
    for(int y=0;y^n;y++) mp[xx][y]=!mp[xx][y];
}
void check(int t){
    bool tt=1;
    for(int i=0;i^n;i++)
    for(int j=0;j^n;j++) if(!mp[i][j]){tt=0;break;}
    if(tt&&ans>t){ans=t;for(int i=0;i^ans;i++) answer[i]=que[i];}
}
void DFS(int x,int t){
    if(t>ans) return;
    if(x==16){check(t);return;}
    change(x/n,x%n);que[t]=x;DFS(x+1,t+1);
    change(x/n,x%n);DFS(x+1,t);
}
int main(){
    char c;
    for(int i=0;i<n;i++) 
    for(int j=0;j<n;j++){  
        cin>>c;  
        if(c=='-') mp[i][j]=1;else mp[i][j]=0;  
    }  
    DFS(0,0);
    printf("%d\n",ans);
    for(int i=0;i^ans;i++) printf("%d %d\n",answer[i]/n+1,answer[i]%n+1);
    return 0;
}
posted @ 2018-02-04 14:18  XSamsara  阅读(86)  评论(0编辑  收藏  举报