The Pilots Brothers' refrigerator
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 22286   Accepted: 8603   Special Judge

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

Source

Northeastern Europe 2004, Western Subregion

题目大意

  给出$4\times 4$共$16$个门把手,改变一个门把手(打开或关闭)需要同时改变同行同列的门把手,当所有门把手都打开时才能打开门。+代表关,-代表开。

基本思路

  此题与POJ 1753差不太多,只是本题没有Impossible的情况,且需要输出改变的门把手的位置。POJ 1753的详细题解请看这里,里面讲的都可以用于此题。因此本文简要略过,只给出爆搜的代码,以及分治的详解。

  一、爆搜出奇迹:

  1、将16个门把手的状态压缩成一个整数。

  2、可以选择改变0个门把手(已经全开),1~16个门把手(重复选相当于没有改变),要求给出最小步数,因此从0搜到16即可。

  3、要求输出要改变的门把手的位置,在搜索中回溯即可。本题为特判,因此输出顺序没有关系。

  4、搜不到要把门把手的状态改回来再搜。

  代码如下:

 #include <stdio.h>

 int handle;

 void read() {
for(int i=; i<; i++) {
for(int j=; j<; j++) {
handle<<=;
if(getchar()=='-')
handle|=;
}
getchar();
}
} void change(int i, int j) {
handle^=0x000F<<(i<<);
handle^=0x1111<<j;
handle^=(0x1<<j)<<(i<<);
} bool check() {
return handle==0xFFFF;
} bool find(int n, int&k, int i, int j) {
if(n==) return check()&&printf("%d\n",k);
if(j==) ++i, j=;
for(; i<; i++) {
for(; j<; j++) {
change(i,j);
if(find(n-,k,i,j+)) {
printf("%d %d\n",-i,-j);
return true;
}
change(i,j);
}
j=;
}
return false;
} void work() {
for(int i=; i<=; i++)
if(find(i,i,,))
return;
} int main() {
read();
work();
return ;
}

POJ 2965 爆搜出奇迹

  二、“分治”?分治!

  1、网上有把这种方法叫做递推的,有叫做贪心的,其实都不太贴切。

  2、考虑一个小问题:若只想改变一个门把手的状态(打开或关闭),而不影响其他的门把手,在同行同列同时操作的规则下,要如何做到?答案是,我们把“同行同列都改变”的操作称为操作op,对于我们想要单独改变的那个门把手,不仅对它做一次操作op,还对它的同行同列的门把手都做一次操作op。注意改变偶数次还是原样,这样中间的门把手改变了$7$次,同行同列的门把手改变了$4$次(因为是$4\times 4$的矩阵),而其他位置改变了2次(对同行同列门把手做操作op时改变的)。于是就达到了我们的目的——只改变中间的门把手状态。

  3、在第2点中,对同行同列元素都做了一次操作op,即都switch了一次。看起来好像不能求得最小步数吗?但要注意在我们把原本是关着的门把手全单独打开的过程中,另外的一些门把手可能被switch了不止$1$次。最终奇数次的才是真正要去switch的。同时这个过程是可逆的,对最终需要switch的门把手都switch一遍之后,就把原本关的全都变成开的了,也就是说我们要计算和保存的是一个要不要switch的状态矩阵(当然你也又可以压缩....不推荐干这种事情,真的)。

  4、在这里,我们先把“把所有门把手都打开,需要改变哪些门把手”的大问题,分成了“单独改变某个门把手,需要改变其他哪些门把手”的小问题,并最终合并各个小问题的答案,得到最终答案,这不是分治是什么?!

  代码如下:

 #include <stdio.h>

 int sswitch[][];

 int main() {
for(int i=; i<; i++) {
for(int j=; j<; j++)
if(getchar()=='+') {
for(int k=; k<; k++) {
sswitch[i][k]^=;
sswitch[k][j]^=;
}
sswitch[i][j]^=;
}
getchar();
} int res=;
for(int i=; i<; i++)
for(int j=; j<; j++)
if(sswitch[i][j])
++res;
printf("%d\n",res); for(int i=; i<; i++)
for(int j=; j<; j++)
if(sswitch[i][j])
printf("%d %d\n",i+,j+);
return ;
}

POJ 2903 分治

——本文原创by BlackStorm,转载请注明出处。

本文链接:http://www.cnblogs.com/BlackStorm/p/5240201.html

POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治的更多相关文章

  1. poj 2965 The Pilots Brothers' refrigerator枚举(bfs+位运算)

    //题目:http://poj.org/problem?id=2965//题意:电冰箱有16个把手,每个把手两种状态(开‘-’或关‘+’),只有在所有把手都打开时,门才开,输入数据是个4*4的矩阵,因 ...

  2. 枚举 POJ 2965 The Pilots Brothers' refrigerator

    题目地址:http://poj.org/problem?id=2965 /* 题意:4*4的矩形,改变任意点,把所有'+'变成'-',,每一次同行同列的都会反转,求最小步数,并打印方案 DFS:把'+ ...

  3. POJ 2965 The Pilots Brothers' refrigerator 位运算枚举

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

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

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

  5. POJ 2965 The Pilots Brothers' refrigerator 暴力 难度:1

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

  6. POJ 2965 The Pilots Brothers' refrigerator (DFS)

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

  7. POJ - 2965 The Pilots Brothers' refrigerator(压位+bfs)

    The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to op ...

  8. POJ 2965 The Pilots Brothers' refrigerator【枚举+dfs】

    题目:http://poj.org/problem?id=2965 来源:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=26732#pro ...

  9. POJ 2965 The Pilots Brothers' refrigerator (枚举+BFS+位压缩运算)

    http://poj.org/problem?id=2965 题意: 一个4*4的矩形,有'+'和'-'两种符号,每次可以转换一个坐标的符号,同时该列和该行上的其他符号也要随之改变.最少需要几次才能全 ...

随机推荐

  1. jQuery LigerUI系列:ligerComboBox

    1. ligerComboBox参数.方法及事件 1.1 参数 2. ligerComboBox示例 2.1 初始化HTML select控件 <link href="/Scripts ...

  2. JavaScript权威设计--Window对象(简要学习笔记十三)

    1.Window对象是所有客户端JavaScript特性和API的主要接入点. Window对象中的一个重要属性是document,它引用Document对象. JavaScript程序可以通过Doc ...

  3. 如何在博客中使用SublimeText风格的代码高亮样式

    因为觉得博客园自带的代码高亮样式很单一,不符合作为前端的我的审美习惯,于是下定决心要想办法折腾出一个方法来应用上另外一套代码高亮样式. 虽然探索的过程是很痛苦的,但最后还是成功了,但也不枉付出的那些努 ...

  4. 前端学HTTP之客户端识别和cookie

    前面的话 Web服务器可能会同时与数千个不同的客户端进行对话.这些服务器通常要记录下它们在与谁交谈,而不会认为所有的请求都来自匿名的客户端.本文主要介绍客户端识别及cookie机制 HTTP首部 HT ...

  5. jeffy-vim-v3.0

    jeffy-vim-v3.0 修改了配色.

  6. 用struts2标签如何从数据库获取数据并在查询页面显示。最近做一个小项目,需要用到struts2标签从数据库查询数据,并且用迭代器iterator标签在查询页面显示,可是一开始,怎么也获取不到数据,想了许久,最后发现,是自己少定义了一个变量,也就是var变量。

    最近做一个小项目,需要用到struts2标签从数据库查询数据,并且用迭代器iterator标签在查询页面显示,可是一开始,怎么也获取不到数据,想了许久,最后发现,是自己少定义了一个变量,也就是var变 ...

  7. PyQt4入门学习笔记(一)

    PyQt4入门学习笔记(一) 一直没有找到什么好的pyqt4的教程,偶然在google上搜到一篇不错的入门文档,翻译过来,留以后再复习. 原始链接如下: http://zetcode.com/gui/ ...

  8. WinServer2008R2 + IIS 7.5 + .NET4.0 经典模式 运行WebAPI程序报404错误的解决方案

    在Windows Server 2008 R2系统下,IIS 7.5 + .NET Framework 4.0的运行环境,以经典模式(Classic Mode)部署一个用.NET 4.0编译的 Web ...

  9. <Node入门经典>读书笔记

    最近在读<Node入门经典>, 之前没有做笔记, 今天开始把看过自己又写了的代码放这里以免忘记. express var express = require('express') var ...

  10. 【C#】给无窗口的进程发送消息

    注:本文适用.net2.0+的winform程序 一个winform程序,我希望它不能多开(但是如何防多开不是本文要讲的),那么在用户启动第二个实例的时候,作为第二个实例来说,大概可以有这么几种做法: ...