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: 17450   Accepted: 6600   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 iand 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
      1 //164K    454MS    C++    1319B    2014-04-26 11:52:45
      2 /*
      3 
      4     题意:
      5         和poj 1753 差不多,不过这里翻转和判断有点不一样,翻转是行列都要翻,判断是
      6     要全1才行。
      7     
      8     解法和poj1753几乎一样,就是要加个二维数组记录一下输出路径。 
      9 
     10 */
     11 #include<stdio.h>
     12 #include<string.h>
     13 int g[10][10];
     14 int root[16][2];
     15 int flag;
     16 inline int judge(int tg[][10])
     17 {
     18     for(int i=1;i<=4;i++)
     19         for(int j=1;j<=4;j++)
     20             if(g[i][j]==0) return 0;
     21     return 1;
     22 }
     23 void flip(int x,int y)
     24 {
     25     for(int i=1;i<=4;i++){
     26         g[x][i]^=1;
     27         g[i][y]^=1;
     28     }
     29     g[x][y]^=1;
     30 }
     31 void dfs(int x,int y,int cnt,int n)
     32 {
     33     if(cnt==n){   
     34         flag=judge(g);
     35         return;
     36     }
     37     if(flag || y>4) return;
     38     root[cnt][0]=x;
     39     root[cnt][1]=y;
     40     flip(x,y);
     41     if(x<4) dfs(x+1,y,cnt+1,n);
     42     else dfs(1,y+1,cnt+1,n);
     43     flip(x,y);
     44     if(x<4) dfs(x+1,y,cnt,n);
     45     else dfs(1,y+1,cnt,n); 
     46 }
     47 int main(void)
     48 {
     49     char c[10];
     50     while(scanf("%s",c)!=EOF)
     51     {
     52         memset(g,0,sizeof(g));
     53         for(int i=0;i<4;i++) g[1][i+1]=c[i]=='-'?1:0;
     54         for(int i=1;i<4;i++){
     55             scanf("%s",c);
     56             for(int j=0;j<4;j++)
     57                 g[i+1][j+1]=c[j]=='-'?1:0;
     58         } 
     59         flag=0;
     60         int cnt=-1;
     61         for(int i=0;i<=16;i++){
     62             dfs(1,1,0,i);
     63             if(flag){
     64                 cnt=i;break;
     65             }
     66         }
     67         printf("%d
    ",cnt);
     68         for(int i=0;i<cnt;i++)
     69             printf("%d %d
    ",root[i][0],root[i][1]);
     70     }
     71     return 0;
     72 }
     73 
     74 /*
     75 
     76 -+--
     77 ----
     78 ----
     79 -+--
     80 
     81 +---
     82 ----
     83 ----
     84 ----
     85 
     86 -+++ 
     87 +---
     88 +---
     89 +---
     90 
     91 ----
     92 ----
     93 ----
     94 ----
     95 
     96 ++++
     97 ++++
     98 ++++
     99 ++++
    100  
    101 */
  • 相关阅读:
    CodeSmith入门教程
    AJAX调用实例
    药品监控增加表结构
    典型SQL 语句总结
    CRM中常用代码
    win2000sever+IIS5不能下载exe文件处理方法
    常用的文件对应的MIME类型
    数据库范式1NF 2NF 3NF BCNF
    多线程编程中如何更改UI值
    NHibernate学习笔记(2)—关系映射
  • 原文地址:https://www.cnblogs.com/GO-NO-1/p/3691093.html
Copyright © 2011-2022 走看看