The Pilots Brothers' refrigerator

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
题目大意:给了一个4*4的棋盘,每个格子有正负两种状态,设任意一组数(i,j),将第i行与第j列全部翻转正负。输出使棋盘变为全负的最少步骤,并打印任意一种最短步骤过程。
     方法与POJ1753相似,均为位运算+BFS
解题思路:同POJ1753 用十六个二进制数来表示当前棋盘状态,用位运算来翻转正负,用bfs来求出最短距离。
     用step[]和father[]来存储当前状态的父节点和翻转的(i,j)。
     搜素完后 根据step和father两个数组反向找,并输出(若一组(i,j)可以使其完成翻转,那么改组的倒序也可以??)
PS:对于一个状态1进行(i1,j1)(i2,j2)。。。(in,jn)进行翻转后到达状态2。
  那么对状态1进行(in,jn)。。。(i1,j1)进行翻转后同样能到达状态2。
 #include<stdio.h>
int vis[]= {},dis[],father[],step[];//father表示当前下标状态的父节点状态,step表示父节点是如何变幻成当前状态的
int c=,queue[*];
int fz(int a,int xy)//通过位运算进行翻转
{
int tmp=,x1,y1;
tmp=tmp<<(xy-);
a=a^tmp;
x1=xy/,y1=xy%;
if (y1==) y1=;
else x1++;
if (x1==) a=a^0x000F;
if (x1==) a=a^0x00F0;
if (x1==) a=a^0x0F00;
if (x1==) a=a^0xF000;
if (y1==) a=a^0x1111;
if (y1==) a=a^0x2222;
if (y1==) a=a^0x4444;
if (y1==) a=a^0x8888;
return a;
}
int bfs(int a)
{
int i,t,front=,rear=,tmp=,ok;
dis[a]=,vis[a]=;
step[a]=,father[a]=-;
queue[front]=a;
while (front<rear)
{
for (i=; i<=; i++)
{
tmp=fz(queue[front],i);
if (vis[tmp]==)
{
father[tmp]=queue[front];
step[tmp]=i;
queue[rear]=tmp;
vis[tmp]=;
dis[rear++]=dis[front]+;
if (tmp==)
{
c=dis[rear-];
return tmp;
}
}
}
front++;
}
return ;
}
int main()
{
char tmp[];
int k,a,i,t=,x1,y1,j;
for (i=; i<=; i++)
{
scanf("%c",&tmp[i]);
if (i%==&&i!=) getchar();
}
k=,a=;
for (i=; i>=; i--)//将状态转换成整数(eg:-+-+为0101)
{
if (tmp[i]=='+') a+=k;
k*=;
}
t=bfs(a);
printf("%d\n",c);
i=;
while (father[t]!=-)
{
j=step[t];
x1=j/,y1=j%;
if (y1==) y1=;
else x1++;
x1=-x1,y1=-y1;
printf("%d %d\n",x1,y1);
t=father[t];
}
return ;
}

POJ2965——The Pilots Brothers' refrigerator的更多相关文章

  1. [POJ2965]The Pilots Brothers' refrigerator (搜索/位运算)

    题意 游戏“The Pilots Brothers:跟随有条纹的大象”有一个玩家需要打开冰箱的任务. 冰箱门上有16个把手.每个手柄可以处于以下两种状态之一:打开或关闭.只有当所有把手都打开时,冰箱才 ...

  2. poj2965 The Pilots Brothers' refrigerator —— 技巧性

    题目链接:http://poj.org/problem?id=2965 题解:自己想到的方法是枚举搜索,结果用bfs和dfs写都超时了.网上拿别人的代码试一下只是刚好不超时的,如果自己的代码在某些方面 ...

  3. poj2965 The Pilots Brothers' refrigerator

    题目链接:http://poj.org/problem?id=2965 分析:1.这道题和之前做的poj1753题目差不多,常规思路也差不多,但是除了要输出最少步数外,还要输出路径.做这道题的时候在怎 ...

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

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

  5. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

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

  6. POJ2965The Pilots Brothers' refrigerator(枚举+DFS)

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

  7. The Pilots Brothers' refrigerator(dfs)

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

  8. 枚举 POJ 2965 The Pilots Brothers' refrigerator

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

  9. The Pilots Brothers' refrigerator 分类: POJ 2015-06-15 19:34 12人阅读 评论(0) 收藏

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

随机推荐

  1. httpd 配置用户访问认证

    需求:单用户访问网站的某个目录,需要使用帐号密码来登录才能访问. 一.编辑虚拟主机的配置文件,添加目录级访问限制 <Directory "/var/www/html/demo" ...

  2. C语言中fgetc、fputc和getc、putc的区别是什么

    看书的时候,发现了这四个函数,想知道他们的不同.结果上网查发现很多人说fgetc.fputc的f代表的是file,就是这两个函数是和文件有关的!但是一看他们的函数声明,如下图: 发现他们的参数里面都有 ...

  3. KnockoutJS(1)-数据模型

    前言 说到数据模型(ViewModel),就不得不提到MVVM模式,接触过WPF和Silverlight的人应该对这个模式比较熟悉. 不熟悉也没多大关系,因为KnockoutJS的使用相对简单. MV ...

  4. C# 获取中文星期的两种方法

    //方法一 public string Week() { string[] weekdays = { "星期日", "星期一", "星期二" ...

  5. php无极分类

    <?php date_default_timezone_set('PRC'); header('Content-type:text/html;charset=UTF-8'); /* $a_lis ...

  6. http返回状态代码及含义

    “100″ : Continue(继续) 初始的请求已经接受,客户应当继续发送请求的其余部分.(HTTP 1.1新) “101″ : Switching Protocols(切换协议) 请求者已要求服 ...

  7. Python垃圾回收机制详解

    一.垃圾回收机制 Python中的垃圾回收是以引用计数为主,分代收集为辅.引用计数的缺陷是循环引用的问题. 在Python中,如果一个对象的引用数为0,Python虚拟机就会回收这个对象的内存. #e ...

  8. wpf 绑定ObservableCollection 的Count属性

    相信大家都遇到过这样的需求,DataGrid里显示符合筛选条件的学生列表,上方TextBolck里显示学生总数,大家可以这样做: 1,XAML代码 <Window x:Class="O ...

  9. WPF简单入门总结

    WPF简单总结 最近看了点关于WPF的东西,总结了点点入门的东西. XAML语法基础 1.  定义样式 <Window.Resources><!--窗体资源的定义--> < ...

  10. windows 与fedora时间差

    windows 默认BIOS时间当前时间UTC+时区, 按北京时间时区,就是要加8个小时. Linux默认BIOS时间是UTC时间,所以同一机子上装WINDOWS与LINUX时间上会差8个小时.这问题 ...