poj 2965 The Pilots Brothers' refrigerator

来源:互联网 发布:云计算平台storm 编辑:程序博客网 时间:2024/06/08 08:09
The Pilots Brothers' refrigerator
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 20973 Accepted: 8109 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<cstdio>#include<string>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>#include<climits>#include<queue>#include<vector>#include<map>#include<sstream>#include<set>#include<stack>#include<utility>#pragma comment(linker, "/STACK:102400000,102400000")#define PI 3.1415926535897932384626#define eps 1e-10#define sqr(x) ((x)*(x))#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)#define  lson   num<<1,le,mid#define rson    num<<1|1,mid+1,ri#define MID   int mid=(le+ri)>>1#define zero(x)((x>0? x:-x)<1e-15)#define mp    make_pair#define _f     first#define _s     secondusing namespace std;const int INF =0x3f3f3f3f;//const int maxn=    ;//const int maxm=    ;//const int INF=    ;typedef long long ll;const ll inf =1000000000000000;//1e15;//ifstream fin("input.txt");//ofstream fout("output.txt");//fin.close();//fout.close();//freopen("a.in","r",stdin);//freopen("a.out","w",stdout);//by yskysker123

const int n=4,m=4;int a[6][6];bool flag;int ans;vector<int>ve;bool check(){    for(int i=1;i<=4;i++)    {        for(int j=1;j<=4;j++)        {            if(a[i][j])  return false;        }    }    return true;}void print(int id){    int y=(id-1)/4+1;    int x=  id%4 ==0? 4 :id%4;    printf("%d  %d\n",y,x);}/*void out(){     for(int i=1;i<=4;i++)    {        for(int j=1;j<=4;j++)        {           cout<<a[i][j];        }         cout<<endl;    }}*/void dfs(int id,int deep,int step){     if(step==deep)                             {       if( flag=check()  )    ans=deep;   //if()里的返回值就是右边的值,ans一开始开成了bool型,输出总是为1,弄了半天才发现,真是晕。        return;    }    if(flag ||  id==17)  return ;    int y=(id-1)/4 +1;    int x=   id%4 ==0? 4 :id%4;    for(int i=1;i<=4;i++)    {        a[y][i]^=1;    }    for(int i=1;i<=4;i++)    {        if(i==y)  continue;        a[i][x]^=1;    }//    cout<<"after"<<endl;//    out();     ve.push_back(id);     dfs(id+1,deep,step+1);     if(flag ||  id==17)  return ;  //我这种做法不可或缺的一步    ve.pop_back();     for(int i=1;i<=4;i++)    {        a[y][i]^=1;    }    for(int i=1;i<=4;i++)    {        if(i==y)  continue;        a[i][x]^=1;    }     dfs(id+1,deep,step);}int main(){    char ch;      ve.clear();      flag=0;        FOR1(i,4)        {            FOR1(j,4)            {            scanf(" %c",&ch);                a[i][j]= ch=='+'?1:0  ;            }        }        int i;        for( i=0;i<=16&&!flag;i++)  //这里好多次调试之后一直为6,这是wa了很多次的重要原因。        {//            cout<<i<<endl;            dfs(   1 ,i,0);        }//        cout<<flag<<endl;        printf("%d\n",ans);        for(int i=0;i<ans;i++)            print(ve[i]);    return 0;}


附上网上看到的:
http://www.cnblogs.com/stdio/archive/2012/05/29/2524038.html   贪心神解

我也学着写了个:

#include <iostream>#include <cstdio>#include <cstring>using namespace std;char a[6][6];int cnt[6][6];const int n=4,m=4;void update(int y,int x){    for(int i=1;i<=4;i++)    {        cnt[y][i]++;        cnt[i][x]++;    }    cnt[y][x]--;}int main(){    memset(cnt,0,sizeof cnt);    for(int i=1;i<=n;i++)    {        for(int j=1;j<=m;j++)        {             scanf("  %c",&a[i][j]);             if(a[i][j]=='+')               update(i,j);        }    }    int ans=0;     for(int i=1;i<=4;i++)    {        for(int j=1;j<=4;j++)        {            ans+=cnt[i][j]%2;        }    }    cout<<ans<<endl;    for(int i=1;i<=4;i++)    {        for(int j=1;j<=4;j++)        {            if(cnt[i][j]%2)                cout<<i<<" "<<j<<endl;        }    }    return 0;}




0 0