• poj 2965 The Pilots Brothers' refrigerator


    The Pilots Brothers' refrigerator
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 18040   Accepted: 6841   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 rowi 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


    与这个点击打开雷同,可先看这题。


    AC代码例如以下:

    #include<iostream>
    #include<cstdio>
    using namespace std;
    
    int n,a[5][5];
    char b[5][5];
    int h[17],z[17],H[17],Z[17],minn;
    
    int PD()
    {
        int i,j;
        for(i=0;i<4;i++)
            for(j=0;j<4;j++)
                if(a[i][j]!=0)
                return 0;
        return 1;
    }
    
    void dfs(int x,int y)
    {
       int i;
       for(i=0;i<4;i++)
            a[x][i]=!a[x][i];
       for(i=0;i<4;i++)
        a[i][y]=!a[i][y];
       a[x][y]=!a[x][y];
    }
    
    void work(int cur,int step)
    {
        int i;
        if(cur==16)
        {
            if(PD()&&step<minn)
                {
                    minn=step;
                    for(i=0;i<minn;i++)
                    {
                        H[i]=h[i];Z[i]=z[i];
                    }
                }
        }
        else{
            work(cur+1,step);
            dfs(cur/4,cur%4);
            h[step]=cur/4;z[step]=cur%4;
            work(cur+1,step+1);
            dfs(cur/4,cur%4);
        }
    
    }
    
    int main()
    {
        int i,j;
        for(i=0;i<4;i++)
            cin>>b[i];
        for(i=0;i<4;i++)
            for(j=0;j<4;j++)
            if(b[i][j]=='+')
            a[i][j]=1;
            else a[i][j]=0;
            minn=17;
        work(0,0);
        cout<<minn<<endl;
        for(i=0;i<minn;i++)
            cout<<H[i]+1<<" "<<Z[i]+1<<endl;
        return 0;
    }
    




  • 相关阅读:
    2017-4-25 winform公共控件属性 菜单和工具栏属性
    2017-4-24 winform窗体基础属性 ico图片生成 不规则窗体的移动 恶搞小程序
    2017-4-23 知识补充
    C# 动态方法和静态方法的区别 (转)
    2017-4-21 Ado.net字符串攻击 防御 实体类 数据访问类 属性扩展 三层架构开发
    ToString()用法 select top 子句
    2017-4-19 ado.net 增,删,改,查,练习
    2017-4-17 类库 通用变量 is和as运算符 委托
    2017-4-16 多态 构造函数 方法重载 静态方法和静态成员
    【2017-03-12】SQL Sever 子查询、聚合函数
  • 原文地址:https://www.cnblogs.com/llguanli/p/7252198.html
Copyright © 2020-2023  润新知