zoukankan      html  css  js  c++  java
  • POJ_2965 The Pilots Brothers' refrigerator 搜索

    题目链接:http://poj.org/problem?id=2965

    简单搜索,一个一个往下找判断即可。同样最多16次即可,再反转同样的无意义。

    代码:

     1 #include <cstdio>
     2 #include <cstdlib>
     3 #include <iostream>
     4 #include <cstring>
     5 #include <algorithm>
     6 #include <queue>
     7 #include <stack>
     8 #include <vector>
     9 using namespace std;
    10 int n = 4, maze[5][5];
    11 int curstp = 0, minstp = 17;
    12 int fir[17], sec[17], bfir[18], bsec[18];
    13 bool flag = false;
    14 
    15 bool check(){
    16     for(int i = 0; i < 4; i++){
    17         for(int j = 0; j < 4; j++){
    18             if(maze[i][j] == 0){
    19                 return false;
    20             }
    21         }
    22     }
    23     return true;
    24 }
    25 
    26 void change(int ii, int jj){
    27     maze[ii][jj] = !maze[ii][jj];
    28     for(int i = 0; i < 4; i++){
    29         if(i != jj)
    30             maze[ii][i] = !maze[ii][i];
    31         if(i != ii)
    32             maze[i][jj] = !maze[i][jj];
    33     }
    34 }
    35 
    36 int dfs(int t){
    37     int ii = t / 4, jj = t - ii * 4;
    38     if(t >= 16){
    39         if(check() && curstp < minstp){
    40             minstp = curstp;
    41             for(int i = 0; i < 16; i++){
    42                 bfir[i] = fir[i]; bsec[i] = sec[i];
    43             }
    44             flag = true;
    45         } 
    46         return 0;
    47     }
    48     //change 
    49     if(curstp + 1 < minstp){
    50         curstp++; change(ii, jj); 
    51         fir[t] = ii, sec[t] = jj;
    52         dfs(t + 1);
    53         curstp--; change(ii, jj);
    54         fir[t] = -1, sec[t] = -1;
    55     }
    56     //no change
    57     dfs(t + 1);
    58 }
    59 
    60 int main(){
    61     for(int i = 0; i < 4; i++){
    62         for(int j = 0; j < 4; j++){
    63             char tmch;
    64             scanf(" %c", &tmch);
    65             maze[i][j] = tmch == '+'? 0: 1;
    66         }
    67     }
    68     memset(fir, -1, sizeof(fir));
    69     memset(sec, -1, sizeof(sec));
    70     memset(bfir, -1, sizeof(fir));
    71     memset(bsec, -1, sizeof(sec));
    72     dfs(0);
    73     if(flag){
    74         printf("%d
    ", minstp);
    75         for(int i = 0; i < 16; i++){
    76             if(bfir[i] != -1){
    77                 printf("%d %d
    ", bfir[i] + 1, bsec[i] + 1);
    78             }
    79         }
    80     }
    81     else{
    82         printf("");
    83     }
    84 } 

    题目:

    The Pilots Brothers' refrigerator
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 26366   Accepted: 10149   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
  • 相关阅读:
    LeetCode12: 整数转罗马数字
    LeetCode11:盛最多水的容器
    LeetCode09:判断回文数
    LeetCode08:字符串转换成整数
    LeetCode04:寻找中位数
    LeetCode03:无重复字符的最长子串
    《JAVA编程思想》第四版 PDF 下载 中文版和英文版 高清PDF扫描带书签
    XML
    异常
    委托和匿名方法和Lambda表达式
  • 原文地址:https://www.cnblogs.com/bolderic/p/6843888.html
Copyright © 2011-2022 走看看