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的矩阵,因 ...
随机推荐
- 如何把select出来的一列数据放在第一个单元格
有如下表: STORE_ID | CUSTOMER_ID S001 | C001 S001 | C002 能不能把上表SELECT出来变成下面这种形式: ...
- freemaker的基本语法
先来解释一下freemaker的基本语法了,<# ... > 中存放所有freemaker的内容,之外的内容全部原样输出.<@ ... /> 是函数调用两个定界符内的内容中,第 ...
- Nginx平台构架 分类: Nginx 2015-07-13 10:55 205人阅读 评论(0) 收藏
深入理解Nginx模块发开与架构解析读书笔记. nginx在启动后,在unix系统中会以daemon的方式(可以手动关闭 nginx.conf daemon off)在后台运行,后台进程包含一个mas ...
- Linux下设置最大文件打开数nofile及nr_open、file-max
在开发运维的时候我们常常会遇到类似“Socket/File: Can’t open so many files”,“无法打开更多进程”,或是coredump过大等问题,这些都可以设置资源限制来解决.今 ...
- TCP参数设置
我们这里应用的是CentOS5.3,并内核使用的是2.6.18-128.el5PAE #1 SMP .修改部分TCP ,有的是为了提高性能与负载,但是存在降低稳定性的风险.有的则是安全方面的配置,则有 ...
- cocos2d-x plist+wen使用
http://zhan.renren.com/tag?value=cocos2dx&from=template http://blog.csdn.net/zhanglongit/article ...
- cocos2d-x 2.0.3 设置高清模式注意事项(已移除-hd方式)
猴子原创,欢迎转载.转载请注明: 转载自Cocos2D开发网–Cocos2Dev.com,谢谢! 原文地址: http://www.cocos2dev.com/?p=304 在cocos2d-x 2. ...
- use_virtual_func_without_pointer_left
#include <oistream> using namespace std; class A { public: void foo() { func(); } virtual void ...
- linux lsof nmap netstat
lsof -i :22 # 显示22端口当前运行的程序 lsof -c ssh # 显示ssh进程打开的文件 lsof -p 2120 #显示进程id2120打开的文件 nmap -sP ...
- phpstrtotime()对于31日求上个月有问题
PHP自带的strtotime()对于31日求上个月有问题,如下: <?php $date = "2012-07-31"; $date_unix = strtotime($d ...