zoukankan      html  css  js  c++  java
  • The Pilots Brothers' refrigerator-DFS路径打印

    I - The Pilots Brothers' refrigerator
    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

    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
    /*
    Author: 2486
    Memory: 144 KB		Time: 360 MS
    Language: C++		Result: Accepted
    */
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    const int maxn=1<<18;
    char maps[5][5];
    bool vis[maxn];
    int path[maxn];//记录终于路径
    int pathvis[maxn];//记录DFS过程中的路径
    int Min;
    int binary(int val,int x,int y) {
        for(int i=0; i<16; i++) {//选取行列进行取反
            if(i/4==x||i%4==y) {
                val^=(1<<i);
            }
        }
        return val;
    }
    void dfs(int s,int step,int x,int y) {
        if(s==0) {
            if(Min>step) {
                Min=step;
                for(int i=0;i<step;i++){
                    path[i]=pathvis[i];//假设比先前的步数更少就转到存储终于路径的数组中
                }
            }
            return;
        }
        if(x>=4)return;
        int nx,ny;
        if(y+1>=4) {//向右走然后向下走
            nx=x+1;
            ny=0;
        } else {
            ny=y+1;
            nx=x;
        }
        int fd=x*4+y;
        int k=binary(s,x,y);
        pathvis[step]=fd;//当前的位置进行反转
        dfs(k,step+1,nx,ny);
        dfs(s,step,nx,ny);
    }
    int main() {
        while(~scanf("%s",maps[0])) {
            for(int i=1; i<4; i++) {
                scanf("%s",&maps[i]);
            }
            int s=0;
            for(int i=3; i>=0; i--) {
                for(int j=3; j>=0; j--) {
                    if(maps[i][j]=='+')s=s<<1|1;
                    else s<<=1;
                }
            }
            if(s==0) {
                printf("0
    ");
                continue;
            }
            Min=10000000;
            dfs(s,0,0,0);
            printf("%d
    ",Min);
            for(int i=0; i<Min; i++) {
                printf("%d %d
    ",path[i]/4+1,path[i]%4+1);
            }
        }
        return 0;
    }


  • 相关阅读:
    tinyxml2使用
    使用libcurl作为Http client
    编译Thrift支持golang
    使用vue初体验之app实现后 小总结
    手机端屏幕自适应(三) 淘宝网适配方案
    手机端屏幕自适应(二)
    手机端的屏幕自适应(一)
    vue directive具体的使用方法
    vue生命周期之我见
    vue-router api学习
  • 原文地址:https://www.cnblogs.com/wzjhoutai/p/6869444.html
Copyright © 2011-2022 走看看