POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治
2016-03-03 21:43
134 查看
The Pilots Brothers' refrigerator
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 sswitch[5][5]; int main() { for(int i=0; i<4; i++) { for(int j=0; j<4; j++) if(getchar()=='+') { for(int k=0; k<4; k++) { sswitch[i][k]^=1; sswitch[k][j]^=1; } sswitch[i][j]^=1; } getchar(); } int res=0; for(int i=0; i<4; i++) for(int j=0; j<4; j++) if(sswitch[i][j]) ++res; printf("%d\n",res); for(int i=0; i<4; i++) for(int j=0; j<4; j++) if(sswitch[i][j]) printf("%d %d\n",i+1,j+1); return 0; }
POJ 2903 分治
——本文原创by BlackStorm,转载请注明出处。
本文链接:/article/7078212.html
相关文章推荐
- 类和对象
- 防盗链测试
- KMP算法
- 基于Nutch+Hadoop+Hbase+ElasticSearch的网络爬虫及搜索引擎
- 《Effective C++ 》学习笔记——条款11
- source insight 背景颜色修改 代码字体大小修改 中文注释空格去除
- Hadoop部署目录的作用
- 普通类调用Spring bean对象
- 字典树
- 蓝桥杯-- 入门训练 圆的面积
- Linux下套接字详解(六)----基于pthread的多线程的TCP套接字(阻塞/同步/并发)
- [LeetCode]121. Best Time to Buy and Sell Stock
- PAT (Advanced Level) Practise 1013 Battle Over Cities (25)
- Android GridView 实现9宫格菜单,并实现item点击按压效果更改图片
- iOS-即时通信
- POJ 1741 Tree [树上点分治]
- 使用APMServ配置zend framework时出现open_basedir restriction in effect问题
- 【C++学习之路】派生类的构造函数(二)
- Apache 的ab测试
- 软件工程(1)——对书本的温习