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

    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

    题意跟上篇poj1753类似,不同的是此题,翻转一个的同时,会改变这个所在的行和列上所有元素的状态,另外该题输出的是最后的改变路径,只要将1753的翻转函数改一下,并在搜索时加上两个数组记录路径即可,大致思路相同。
    View Code
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <queue>
     5 #include <stack>
     6 using namespace std;
     7 
     8 int map[10][10],n,flag;
     9 int dx[20],dy[20];
    10 void filp(int row,int col)
    11 {
    12     int j;
    13     for(j=0;j<4;j++)
    14     {
    15         map[row][j]=!map[row][j];
    16         map[j][col]=!map[j][col];
    17     }
    18     map[row][col]=!map[row][col];
    19 }
    20 int judge()
    21 {
    22     int i,j;
    23     for(i=0;i<4;i++)
    24         for(j=0;j<4;j++)
    25             if(map[i][j]==0)
    26                 return 0;
    27     return 1;
    28 }
    29 void dfs(int row,int col,int cnt)
    30 {
    31     if(cnt==n)
    32     {
    33         flag=judge();
    34         return ;
    35     }
    36     if(flag||row>=4)
    37         return;
    38     if(row<4)
    39     {
    40         if(col<3)
    41         {
    42             filp(row,col);
    43             dx[cnt]=row,dy[cnt]=col;
    44             dfs(row,col+1,cnt+1);
    45             if(flag) return;
    46             filp(row,col);
    47             dfs(row,col+1,cnt);
    48         }
    49         else 
    50         {
    51             filp(row,col);
    52             dx[cnt]=row,dy[cnt]=col;
    53             dfs(row+1,0,cnt+1);
    54             if(flag) return;
    55             filp(row,col);
    56             dfs(row+1,0,cnt);
    57         }
    58     }
    59     else
    60         return;
    61 }
    62 int main()
    63 {
    64     char s[5][5];
    65     int i,j;
    66     flag=0;
    67     for(i=0;i<4;i++)
    68     {
    69         scanf("%s",s[i]);
    70         for(j=0;j<4;j++)
    71         {
    72             if(s[i][j]=='-')
    73                 map[i][j]=1;
    74             else
    75                 map[i][j]=0;
    76         }
    77     }
    78     for(n=0;n<=16;n++)
    79     {
    80         dfs(0,0,0);
    81         if(flag)
    82             break;
    83     }
    84     printf("%d\n",n);
    85     for(i=0;i<n;i++)
    86         printf("%d %d\n",dx[i]+1,dy[i]+1);
    87     return 0;
    88 }
  • 相关阅读:
    hdu-3001 三进制状态压缩+dp
    最长公共子序列(LCS)
    矩阵最优路线DP
    CF-721C DAG图拓扑排序+费用DP
    拓扑排序
    BFS+二进制状态压缩 hdu-1429
    DAG最长路问题 hdu-1224
    并查集-解决区间和纠错问题 hdu-3038
    hdu 4972 根据每轮篮球赛分差求结果
    hdu 1116 欧拉回路+并查集
  • 原文地址:https://www.cnblogs.com/wilsonjuxta/p/2953687.html
Copyright © 2011-2022 走看看