POJ 2965 The Pilots Brothers' refrigerator (枚举+BFS+位压缩运算)
http://poj.org/problem?id=2965
题意:
一个4*4的矩形,有'+'和'-'两种符号,每次可以转换一个坐标的符号,同时该列和该行上的其他符号也要随之改变。最少需要几次才能全部变成'-'。
思路:
这道题和黑白棋那道题目差不多,唯一的差别就是需要记录一下路径。
我是用BFS来做的,用二进制来存储。翻转时用位运算的异或比较方便,可以看一下我的这篇博客(http://www.cnblogs.com/zyb993963526/p/6347741.html),上面写的比较清楚。
#include <iostream>
#include <algorithm>
#include<queue>
using namespace std; const int maxn = ; int map[][];
char s;
int vis[maxn];
int sum; int fac[] = { , , , ,
, , , ,
, , , ,
, , , }; struct node
{
int x;
int d;
}; struct node2
{
int pa;
int i;
}path[maxn]; void print_ans(int k)
{
if (path[k].pa == sum)
{
cout << path[k].i / + << " " << path[k].i % + << endl;
return;
}
print_ans(path[k].pa);
cout << path[k].i / + << " " << path[k].i % + << endl;
} void bfs()
{
queue<node> q;
node p;
p.x = sum;
p.d = ;
q.push(p);
vis[p.x] = ;
while (!q.empty())
{
node u = q.front();
q.pop();
if (u.x == )
{
cout << u.d << endl;
print_ans(u.x);
return;
}
for (int i = ; i < ; i++)
{
int k = u.x^fac[i];
if (!vis[k])
{
vis[k] = ;
node v;
v.x = k;
v.d = u.d + ;
q.push(v);
path[k].pa = u.x;
path[k].i = i;
}
}
}
} int main()
{
//freopen("D:\\txt.txt", "r", stdin);
int cnt = ;
for (int i = ; i < ; i++)
for (int j = ; j < ; j++)
{
cin >> s;
if (s == '-')
{
map[i][j] = ;
sum += ( << cnt); //二进制转换成十进制
}
else map[i][j] = ;
cnt--;
}
bfs();
return ;
}
POJ 2965 The Pilots Brothers' refrigerator (枚举+BFS+位压缩运算)的更多相关文章
- poj 2965 The Pilots Brothers' refrigerator枚举(bfs+位运算)
//题目:http://poj.org/problem?id=2965//题意:电冰箱有16个把手,每个把手两种状态(开‘-’或关‘+’),只有在所有把手都打开时,门才开,输入数据是个4*4的矩阵,因 ...
- 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【BFS+状压 Or 脑洞】
题目链接: http://poj.org/problem?id=1753 题意: 给定冰箱门的开关情况,改变一个门则其所在行列的门都会发生改变,求出改变门的最少操作使得最终所有门都是打开状态. 代码: ...
- 枚举 POJ 2965 The Pilots Brothers' refrigerator
题目地址:http://poj.org/problem?id=2965 /* 题意:4*4的矩形,改变任意点,把所有'+'变成'-',,每一次同行同列的都会反转,求最小步数,并打印方案 DFS:把'+ ...
- POJ 2965 The Pilots Brothers' refrigerator 位运算枚举
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 151 ...
- 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: 15136 ...
- 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(压位+bfs)
The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to op ...
随机推荐
- jquery两稳定版本比较~~
jquery历经了多个版本的更新,版本上的比较貌似没什么必要性,一般来说新的版本会比旧的版本各方面都略有提升,但由于新版中增加了各种新的功能,难免会引起bug的发生.评估一个版本是否适合当前开发场景使 ...
- Python一键安装全部依赖包
requirements.txt用来记录项目所有的依赖包和版本号,只需要一个简单的pip命令就能完成. pip freeze >requirements.txt 然后就可以用 pip insta ...
- ubuntu 安装ftp,配置,和java调用
1:安装 ftp服务器 sudo apt-get install vsftpd 自动安装使用的是主机的用户名和密码:liyafei,1367xxx 访问方式,ftp://localhost:21,ft ...
- Scala系统学习(五):Scala访问修辞符
本章将介绍Scala访问修饰符.包,类或对象的成员可以使用私有(private)和受保护(protected)的访问修饰符进行标注,如果不使用这两个关键字的其中一个,那么访问将被视为公开(public ...
- [django]form不清空问题解决
https://www.cnblogs.com/OldJack/p/7118396.html 有时候提交表单后,发现某个字段写错了,但是form的其他字段竟然被清空,这个万万不能接受.所有django ...
- [LeetCode] 257. Binary Tree Paths_ Easy tag: DFS
Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...
- [LeetCode] 429. N-ary Tree Level Order Traversal_ Easy
Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- Integer类之成员变量
一.一共11个成员变量. 二.详情介绍. 1.value值.这个是Integer类的唯一标志.最重要的实例属性. 2.最小值和最大值常量.注意,计算机里面是以补码形式保存的,因此用十六进制时,给的数据 ...
- mysql参数配置文件
(1)参数配置文件中的内容以键值对形式存在. (2)如何查看键值对?show variables like '%name%';或者查看information_schema库下的global_varia ...
- SSM三层模型之间的参数传递
Controller接收各种形式的传参: RequestMapping()包含三部分信息: 表单传参: 1.表单action请求 1:参数名称和顺序都要和表单字段名称一致,直接接收(适合字 ...