zoukankan      html  css  js  c++  java
  • 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]);
    
    }
  • 相关阅读:
    javascript入门篇(一)
    vue开发项目详细教程(第一篇 搭建环境篇)
    node基础03:使用函数
    node基础02:第一个node程序
    node基础01:简要介绍
    mac基本用法
    ES5基础01:正则表达式
    php基础11:运算符
    H5(一):使用formData对象模拟表单
    HTTP基础(一):如何使用浏览器network查看请求和响应的信息
  • 原文地址:https://www.cnblogs.com/joeylee97/p/6658796.html
Copyright © 2011-2022 走看看