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. UVM入坑系列笔记(一)

    最近本人在做毕业设计,需要用到UVM搭建验证平台,故在网上查找相关资料,看了一些博客和科普,多少有些收获,记录在这里,以便以后复习查看.以下是本人根据网上学习资料整理的笔记,如果有什么不对的地方欢迎指 ...

  2. 【JDBC-MVC模式】开发实例

    JDBC - 开发实例-MVC模式  1. 在web.xml中配置连接数据库的信息 web.xml: <context-param> <param-name>server< ...

  3. 树莓派 - 蓝牙 (1) 试试Beacon

    首先先了解一下bluez, 以及常用的tools. - hcitool.bluetoothctl等工具,可以进行BLE设备的扫描.连接.配对.广播等操作: - hcitool可以发送HCI comma ...

  4. Android开发——流量统计

    1. 获取应用UID 在设备的proc目录下我们可以看到一些比较熟悉的目录/文件,比如data,system,cpuinfo(获取CPU信息)等,其中uid_stat的各个以应用Uid命名的目录下,便 ...

  5. 洛谷P4779 Dijkstra 模板

    这道题很久之前做过 今天复习(复读)一遍 有疑问的就是pair的专属头文件#include<utility> 但是据说iostream和vector等已经包含了这个 #include< ...

  6. 让你系统的了解shell

    当你在进行登录时,系统会检查的文档:1. /etc/profile:首先,系统会检查这个文件,以定义如下这些变量:PATH.USER.LOGNAME.MAIL.HOSTNAME.HISTSIZE.IN ...

  7. 用python写自定义模板

    模板语法有点像php !/usr/bin/env python """ #demo.py.html <html> <?py include head.p ...

  8. Food Delivery (区间DP)

    When we are focusing on solving problems, we usually prefer to stay in front of computers rather tha ...

  9. tarjan求割边割点

    tarjan求割边割点 内容及代码来自http://m.blog.csdn.net/article/details?id=51984469 割边:在连通图中,删除了连通图的某条边后,图不再连通.这样的 ...

  10. 79. could not initialize proxy - no Session 【从零开始学Spring Boot】

    [原创文章,转载请注明出处] Spring与JPA结合时,如何解决懒加载no session or session was closed!!! 实际上Spring Boot是默认是打开支持sessio ...