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+位压缩运算)的更多相关文章

  1. poj 2965 The Pilots Brothers' refrigerator枚举(bfs+位运算)

    //题目:http://poj.org/problem?id=2965//题意:电冰箱有16个把手,每个把手两种状态(开‘-’或关‘+’),只有在所有把手都打开时,门才开,输入数据是个4*4的矩阵,因 ...

  2. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

  3. POJ 2965 The Pilots Brothers' refrigerator【BFS+状压 Or 脑洞】

    题目链接: http://poj.org/problem?id=1753 题意: 给定冰箱门的开关情况,改变一个门则其所在行列的门都会发生改变,求出改变门的最少操作使得最终所有门都是打开状态. 代码: ...

  4. 枚举 POJ 2965 The Pilots Brothers' refrigerator

    题目地址:http://poj.org/problem?id=2965 /* 题意:4*4的矩形,改变任意点,把所有'+'变成'-',,每一次同行同列的都会反转,求最小步数,并打印方案 DFS:把'+ ...

  5. POJ 2965 The Pilots Brothers' refrigerator 位运算枚举

      The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 151 ...

  6. POJ 2965 The Pilots Brothers' refrigerator 暴力 难度:1

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16868 ...

  7. POJ 2965 The Pilots Brothers' refrigerator (DFS)

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15136 ...

  8. poj 2965 The Pilots Brothers' refrigerator (dfs)

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17450 ...

  9. 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 ...

随机推荐

  1. spring boot 中用@value给static变量赋值

    需求:改写一个JedisUtils,工具类,所以最好用静态方法和变量. @value("${redis.host}") private static String redisHos ...

  2. Spark Sort-Based Shuffle具体实现内幕和源码详解

    为什么讲解Sorted-Based shuffle?2方面的原因:一,可能有些朋友看到Sorted-Based Shuffle的时候,会有一个误解,认为Spark基于Sorted-Based Shuf ...

  3. [LeetCode] 496. Next Greater Element I_Easy tag: Stack

    You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...

  4. 机器学习理论基础学习18---高斯过程回归(GPR)

    一.高斯(分布)过程(随机过程)是什么? 一维高斯分布 多维高斯分布 无限维高斯分布   高斯网络 高斯过程 简单的说,就是一系列关于连续域(时间或空间)的随机变量的联合,而且针对每一个时间或是空间点 ...

  5. jsp02

    el表达式,jstl标签 1.el表达式是什么? sun制定的一种规范,主要是给标签的属性赋值,或者直接输出. <%=user.name%> java5版本以上支持jsp的el表达式和js ...

  6. TCP状态转换图、滑动窗口、半连接状态、2MSL

    一.TCP状态转换图 下图对排除和定位网络或系统故障时大有帮助,也帮助我们更好的编写Linux程序,对嵌入式开发也有指导意义.    先回顾一下TCP建立连接的三次握手过程,以及关闭连接的四次握手过程 ...

  7. 一个新人对于DW标签的理解

    标签呢分为 一.一般标签 一般标签内又分为 ① 格式控制标签 格式控制标签的书写格式是: <font .....></font>  以font为开头以/font为结尾 font ...

  8. Integer类之equals与hashCode

    一.源码. 1.equals源码.可以看出,比较的是两者的类型相同且内容相同.即是否有相等的int类型的value值. ######################################## ...

  9. python 爬取文章

    这里我们利用强大的python爬虫来爬取一篇文章.仅仅做一个示范,更高级的用法还要大家自己实践. 好了,这里就不啰嗦了,找到一篇文章的url地址:http://www.duanwenxue.com/a ...

  10. 【转】Redis之发布 订阅模式

    本例包括 jedis_demo:入口类 jedis_control:jedis控制器(jedis的连接池) jedis_pub_sub_listener:订阅的监听器 singleton_agent: ...