• POJ 2965 The Pilots Brothers' refrigerator


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

    Source

    Northeastern Europe 2004, Western Subregion
     
     
     
    解析:状态压缩。本题与POJ 1753 Flip Game大致相同,只不过翻转的规则改为了将那一行和那一列进行翻转。
     
     
     
    //从上到下,从左到右,每个把手翻转应该异或的值
    void init()
    {
        for(int i = 0; i < 4; ++i){
            for(int j = 0; j < 4; ++j){
                int tmp = 0;
                tmp ^= 1<<(i*4+j);
                for(int k = 0; k < 4; ++k)
                    tmp ^= 1<<(i*4+k);
                for(int k = 0; k < 4; ++k)
                    tmp ^= 1<<(k*4+j);
                printf("%d ", tmp);
            }
            printf("
    ");
        }
    }
    

    预处理得到change[]。

     
     
     
    #include <cstdio>
    #include <vector>
    using namespace std;
    
    const int INF = 0x7fffffff;
    char s[20];
    int change[] = {4383,8751,17487,34959,4593,8946,17652,35064,7953,12066,20292,36744,61713,61986,62532,63624};
    int pos[16] = {1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768};
    
    
    void solve()
    {
        int init_state = 0;
        for(int i = 0; i < 16; ++i){
            if(s[i] == '+')
                init_state += pos[i];
        }
        vector<pair<int, int> > resv, v;
        int res = INF;
        for(int i = 0; i < 65536; ++i){
            int state = init_state;
            for(int j = 0; j < 16; ++j){
                if(i&pos[j]){
                    state ^= change[j];
                    v.push_back(make_pair(j/4+1, j%4+1));
                }
            }
            if(state == 0 && (int)v.size() < res){
                res = v.size();
                resv = v;
            }
            v.clear();
        }
        printf("%d
    ", res);
        for(int i = 0; i < res; ++i)
            printf("%d %d
    ", resv[i].first, resv[i].second);
    }
    
    int main()
    {
        for(int i = 0; i < 4; ++i)
            scanf("%s", &s[i*4]);
        solve();
        return 0;
    }
    

      

    由于本题的特殊性,提供一个更高效的解法,参考"高效解法,供路人借鉴~"。

    #include <cstdio>
    #include <cstring>
    
    char s[4][5];
    bool mark[4][4];
    
    void solve()
    {
        memset(mark, 0, sizeof(mark));
        for(int i = 0; i < 4; ++i){
            for(int j = 0; j < 4; ++j){
                if(s[i][j] == '+'){
                    mark[i][j] = !mark[i][j];
                    for(int k = 0; k < 4; ++k){
                        mark[i][k] = !mark[i][k];
                        mark[k][j] = !mark[k][j];
                    }
                }
            }
        }
        int cnt = 0;
        int a[16], b[16];
        for(int i = 0; i < 4; ++i){
            for(int j = 0; j < 4; ++j){
                if(mark[i][j]){
                    a[cnt] = i+1;
                    b[cnt++] = j+1;
                }
            }
        }
        printf("%d
    ", cnt);
        for(int i = 0; i < cnt; ++i)
            printf("%d %d
    ", a[i], b[i]);
    }
    
    int main()
    {
        for(int i = 0; i < 4; ++i)
            scanf("%s", s[i]);
        solve();
        return 0;
    }
    

      

  • 相关阅读:
    java-集合框架-泛型2-泛型限定
    进程间通信
    多进程编程基础概念
    linux deb 打包流程
    linux RPM 打包流程
    Python 第一個程序
    从注册验证码入手,我保住了30%的流失用户
    为什么Web端登录需要验证码?
    网络验证码的进化:从简单图文到无感验证
    公开课 | 金融知识图谱的应用探索
  • 原文地址:https://www.cnblogs.com/inmoonlight/p/5791166.html
Copyright © 2020-2023  润新知