码迷,mamicode.com
首页 > 其他好文 > 详细

The Pilots Brothers' refrigerator-DFS路径打印

时间:2015-07-22 20:56:11      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

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\n");
            continue;
        }
        Min=10000000;
        dfs(s,0,0,0);
        printf("%d\n",Min);
        for(int i=0; i<Min; i++) {
            printf("%d %d\n",path[i]/4+1,path[i]%4+1);
        }
    }
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

The Pilots Brothers' refrigerator-DFS路径打印

标签:

原文地址:http://blog.csdn.net/qq_18661257/article/details/47008923

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!