POJ 2965 The Pilots Brothers' refrigerator 位运算枚举
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 |
受到POJ1753的启发,用枚举过了,但是这个题有高效的数学方法。。。
#include <stdio.h>
#include <queue>
#include <string.h>
using namespace std; struct node
{
int data, step;
}path[];
queue<struct node>q; int swit(int s, int n)
{
int t = n / * ;
for(int i = t; i <= t+; i++)
s ^= <<i;
for(int i = n % ; i < ; i += )
s ^= <<i;
s ^= <<n;
return s;
} int vis[];
void bfs()
{
while(!q.empty())
{
struct node u = q.front();
q.pop();
if(u.data == )
{
printf("%d\n", u.step);
int tmp = u.data;
for(int i = ; i < u.step; i++)
{
printf("%d %d\n", -path[tmp].step/, -path[tmp].step%);
tmp = path[tmp].data;
}
return;
}
for(int i = ; i < ; i++)
{
int y = swit(u.data, i);
if(!vis[y])
{
q.push((struct node){y, u.step+});
path[y] = (struct node){u.data, i};
vis[y] = ;
}
}
}
} int main()
{
char s[];
int x = ;
for(int i = ; i < ; i++)
{
scanf("%s", s);
for(int j = ; j < ; j++)
{
if(s[j] == '+')
x = (x<<) + ;
else x <<= ;
}
}
memset(vis, , sizeof(vis));
vis[x] = ;
q.push((struct node){x, });
bfs();
return ;
}
POJ 2965 The Pilots Brothers' refrigerator 位运算枚举的更多相关文章
- POJ 2965 The Pilots Brothers' refrigerator (暴力枚举)
https://vjudge.net/problem/POJ-2965 与poj-1753相似,只不过这个要记录路径.poj-1753:https://www.cnblogs.com/fht-lito ...
- 枚举 POJ 2965 The Pilots Brothers' refrigerator
题目地址:http://poj.org/problem?id=2965 /* 题意:4*4的矩形,改变任意点,把所有'+'变成'-',,每一次同行同列的都会反转,求最小步数,并打印方案 DFS:把'+ ...
- POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22286 ...
- 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 (dfs)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17450 ...
- POJ 2965 The Pilots Brothers' refrigerator (DFS)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15136 ...
- POJ 2965 The Pilots Brothers' refrigerator (枚举+BFS+位压缩运算)
http://poj.org/problem?id=2965 题意: 一个4*4的矩形,有'+'和'-'两种符号,每次可以转换一个坐标的符号,同时该列和该行上的其他符号也要随之改变.最少需要几次才能全 ...
- POJ - 2965 The Pilots Brothers' refrigerator(压位+bfs)
The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to op ...
- poj 2965 The Pilots Brothers' refrigerator枚举(bfs+位运算)
//题目:http://poj.org/problem?id=2965//题意:电冰箱有16个把手,每个把手两种状态(开‘-’或关‘+’),只有在所有把手都打开时,门才开,输入数据是个4*4的矩阵,因 ...
随机推荐
- angular js 表单验证
<!doctype html> <html ng-app="myapp"> <head> <meta charset="UTF- ...
- 在地图上添加POI
使用Tangram的Marker, 可以在地图上做各种标记, 效果图: Tangram是通过Marker在地图上添加标记的,Marker分Point, Polyline和Polygon三种, 分别对应 ...
- Karma-Jasmine之安装与实例详解(一)
本文先介绍karma-jasmine的安装.接下来的(二)中主要通过实例利用断言库详解karma-jasmine如何进行单元测试. Karma介绍 首先说一下karma,官方是这样给出的,karma不 ...
- JGit与远程仓库链接使用的两种验证方式(ssh和https)
JGit是使用JAVA的API来操控Git仓库的库,由Eclipse公司维护.他提供的API分成两个层次,底层命令和高层命令.底层API是直接作用于低级的仓库对象,高层的API是一个面向普通用户级别功 ...
- ABAP更改现有程序
语法: READ REPORT <prog> INTO <itab>. INSERT REPORT <prog> FROM <itab>. 假定下列简 ...
- linux IO 内核参数调优 之 原理和参数介绍
1. page cache linux操作系统默认情况下写都是有写缓存的,可以使用direct IO方式绕过操作系统的写缓存.当你写一串数据时,系统会开辟一块内存区域缓存这些数据,这块区域就是我们常 ...
- 升级mac的java版本
在OS X EI Capitan下, java版本太低,从oracle官网下载的dmg文件升级一直有问题, 我发现mac下的java环境有三处 #这应该是系统自带java环境,默认/usr/bin/j ...
- div随另一个div自动增高
<script type="text/javascript"> document.getElementById("div1").style.heig ...
- 4G上网卡NIDS拨号之Rmnet驱动
4G上网卡一般为双对外通讯口,一个是串口.一个是USB. 但是基于串口的常用波特率为115200,速度过于底下,所以大多使用USB. 1)一般来说常用ppp拨号方式,ppp拨号方式分为应用层pppd与 ...
- sql if
SELECT a.id, a.EduSiteNo, a.EduSiteName, a.SchoolId, a.LinkMan, a.Tel, a.Mobile, a.Fax, a.Address, C ...