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 ...
随机推荐
- Excel导入功能
一:前端 <t:dgToolBar title="Excel题库导入" icon="icon-search" onclick="question ...
- centos 7 lNMP 安装之php 篇
1.准备工作 安装依赖包 yum install -y gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype ...
- 51nod1256乘法逆元
1256 乘法逆元 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注给出2个数M和N(M < N),且M与N互质,找出一个数K满足0 < K < ...
- CCNP第三天 EIGRP综合实验
实验题如图所示:其中R2连R3 R5为快速以太网线,其他均为串线,帧中继默认是富曼斯(全连网状结构),即所有接入的路由之间的PVC都已经打通,所有 要关闭R5和R8的逆向arp功能,来手工配置R5到 ...
- KM3
本科:读完一本书,知道book里讲的是什么,带有知识吸收: 硕士:读完一本书,知道book里的内容,而且还能发现相关的问题,带有知识的思考: 博士:读完一本书,知道book里的内容,而且还能发现相关的 ...
- (转载)linux下tar.gz、tar、bz2、zip等解压缩、压缩命令小结
linux下tar.gz.tar.bz2.zip等解压缩.压缩命令小结 bz2 tgz z等众多压缩文件的压缩与解压方法,需要的朋友可以参考下 1) Linux下最常用的打包程序就是tar了,使用ta ...
- oracle 将科学计数法数据转换为非科学计数法数据
oracle 自定义函数: CREATE OR REPLACE FUNCTION ConvertNumeric(rawData VARCHAR2) --用于返回转换科学计算法dhx RETURN VA ...
- javascript eval 执行过程
当执行eval时,会执行如下过程 eval(x): 1.如果 x的类型不是string,那么会return x; 2.把x转换成 ecmascript 代码.如果转换失败,责抛出SyntaxError ...
- MBProgressHUD ---
1,MBProgressHUD常用属性和用法Demo - (void)testMBProgressHUD { NSLog(@"test MBProgressHUD "); /* 要 ...
- Mysql InnoDB彻底释放磁盘空间
Innodb数据库对于已经删除的数据只是标记为删除,并不真正释放所占用的磁盘空间,这就导致InnoDB数据库文件不断增长. 如果需要彻底释放磁盘空间,则需要先导出数据,然后删除数据文件,最后导入数据. ...