zoukankan      html  css  js  c++  java
  • poj 2965 枚举+DFS

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

     思路:因为每个位置最多翻一次,所以最多翻16次,用枚举来做。(代码是看别人的,有一部分还不能完全理解。。)

    #include "cstdio"
    #include "algorithm"
    #include "cstring"
    char map[5][5];
    int m[5][5];
    typedef struct {
        int s,t;
    }road;
    road a[20];
    int k,ans;
    int P(){
        for(int i=1;i<=4;i++){
            for(int j=1;j<=4;j++){
                if(m[i][j]==0){
                    return 0;
                }
            }
        }
        return 1;
    }
    void G(int x,int y){
        m[x][y]^=1;
        for(int i=1;i<=4;i++){
            m[x][i]^=1;
        }
        for(int i=1;i<=4;i++){
            m[i][y]^=1;
        }
    }
    void  dfs(int x,int y,int step) {
    //    G(x, y);
        if (step == ans) {
            k = P();//
            return;
        }
        if(k||x>=5){//不太理解这里为什么要判断k是否为1,个人觉得只要当ans=step时在dfs外判断即可??
            return;
        }
        G(x, y);
        if(y<4){
            dfs(x,y+1,step+1);
            a[step].s=x;
            a[step].t=y;
        }
        else {
            dfs(x+1,1,step+1);
            a[step].s=x;
            a[step].t=y;
        }
        G(x,y);
        if(y<4){
            dfs(x,y+1,step);
        }
        else {
            dfs(x+1,1,step);
        }
    
    }
    int main(){
        k=0;
        for(int i=1;i<=4;i++){
            scanf("%s",map[i]);
            for(int j=0;j<4;j++){
                if(map[i][j]=='-'){
                    m[i][j+1]=1;
                }
                else if(map[i][j]=='+'){
                    m[i][j+1]=0;
                }
            }
        }
        for(int i=1;i<=16;i++){
            ans=i;
            dfs(1,1,0);
            if(k){
                printf("%d
    ",ans);
                break;
            }
        }
        for(int i=0;i<ans;i++){
            printf("%d %d
    ",a[i].s,a[i].t);
        }
        return  0;
    }
  • 相关阅读:
    [LeetCode] Best Time to Buy and Sell Stock with Transaction Fee 买股票的最佳时间含交易费
    Visual Studio Many Projects in One Solution VS中多工程开发
    [LeetCode] 713. Subarray Product Less Than K 子数组乘积小于K
    [LeetCode] Minimum ASCII Delete Sum for Two Strings 两个字符串的最小ASCII删除和
    [LeetCode] Erect the Fence 竖立栅栏
    3D Slicer Reconstruct CT/MRI
    [LeetCode] Partition to K Equal Sum Subsets 分割K个等和的子集
    [LeetCode] Degree of an Array 数组的度
    [LeetCode] Count Binary Substrings 统计二进制子字符串
    [LeetCode] Max Area of Island 岛的最大面积
  • 原文地址:https://www.cnblogs.com/mj-liylho/p/6357849.html
Copyright © 2011-2022 走看看