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 ...
随机推荐
- DEDECMS中,获取当前栏目名称
获取当前栏目名称 {dede:field name='typeurl' function=”GetTypeName(@me)”/} 指的是当前栏目的URL地址 {dede:field name='ty ...
- thinkphp 前后版本ajaxReturn方法的分别
之前用的是thinkphp2的版本现在改到thinkphp3.2已上的版本,发现ajaxReturn这个方法返回的数据不一样了,现在做下记录 thinkphp2的ajaxReturn的实现原码 pro ...
- CCNA第二讲笔记
网络定义:一组由介质(线缆)互联的网络设备(路由器.交换机)和终端系统(PC): 工作组:局域网范畴,范围最小的局域网,且不涉及网络设备.台式机需要有多块网卡,利用双绞线与其他台式机进行互联,扩展性差 ...
- EasyUI –tree、combotree学习总结
EasyUI –tree.combotree学习总结 一. tree总结 (一).tree基本使用 tree控件是web页面中将数据分层一树形结构显示的. 使用$.fn.tree.defaults ...
- MVC3缓存:使用页面缓存
在以前的WebForm的开发中,在页面的头部加上OutputCache即可启用页面缓存,而在MVC3中,使用了Razor模板引擎的话,该如何使用页面缓存呢?如何启用 在MVC3中要如果要启用页面缓存, ...
- mvc的删除
有UserInfo一个实体类,db代表请求上下文 属性有ID,UserName,UserPass,Email var users=from c in db.UserInfo select c; var ...
- mysql高可用探究 MMM高可用mysql方案
1 MMM高可用mysql方案 1.1 方案简介 MMM即Master-Master Replication Manager for MySQL(mysql主主复制管理器)关于mysql主主复 ...
- Asp.net MVC入门视频教程
编程开发 > Asp.net视频教程 > Asp.net MVC入门视频教程 > 1.传统web处理方式和mvc处理方式 上传日期:2014-08-16 10:02:45 相关摘要 ...
- python学习笔记16(错误、异常)
一.什么是错误,什么是异常 错误是指在执行代码过程中发生的事件,它中断或干扰代码的正常流程并创建异常对象.当错误中断流程时,该程序将尝试寻找异常处理程序(一段告诉程序如何对错误做出响应的代码),以帮助 ...
- iOS定位坐标转换工具-b
坐标系介绍 首先介绍一下目前的定位坐标系统1.地球坐标 :( 代号:GPS.WGS84 )--- 有W就是世界通用的也就是原始坐标体系,这是国际公认的世界标准坐标体系: 使用 WGS84 坐标系统的产 ...