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 ...
随机推荐
- C# 定时器 一个简单 并且可以直接运行的Demo
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- excel 数字转文本
问: 在EXCEL2003中,如何把一列数字转换成文本格式我的意思的,这一列数字全部变成带有文本格式符号(就是左上角有个绿色小三角)的那种以文本形式存储的数字.目前我只知道一个一个双击单元格,但一列数 ...
- 软件包管理:rpm包管理-yum在线管理-IP地址配置和网络yum源
只需告诉系统你想安装那个包,剩下的所有依赖问题yum都会解决. 有些情况下不能上网,但可以使用光盘. centos的yum是免费的.redhatyum付费. yum管理的其实同样是rpm包.并没有yu ...
- 2:3 Action的配置
< 一 作用> 一:封装工作单元(相当于是控制层,封装出modelAndView) 二:定义name属性接受前台传过来的数据,再定义message属性,用于存放返回前台页面展示的数 据,实 ...
- bootstrap3中关于布局的两种样式
container:用.container包裹的内容即可实现居中对齐.注意,由于在各分辨率下面都设置了padding 和 固定宽度,.container不能嵌套.row:栏栅系统是把父容器平均分为12 ...
- CAEAGLLayer
CAEAGLLayer 当iOS要处理高性能图形绘制,必要时就是OpenGL.应该说它应该是最后的杀手锏,至少对于非游戏的应用来说是的.因为相比Core Animation和UIkit框架,它不可思议 ...
- 小黄人IP营销的四种玩法思维导图
小黄人IP营销的四种玩法思维导图 ------------------------------ 本人微信公众帐号: 心禅道(xinchandao) 本人微信公众帐号:双色球预测合买(ssqyuce)
- linux复制指定目录下的全部文件到另一个目录中,linux cp 文件夹
linux复制指定目录下的全部文件到另一个目录中复制指定目录下的全部文件到另一个目录中文件及目录的复制是经常要用到的.linux下进行复制的命令为cp.假设复制源目录 为 dir1 ,目标目录为dir ...
- web前端----jQuery动画效果
动画效果 // 基本 show([s,[e],[fn]]) hide([s,[e],[fn]]) toggle([s],[e],[fn]) // 滑动 slideDown([s],[e],[fn]) ...
- 散列表(HashTable)
散列表 i. 散列函数 i. 冲突解决 ii. 分离链表法 ii. 开放地址法 iii. 线性探测法 iii. 平方探测法 iii. 双散列 ii. 再散列 ii. 可扩散列 i. 装填因子:元素个数 ...