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的矩阵,因 ...
随机推荐
- text code
https://github.com/Itseez/opencv/blob/master/modules/objdetect/src/erfilter.cpp
- [TypeScript] Using Lodash in TypeScript with Typings and SystemJS
One of the most confusing parts of getting started with TypeScript is figuring out how to use all th ...
- KALI ssh无法登陆的解决办法
应该是sshd的设置不允许root用户用密码远程登录 修改 vim /etc/ssh/sshd_config 找到# Authentication:LoginGraceTime 120PermitRo ...
- linux vncserver配置
需要使用Linux的远程桌面服务,xmanager之前用过,感觉一般,这次尝试下vnc 我的操作系统是centos7 一服务端 安装VNCServer #yum -y install vnc *vnc ...
- 动态树 Link-Cut Trees
动态树 动态树问题, 即要求我们维护一个由若干棵子结点无序的有根树组成的森林. 要求这个数据结构支持对树的分割.合并,对某个点到它的根的路径的某些操作,以及对某个点的子树进行的某些操作. 在这里我们考 ...
- SharePoint SiteCollection Administrator
到网上去找怎么取到一个站点的sitecollection Administrator, 如果设置了一个站点的 sitecollection Administrator, 那么通过: SPSite ...
- 在Abp框架中使用Mysql数据库的方法以及相关问题小记
最近发现了一款DDD的框架 看起来不错,据说挺流弊的 刚好最近要弄点小东西,拿来试试也不错 苦于穷逼买不起高配服务器,只好装mysql数据库了 下面说下如何在该框架下使用Mysql数据库 打开项目后, ...
- HTML中的API
在程序语言里面就使用API这个行为来讲,可拆解为两个操作:取得API接口和运行API功能 例如:书本具有传授知识的功能,这里就好比一个API,学生拿出某个课本学习,就相当于取得API,学习通过课本学习 ...
- AIX filesystemcache引发的Oracle事故
链接地址: http://www.jydba.net/aix-filesystemcache%e5%bc%95%e5%8f%91%e7%9a%84oracle%e4%ba%8b%e6%95%85/ A ...
- Struts2多文件上传
第一步:首先创建一个多文件上传的页面 <html> <head> <meta http-equiv="Content-Type" content=&q ...