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

    The Pilots Brothers' refrigerator
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 14076   Accepted: 5275   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<iostream>
    #include<cstdio>
    #include<cstring>
    
    using namespace std;
    
    int lock[10][10];
    int r[16],c[16];
    int flag,step;
    
    int isOpen(){
        int i,j;
        for(i=3;i<7;i++)
            for(j=3;j<7;j++)
                if(!lock[i][j])
                    return 0;
        return 1;
    }
    
    void flip(int row,int col){
        lock[row][col]=!lock[row][col];
        for(int i=3;i<7;i++)
            lock[i][col]=!lock[i][col];
        for(int j=3;j<7;j++)
            lock[row][j]=!lock[row][j];
    }
    
    void DFS(int row,int col,int deep){
        if(deep==step){
            flag=isOpen();
            return ;
        }
        if(flag || row==7)
            return ;
        r[deep]=row;
        c[deep]=col;
        flip(row,col);
        if(col<6)
            DFS(row,col+1,deep+1);
        else
            DFS(row+1,3,deep+1);
    
        flip(row,col);
        if(col<6)
            DFS(row,col+1,deep);
        else
            DFS(row+1,3,deep);
    }
    
    int main(){
    
        freopen("input.txt","r",stdin);
    
        char ch;
        memset(lock,0,sizeof(lock));
        for(int i=3;i<7;i++)
            for(int j=3;j<7;j++){
                cin>>ch;
                if(ch=='-')
                    lock[i][j]=1;
            }
        for(step=0;step<=16;step++){
            DFS(3,3,0);
            if(flag)
                break;
        }
        printf("%d\n",step);
        for(int i=0;i<step;i++)
            printf("%d %d\n",r[i]-2,c[i]-2);
        return 0;
    }
  • 相关阅读:
    四、运算符
    三、Golang 变量
    二、Golang的概述
    一、Golang开山篇
    部分技术使用
    Teleport_实战
    zabbix_浅谈
    渗透测试工具集合(漏洞练习平台)
    常见开源监控软件的介绍
    Ansible-大保健
  • 原文地址:https://www.cnblogs.com/jackge/p/2944316.html
Copyright © 2011-2022 走看看