zoukankan      html  css  js  c++  java
  • POJ 2965 The Pilots Brothers' refrigerator (DFS)

    The Pilots Brothers' refrigerator
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 15136   Accepted: 5660   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
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<queue>
    
    using namespace std;
    
    char str[5][5];
    
    int change[16]={ 63624 , 62532, 61986,61713, 36744,20292,12066,
    7953,35064, 17652,8946,4593,34959, 17487, 8751, 4383};
    
    int vis[70000];
    int path[70000];
    
    struct node{
        int status;
        int step;
    };
    
    void BFS(int curstate){
        queue<node> q;
        while(!q.empty())
            q.pop();
        node cur,next;
        cur.status=curstate,    cur.step=0;
        vis[cur.status]=1;
        q.push(cur);
        while(!q.empty()){
            cur=q.front();
            q.pop();
            for(int i=0;i<16;i++){
                next.status=cur.status^change[i];
                next.step=cur.step+1;
                if(!vis[next.status]){
                    vis[next.status]=1;
                    path[next.status]=i;
                    q.push(next);
                }
                if(next.status==65535){
                    printf("%d
    ",next.step);
                    int k=next.status;
                    while(k!=curstate){
                        printf("%d %d
    ",path[k]/4+1,path[k]%4+1);
                        k=k^change[path[k]];
                    }
                    return ;
                }
            }
        }
    }
    /*
    int num[16];        //即求change[]的方法
    
    void init(){
        for(int i=0;i<16;i++){
            num[i]=0;
            for(int j=0;j<16;j++)
                if((i/4==j/4) || (i%4==j%4))
                    num[i]=(num[i]<<1)+1;
                else
                    num[i]<<=1;
        }
    }
    */
    int main(){
    
        //freopen("input.txt","r",stdin);
        /*
        init();
        for(int i=0;i<16;i++)
            printf("%d ",num[i]);
        printf("
    ");
        */
        while(~scanf("%s",str[1]+1)){
            for(int i=2;i<5;i++)
                scanf("%s",str[i]+1);
            int status=0;
            for(int i=1;i<=4;i++)
                for(int j=1;j<=4;j++){
                    status<<=1;
                    if(str[i][j]=='-')
                        status |= 1;
                }
            if(status==65535){
                printf("0
    ");
                continue;
            }
            memset(vis,0,sizeof(vis));
            BFS(status);
        }
        return 0;
    }
    4 4

    Source

    Northeastern Europe 2004, Western Subregion
     
     
     
  • 相关阅读:
    数据仓库与数据挖掘的一些基本概念
    System.currentTimeMillis();
    html练习(3)
    HDU 1556 Color the ball【算法的优化】
    ubuntu12.04 安装配置jdk1.7
    java中的switch用String作为条件
    oracle中 connect by prior 递归算法
    C#复习题
    AngularJS:Http
    AngularJS:Service
  • 原文地址:https://www.cnblogs.com/jackge/p/3140062.html
Copyright © 2011-2022 走看看