zoukankan      html  css  js  c++  java
  • POJ2965【+,-开冰箱】简单DFS--有一步看不懂!!

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

    Source

    Northeastern Europe 2004, Western Subregion
     
    题意:每次翻一点,把它横竖行(包括自己)都改变,直到全为 - ;输出步骤数和具体步骤
     
    #include <stdio.h>
    #include <math.h>
    int map1[6][6],temp,l;
    int way[17][2]={1};
    
    int judge()
    {
        int x=0,i,j;
        for (i=1; i<=4; i++)
        {
            for (j=1; j<=4; j++)
                x+=map1[i][j];
        }
        if(x==16)
            return 1;
        else
            return 0;
    }
    
    void convert(int x,int y)
    {
        int i;
        map1[x][y]=!map1[x][y];
        for(i=1;i<=4;i++)
        {
            map1[x][i]=!map1[x][i];
            map1[i][y]=!map1[i][y];
        }
    }
    
    void dfs (int x,int y,int t,int sum,int sl)
    {
        int i,j;
        if(t==sum)
        {
            temp=judge();
            l=sl;
            return ;
        }
        for(i=x;i<=4;i++)
        {
            if(i==x)
                j=1+y;        //难以理解
            else
                j=1;
            for(j;j<=4;j++)
            {
                way[sl][0]=i;
                way[sl][1]=j;
                convert(i,j);
                dfs(i,j,t+1,sum,sl+1);
                if(temp==1)return ;
                convert(i,j);
            }
        }
    }
    /*void dfs(int ii,int jj,int now,int total,int ll)
    {
        int i,j;
        if(now==total)
        {
            temp=judge();
            l=ll;
            return;
        }
        for(i=ii;i<=4;i++)
        {
            if(i==ii) j=jj+1;
            else j=1;
            for(;j<=4;j++)
            {
                way[ll][0]=i;
                way[ll][1]=j;
                convert(i,j);
                dfs(i,j,now+1,total,ll+1);
                if(temp==1) return;
                convert(i,j);
            }
        }
    }*/
    int main ()
    {
        int i,j;
        char s;
        for (i=1; i<=4; i++)
        {
            for (j=1; j<=4; j++)
            {
                scanf("%c",&s);
    
                if (s=='+')
                    map1[i][j]=0;
                else
                    map1[i][j]=1;
            }getchar();
        }
        for(i=1;i<=16;i++)
        {
            temp=0;
            dfs(1,0,0,i,1);
            if(temp==1)break;
        }
        printf("%d
    ",l-1);
        for(i=1;i<=l-1;i++)
            printf("%d %d
    ",way[i][0],way[i][1]);
        return 0;
    }
  • 相关阅读:
    SonarQube代码质量管理平台安装与配置
    特殊字符大全
    测试“用户登录”功能
    通用测试用例大全
    k8s dashboard 部署并使用 kubeconfig 登陆
    k8s cert-manager 部署
    nginx ingress部署
    Harbor 安装
    kubernetes 高可用集群1.14 部署
    shell 免密远程执行
  • 原文地址:https://www.cnblogs.com/zhangfengnick/p/4907659.html
Copyright © 2011-2022 走看看