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. Celery框架简单实例

    Python 中可以使用Celery框架 Celery框架是提供异步任务处理的框架,有两种用法,一种:应用程式发布任务消息,后台Worker监听执行,好处在于不影响应用程序继续执行.第二种,设置定时执 ...

  2. [py][mx]django静态文件目录配置

    使用TemplateView直接返回html from django.views.generic import TemplateView urlpatterns = [ path('',Templat ...

  3. pythonon ddt数据驱动二(json, yaml 驱动)

    这一篇主要是关于文件的数据驱动. 一.通过json文件驱动 @ddt class MyTest(unittest.TestCase): @file_data('test_data_list.json' ...

  4. linux du df ls

    du可以查看文件和文件夹的大小,df主要查看数据库大小,查看分区的的磁盘使用情况,ls可以添加-h参数,方便我们查看文件大小 df可以查看一级文件夹大小.使用比例.档案系统及其挂入点,但对文件却无能为 ...

  5. 004-ubuntu安装配置SSH服务

    一.ssh安装. 1.# sudo apt-get -y install openssh-server. 2.在/etc/ssh/sshd_config文件中添加一句:PermitRootLogin ...

  6. 【转】五分钟读懂大数据核心MapReduce架构及原理

    什么是MapReduce Hadoop中的MapReduce是一个简单的软件框架,基于它写出的应用程序可以运行在由上千个商用机器组成的大型集群上,并以一种可靠容错式并行处理TB级数据 MapReduc ...

  7. Hive配置文件中配置项的含义详解(收藏版)

    这里面列出了hive几乎所有的配置项,下面问题只是说出了几种配置项目的作用.更多内容,可以查看内容 问题导读: 1.hive输出格式的配置项是哪个? 2.hive被各种语言调用如何配置? 3.hive ...

  8. SQL语句常见优化方法

    Sql优化方法 先进行选择运算(where limit)再进行连接运算 where子句中应把过滤性最强的条件放在最前面 where子句中字段的顺序应和组合索引中字段顺序一致 使用索引 使用覆盖索引来避 ...

  9. Linux基础命令---ar

    ar ar指令可以创建.修改库,也可以从库中提取单个模块.库是一个单独的文件,里面包含了按照特定结构组织起来的其他文件,我们称作member.归档文件通常是一个二进制文件,我们一般将归档文件当作库来使 ...

  10. SpringBoot集成Socket服务后打包(war包)启动时如何启动Socket服务(web应用外部tomcat启动)

      1.首先知道SpringBoot打包为jar和war包是不一样的(只讨论SpringBoot环境下web应用打包)     1.1.jar和war包的打开方式不一样,虽然都依赖java环境,但是j ...