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. A solution for MySQL Assertion failure FIL_NULL

    A solution for MySQL Assertion failure FIL_NULL http://michaelfranzl.com/2014/01/25/solution-mysql-a ...

  2. semi-consistent简介

    semi-consistent简介 http://hedengcheng.com/?p=220 semi-consistent简介    1 semi-consistent实现    2 MySQL ...

  3. nginx 11个处理阶段 && nginx lua 8个处理阶段

    1. nginx 11 个处理阶段 nginx实际把请求处理流程划分为了11个阶段,这样划分的原因是将请求的执行逻辑细分,各阶段按照处理时机定义了清晰的执行语义,开发者可以很容易分辨自己需要开发的模块 ...

  4. 使用SolrJ代码导入,发布搜索服务

    搭建solr服务器:http://www.cnblogs.com/liyafei/p/8005571.html 一导入要搜索的字段 1:确定发布搜索的字段,sql语句 SELECT a.id, b.  ...

  5. iOS UI基础-3.0图片浏览器及plist使用

    需求: 1.显示当前图片序号/总图片数 2.显示图片 3.上一张图片.下一张图片转换 4.显示图片描述 下面用代码来实现 // // UYViewController.m // 3.0图片查看器 // ...

  6. Twitter OA prepare: Equilibrium index of an array

    Equilibrium index of an array is an index such that the sum of elements at lower indexes is equal to ...

  7. cc150 --链表中倒数第k个节点

    题目描述 输入一个链表,输出该链表中倒数第k个结点.   快指针先走K步,然后快慢同时走,快走到末尾时,慢指针就是倒数第个.     public class Solution { public Li ...

  8. layer常用方法代码

    layer是一款近年来备受青睐的web弹层组件,她具备全方位的解决方案,致力于服务各水平段的开发人员,您的页面会轻松地拥有丰富友好的操作体验. 在与同类组件的比较中,layer总是能轻易获胜.她尽可能 ...

  9. nginx 总结

    本文转自:http://freeloda.blog.51cto.com/2033581/1288553 ,感谢大神的辛勤付出! 大纲 一.前言 二.环境准备 三.安装与配置Nginx 四.Nginx之 ...

  10. 日志处理(一) log4j 入门和详解(转)

    log4j  入门. 详解 转自雪飘寒的文章 1. Log4j  简介 在应用程序中添加日志记录总的来说基于三 个目的:    监视代码中变量的变化情况,周期性的记录到文件中供其他应用进行统计分析工作 ...