zoukankan      html  css  js  c++  java
  • POJ 2965, The Pilots Brothers' refrigerator

    POJ 2965, The Pilots Brothers' refrigerator
    POJ 1753, Flip Game

    这两道题类似,可转化图论的最短路径问题:
    顶点(Vertex)数:65536
    边(Edge)数:65536 * 16 / 2
    边的权值(Weight):1
    对于给定两个点, 求最短路径:
    由于边的权值均为1, 所以适宜采用BFS + Mark Table, 时间复杂度为O(N), N为顶点数。


    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


    // POJ1753.cpp : Defines the entry point for the console application.
    //

    #include 
    <algorithm>
    #include 
    <iostream>
    #include 
    <sstream>
    using namespace std;

    template
    <typename Target, typename Source>
    Target lexical_cast(Source arg){
        std::stringstream interpreter;
        Target result;
        interpreter
    <<arg;
        interpreter
    >>result;
        
    return result;
    }

    void minSwitchs(int cposition)
    {
        
    if (cposition == 0)
        {
            cout 
    << "0\n";
            
    return;
        }

        
    const unsigned short pattern[16= {0xf888,0xf444,0xf222,0xf111,0x8f88,0x4f44,0x2f22,0x1f11,
                                            
    0x88f8,0x44f4,0x22f2,0x11f1,0x888f,0x444f,0x222f,0x111f };
        
    const int MAX = 65536;

        
    char cnt[MAX];
        unsigned 
    short door[MAX];
        unsigned 
    short queuen[MAX];
        memset(cnt,
    -1,sizeof(cnt));
        cnt[cposition] 
    = 0;

        queuen[
    0= cposition;
        
    int front = -1;
        
    int    rear = 0;

        unsigned 
    short cpos = 0;
        unsigned 
    short npos = 0;
        
    while (front != rear)
        {
            
    if(++front > 65535)front %= 65536;
            cpos 
    = queuen[front];

            
    for (int i = 0; i < 16++i) 
            {
                npos 
    = cpos ^ pattern[i];;
                
    if (cnt[npos] == -1)
                {
                    
    if(++rear > 65535)rear %= 65536;
                    queuen[rear] 
    = npos;
                    cnt[npos] 
    = cnt[cpos] + 1;
                    door[npos] 
    = i;
                }
                
    if (npos == 0)
                {
                    
    int ppos = npos;

                    
    string ret = "";
                    
    int steps = cnt[npos];
                    
    while (ppos != cposition)
                    {
                        ret 
    += "\n";
                        ret 
    += ((door[ppos]&3+ 1+ '0';
                        ret 
    += " ";
                        ret 
    += ((door[ppos]>>2+ 1+ '0';
                        ppos 
    = ppos ^ pattern[door[ppos]];
                    }
                    std::reverse(ret.begin(), ret.end());
                    ret 
    = lexical_cast<stringint>(steps) + "\n" + ret;
                    cout 
    << ret;
                    
    return;
                }
            }
        }
        cout 
    << "-1";
    }


    int main(int argc, char* argv[])
    {
        
    int cposition = 0;
        
    char s[5];
        s[
    4]='\0';
        
    for(int i=1;i<=4;i++)
        {
            scanf(
    "%s",s);
            
    for(int j=0;j<=3;j++)
            {
                cposition 
    <<= 1;
                
    if(s[j]=='+')++cposition;
            }
        }

        minSwitchs(cposition);
        
    return 0;
    }
       
  • 相关阅读:
    Oracle:Using the DBMS_STATSpackage
    Oracle partitioning is not always a good idea.
    Oracle: Benefits and consequences of the NOLOGGING option
    Oracle :Insert ways.
    Oracle:临时表的统计信息
    C#中使用DTS来导入数据及相关问题
    [收藏]CSS网页制作时实现自动换行的小技巧
    新加了牛人的Blog链接
    在.Net下使用Access 的日期类型 及与js的日历控件交互
    在程序中生成PDF
  • 原文地址:https://www.cnblogs.com/asuran/p/1574806.html
Copyright © 2011-2022 走看看