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;
    }
  • 相关阅读:
    Zookeeper概念学习系列之分布式事务
    序列化 反序列化 输入流 输出流
    dfs常见的配置文件中的value与description(重要)
    [转]SQL Server Reporting Services
    [转]webpack4.0.1安装问题和webpack.config.js的配置变化
    [转]Vue.js 入门教程
    [转]Webpack 入门教程
    [转]使用C#调用cmd来执行sql脚本
    [转]winform利用读取xml获取webconfig
    [转]bootstrap栅格系统的属性及使用
  • 原文地址:https://www.cnblogs.com/jackge/p/2944316.html
Copyright © 2011-2022 走看看