• The Pilots Brothers' refrigerator DFS+枚举


    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

     

    #include<cstdio>
    #include<set>
    #include<map>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    #include<iostream>
    #include<string>
    using namespace std;
    typedef long long LL;
    
    /*
    DFS 枚举可能的操作步数
    */
    int g[5][5],n,m,step;
    int r[25],c[25];
    bool f = false;
    void change(int x,int y)
    {
        for(int i=1;i<=4;i++)
            if(i!=x)
                g[i][y] = 1-g[i][y];
        for(int j=1;j<=4;j++)
            if(j!=y)
                g[x][j] = 1-g[x][j];
        g[x][y] = 1-g[x][y];
    }
    bool judge()
    {
        for(int i=1;i<=4;i++)
            for(int j=1;j<=4;j++)
                if(g[i][j]==1)
                    return false;
        return true;
    }
    void dfs(int x,int y,int d)
    {
        if(d==step)//达到确定的查找步数
        {
            f = judge();
            return ;
        }
        if(f||x>4) return ;//越界或者已经找到解
        change(x,y);
        r[d]=x;//保存路径。
        c[d]=y;//保存路径。
        if(y<4)
            dfs(x,y+1,d+1);
        else
            dfs(x+1,1,d+1);
        change(x,y);//第二次将矩阵恢复,回溯法
        if(y<4)
            dfs(x,y+1,d);//这里由于反转两次该点,相当于没有取该点
        else
            dfs(x+1,1,d);
    }
    int main()
    {
        string str;
        for(int i=0;i<4;i++)
        {
            cin>>str;
            for(int j=0;j<4;j++)
            {
                g[i+1][j+1] = (str[j]=='-')?0:1;
            }
        }
        for(step = 1;step<=16;step++)
        {
            dfs(1,1,0);
            if(f)
                break;
        }
        printf("%d
    ",step);
        for(int i=0;i<step;i++)
            printf("%d %d
    ",r[i],c[i]);
    
    }
  • 相关阅读:
    重装win7之后,wubi安装的ubuntu的启动菜单不见
    data_source.rb:182: TZInfo::DataSourceNotFound解决办法
    Gem::InstallerError:The 'json' native gem requires installed build tools
    去掉DL is deprecated, please use Fiddle警告信息
    ruby on rails安装(ubuntu 14.04)
    ruby on rails 安装(win8 64位)
    满城月季花开
    python中的zip()函数
    python学习之“序列”的理解
    二进制、十进制、八进制、十六进制的理解
  • 原文地址:https://www.cnblogs.com/joeylee97/p/6658796.html
Copyright © 2020-2023  润新知