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;
    }
       
  • 相关阅读:
    Unreal Engine 4官网教程
    快速上手制作暗黑类游戏动作打击感的一些要点
    充分利用 UE4 中的噪声
    综合帖:多角度闲聊游戏打击感
    鹅厂内部分享:七步教你从无到有做数值
    二叉树、二叉搜索树、平衡二叉树、B树、B+树的精确定义和区别探究
    MySQL的InnoDB索引原理详解
    分布式核心
    Mysql 原理以及常见mysql 索引等
    redis核心原理
  • 原文地址:https://www.cnblogs.com/asuran/p/1574806.html
Copyright © 2011-2022 走看看