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. 深入理解typeof操作符

    typeof可以检测数据的类型 typeof返回结果的其实是字符串:可以通过以下测试出来 console.log( typeof(typeof(a))); // string typeof返回的数据类 ...

  2. [HIHO] 1048 铺地板

    历经千辛万苦,小Hi和小Ho终于到达了举办美食节的城市!虽然人山人海,但小Hi和小Ho仍然抑制不住兴奋之情,他们放下行李便投入到了美食节的活动当中.美食节的各个摊位上各自有着非常多的有意思的小游戏,其 ...

  3. 深入理解JavaScript的设计模式

    使用适当的设计模式可以帮助你编写更好.更易于理解的代码.这样的代码也更容易维护.但是,重要的是不要过度使用它们.在使用设计模式之前,你应该仔细考虑你的问题是否符合设计模式. 当你开始一个新的项目时,你 ...

  4. 《Java编程思想》笔记14.类型信息

    运行时类型信息使得你可以在运行时发现和使用类型信息,主要有两种方式: "传统的"RTTI,它假定我们在编译时已经知道了所有的类型: "反射"机制,它允许我们在运 ...

  5. Ubuntu16.04 搭建svn

    1.安装SVN 安装前先更新一下 sudo apt-get update 接下来安装 sudo apt-get install subversion 2.创建SVN版本库 1.创建home下的svn文 ...

  6. 跟初学者学习IbatisNet第一篇

    写在前面的话:我自己也是一个初学者,写这个专题只是为了对学过知识的巩固,如果有什么不对的地方,欢迎大家指正…………………… 第一篇就简单介绍一下什么是IbatisNet,然后写一个简单的Demo,在后 ...

  7. Cow Exhibition (01背包)

    "Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - Cows with G ...

  8. 字典树模板题 POJ 2503

    #include <cstdio> #include <cstring> ],fr[]; int st; struct Tire{ ]; ]; }node[]; void in ...

  9. 【尺取】HDU Problem Killer

    acm.hdu.edu.cn/showproblem.php?pid=5328 [题意] 给定一个长度为n的正整数序列,选出一个连续子序列,这个子序列是等差数列或者等比数列,问这样的连续子序列最长是多 ...

  10. BZOJ4580: [Usaco2016 Open]248

    n<=248个数字,可以进行这样的操作:将相邻两个相同的数字合并成这个数字+1,求最大能合成多少. f(i,j)--区间i到j能合成的最大值,f(i,j)=max(f(i,k)+1),f(i,k ...