POJ2965——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的更多相关文章
- [POJ2965]The Pilots Brothers' refrigerator (搜索/位运算)
题意 游戏“The Pilots Brothers:跟随有条纹的大象”有一个玩家需要打开冰箱的任务. 冰箱门上有16个把手.每个手柄可以处于以下两种状态之一:打开或关闭.只有当所有把手都打开时,冰箱才 ...
- poj2965 The Pilots Brothers' refrigerator —— 技巧性
题目链接:http://poj.org/problem?id=2965 题解:自己想到的方法是枚举搜索,结果用bfs和dfs写都超时了.网上拿别人的代码试一下只是刚好不超时的,如果自己的代码在某些方面 ...
- poj2965 The Pilots Brothers' refrigerator
题目链接:http://poj.org/problem?id=2965 分析:1.这道题和之前做的poj1753题目差不多,常规思路也差不多,但是除了要输出最少步数外,还要输出路径.做这道题的时候在怎 ...
- POJ 2965 The Pilots Brothers' refrigerator 暴力 难度:1
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16868 ...
- POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22286 ...
- POJ2965The Pilots Brothers' refrigerator(枚举+DFS)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22057 ...
- The Pilots Brothers' refrigerator(dfs)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19718 ...
- 枚举 POJ 2965 The Pilots Brothers' refrigerator
题目地址:http://poj.org/problem?id=2965 /* 题意:4*4的矩形,改变任意点,把所有'+'变成'-',,每一次同行同列的都会反转,求最小步数,并打印方案 DFS:把'+ ...
- 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 ...
随机推荐
- [转]Linux/Unix系统镜像/备份/恢复 (dd 命令使用)
ref: http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=22561912&id=156879 开源系统默认安装了一个d ...
- 简单3D翻转
1.先上图~~~ 2.代码 html部分 <body> <div id="my3d"> <div id="box"> < ...
- MSP430F4152串口操作
/**********************************************************************/ /* 名称:串口通讯 功能:将接到的数据组后原封不 ...
- select 函数实现 三种拓扑结构 n个客户端的异步通信 (完全图+线性链表+无环图)
一.这里只介绍简单的三个客户端异步通信(完全图拓扑结构) //建立管道 mkfifo open顺序: cl1 读 , cl2 cl3 向 cl1写 cl2 读 , cl1 cl3 向 cl2写 cl3 ...
- aaaa
http://www.host.com http://www.host.com http://sz.weixun.com/scenery/details-3.htm http://sz.weixun. ...
- csharp excel interop programming
string fileName = "c:\\a.xlsx"; var application = new Application(); application.Visible = ...
- 在百万数据中找出重复的数据sql
select * from (select count(name) as isone, name from tbl_org_departments group by name) t where t.i ...
- ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务 的解决方法
今天用PL/SQL连接虚拟机中的Oracle数据库,发现报了“ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务”错误,也许你也遇到过,原因如下: oracle安装成功后,一直未停止 ...
- Code for the Homework1 改进
#include <iostream> #include <vector> #include "shape.h" //using namespace std ...
- Deferred Shading(延迟渲染)
1.简介 在计算机图形学的词典里,Shading表示“对受光物体的渲染”,这个渲染过程包括下面几步[1]: 1) 计算几何多边形(也就是Mesh). 2) 决定表面材质特性,例如法 ...