POJ-2965-ThePilotsBrothers'refrigerator

来源:互联网 发布:天盾微信数据恢复工具 编辑:程序博客网 时间:2024/06/16 07:02
The Pilots Brothers' refrigerator
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 23718 Accepted: 9141 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

61 11 31 44 14 34 4

Source

Northeastern Europe 2004, Western Subregion


#include<iostream>#include<algorithm>#include<cstdio>#include<cmath>#include<cstring>using namespace std;bool Map[4][4];bool flag = false;int step, top = 0;struct node{    int x, y;}xt[20];void fold(int x, int y){    for(int i = 0; i<4; ++i)    {        Map[x][i] = !Map[x][i];        Map[i][y]= !Map[i][y];    }    Map[x][y] = !Map[x][y];}int Make_sure(){    for(int i = 0; i<4; ++i)        for(int j = 0; j<4; ++j)        if(!Map[i][j])return false;    return true;}void dfs(int x, int y, int s){    if(s==step)    {        flag = Make_sure();        return;    }    if(y==4)    {        ++x;        y = 0;    }    if(x==4||flag)return;    fold(x,y);    xt[top].x = x;    xt[top].y = y;    ++top;    dfs(x,y+1,s+1);    fold(x,y);    if(flag)return;    --top;    dfs(x,y+1, s);}int main(){    char s[6];    for(int i = 0; i<4; ++i)    {        scanf("%s", s);        for(int j = 0; j<4; ++j)        {            if(s[j]=='+')Map[i][j] = 0;            else Map[i][j] = 1;        }    }    for(step = 0; step<=16;++step)    {        dfs(0,0,0);        if(flag)break;    }    printf("%d\n", step);    for(int i = 0; i<top; ++i)        printf("%d %d\n",xt[i].x+1, xt[i].y+1);    return 0;}


0 0
原创粉丝点击