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;
}

最新文章

  1. 设计模式学习之装饰者模式(Decorator,结构型模式)(16)
  2. 解决redhat linux下IP地址可以ping通,域名无法ping通问题
  3. 保存登录信息的Cookie加密技术
  4. [转]UIApplicationDelegate分析小结
  5. noip2016赛后总结
  6. Dotfuscator自定义规则中的元素选择
  7. bashell基础
  8. js判断时间段
  9. 2星|《IT真相》:日本咨询师面对美国云服务的发展,对日本IT业哀其不争
  10. poj2987 求最大权闭合回路
  11. LIS最长上升子序列三种方法 (模板)
  12. 学习笔记之1001 Inventions That Changed the World
  13. 全网最详细的Oracle10g/11g的官方下载地址集合【可直接迅雷下载安装】(图文详解)
  14. django 返回json数据
  15. hdu2609 最小表示法
  16. linux 挂载命令mount、umount
  17. HDU1698:Just a Hook(线段树区间更新)
  18. 关于Android应用开发的一些安全注意事项
  19. servlet,listener,filter,interceptor的关系
  20. TPS61175/TPS55340 3A/5A、40V 电流模式集成 FET 升压 DC/DC 转换器

热门文章

  1. C/C++连接MySQL数据库执行查询
  2. (16) Cloudflare pki公钥基础设施
  3. ubuntu14.04 configure: error: xml2-config not found. Please check your libxml2 installation错误解决
  4. The Coco-Cola Store
  5. jQuery对象数据缓存Cache原理及jQuery.data详解
  6. Oracle数据库学习之存储过程--提高程序执行的效率
  7. Archiving not possible: No primary destinations errors
  8. bzoj1708 [Usaco2007 Oct]Money奶牛的硬币 背包dp
  9. 弹飞绵羊(bzoj 2002)
  10. 【BZOJ3626】LCA(树上差分,树链剖分)