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&#39; refrigerator-DFS路径打印的更多相关文章

  1. poj2965 The Pilots Brothers&#39; refrigerator(直接计算或枚举Enum+dfs)

    转载请注明出处:http://blog.csdn.net/u012860063? viewmode=contents 题目链接:http://poj.org/problem? id=2965 ---- ...

  2. POJ 2965:The Pilots Brothers&#39; refrigerator

    id=2965">The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  3. poj 2965 The Pilots Brothers&#39; refrigerator

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18040 ...

  4. POJ - 2965 - The Pilots Brothers&#39; refrigerator (高效贪心!!)

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19356 ...

  5. poj 2965 The Pilots Brothers&#39; refrigerator(dfs 枚举 +打印路径)

    链接:poj 2965 题意:给定一个4*4矩阵状态,代表门的16个把手.'+'代表关,'-'代表开.当16个把手都为开(即'-')时.门才干打开,问至少要几步门才干打开 改变状态规则:选定16个把手 ...

  6. [poj]2488 A Knight's Journey dfs+路径打印

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45941   Accepted: 15637 Description Bac ...

  7. poj 2965 The Pilots Brothers' refrigerator (dfs)

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17450 ...

  8. POJ2965The Pilots Brothers' refrigerator(枚举+DFS)

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22057 ...

  9. The Pilots Brothers' refrigerator(dfs)

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19718 ...

随机推荐

  1. 03pandas

    一.pandas简述 1)pandas是一个开源的,BSD许可的库,为Python编程语言提供高性能,易于使用的数据结构和数据分析工具. 2)numpy能够帮助我们处理数值,但是pandas除了处理数 ...

  2. CAS机制(多线程)

    ---- 什么是CAS机制 CAS机制主要是发生于Java中原子操作类(JUC)的底层实现中,其中在CAS机制中包含3个基本参数:内存地址V.旧预期值A.要修改的新值B. 当要更新一个变量的时候,只有 ...

  3. C++中派生类使用基类成员的问题

    在C++中,派生类在定义构造函数时,会调用基类构造函数首先完成基类部分的构造: class Derive : public Base { public: Derive(string nam, int ...

  4. 又见GCD (已知最大公约数和其中一个数求另一个数)

    #include<cstdio> int f1(int a,int b) //最大公约数 { ) return b; else return f1(b,a%b); } int f2(int ...

  5. ServletContext作用功能详解

    ServletContext,是一个全局的储存信息的空间,服务器开始,其就存在,服务器关闭,其才释放.request,一个用户可有多个:session,一个用户一个:而servletContext,所 ...

  6. HDU3183A Magic Lamp,和NYOJ最大的数一样

    A Magic Lamp Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  7. hdu 3879 最大权闭合图(裸题)

    /* 裸的最大权闭合图 解:参见胡波涛的<最小割模型在信息学竞赛中的应用 #include<stdio.h> #include<string.h> #include< ...

  8. jmesa应用

    一直以来,都没发现什么好的分页组件,最初时用过displaytag,仔细研究了一下,发现它并没有别人说的那么强大,至少离自己的期望还很远,因此尝试寻找其它新的分页组件,但很久以来都没发现自己满意的.无 ...

  9. 2016 Multi-University Training Contest 4 solutions BY FZU

    1001 Another Meaning 对于这个问题,显然可以进行DP: 令dp[i]表示到i结尾的字符串可以表示的不同含义数,那么考虑两种转移: 末尾不替换含义:dp[i - 1] 末尾替换含义: ...

  10. 从零开始写STL-string类型

    class string { public: typedef size_t size_type; typedef char* iterator; typedef char value_type; pr ...