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
     
     
     
  • 相关阅读:
    github not authorized eclipse 关于 代码不能提交到GitHub
    idea 导入项目后 有的项目目录结构不展开解决办法
    intellij idea 主题大全,看不惯idea 那2种主题的来这里了
    win10 系统输入法与 idea的 ctr+shift+f 快捷键冲突,解决办法
    此地址使用了一个通常用于网络浏览以外目的的端口。出于安全原因,Firefox 取消了该请求。
    关于IntelliJ IDEA有时候快捷键无效的说明
    杜恩德是谁?
    oracle如何连接别人的数据库,需要在本地添加一些配置
    2.6---找有环链表的开头结点(CC150)
    2.3---删除链表的结点,不提供头结点(CC150)
  • 原文地址:https://www.cnblogs.com/jackge/p/3140062.html
Copyright © 2011-2022 走看看