题目链接:

http://poj.org/problem?id=1753

题意:

给定冰箱门的开关情况,改变一个门则其所在行列的门都会发生改变,求出改变门的最少操作使得最终所有门都是打开状态。

代码:

bfs+状态压缩很容易想到~~

这里的状态压缩要需要多加小心,注意一下存储的是翻转门的情况~

#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
typedef pair<int, int> p;
const int maxn = 1<<17;
struct node
{
int value;
int step;
};
int pos[maxn];
int vis[maxn];
int x[maxn], y[maxn];
node sta;
int pa[maxn];
int dir[16] = {
0xf888, 0x8f88, 0x88f8, 0x888f, 0xf444, 0x4f44, 0x44f4, 0x444f,
0xf222, 0x2f22, 0x22f2, 0x222f, 0xf111, 0x1f11, 0x11f1, 0x111f
};
int bfs()
{
queue<node>q;
vis[sta.value] = 1;
sta.step = 0;
pa[sta.value] = -1;
q.push(sta);
while(!q.empty()){
node t = q.front();q.pop();
if(!t.value) return t.step;
for(int i = 0; i < 16; i++){
node n;
n.value = t.value ^ dir[i];
if(vis[n.value]) continue;
vis[n.value] = 1;
pa[n.value] = t.value;
n.step = t.step + 1;
pos[n.value] = i;
q.push(n);
}
}
return -1;
}
int main (void)
{
char c;
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
scanf("%c",&c);
if(c== '+') sta.value |= 1<<(15- i * 4 - j);
}
getchar();
} int res = bfs();
int va = 0;
for(int i = 0; i < res; i++){
x[i] = pos[va] % 4 + 1;
y[i] = pos[va] /4 + 1;
va = pa[va];
}
printf("%d\n",res);
for(int i = res - 1; i >= 0; i--)
printf("%d %d\n", x[i], y[i]);
return 0; }

分析:

脑洞做法:

首先观察样例,发现关门的在(1,2)(4,2),而样例却把他们所在行和列的全部翻了一遍。试想,对于某一点(1,2)来说,行和列的元素都翻一遍的话, (1,2)改变了7次,行列元素改变4次,而其他元素改变2次~~也就是说实际上只有(1,2)的状态改变了。把所有行列的元素翻一遍,记录翻动次数,统计翻转过后依然不满足的有多少个,直接翻转这些就好了~至于这道题因为(1,2)(4,2)本身在同一列~所以最后翻动次数为偶数~相当于没有动,就可以不翻转他们了~

#include<iostream>
using namespace std;
const int maxn = 10;
int a[maxn][maxn];
int m[maxn][maxn];
int row[maxn], cal[maxn];
int main (void)
{
char c;
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
cin>>c;
a[i][j] = (c=='-')?0:1;
}
}
for(int i = 0; i <4 ;i++){
for(int j = 0; j < 4; j++){
if(!a[i][j]) continue;
for(int k = 0; k < 4; k++){
m[i][k]++;
m[k][j]++;
}
}
}
int res = 0;
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
if((m[i][j] + a[i][j])&1){
row[res] = i;
cal[res++] = j;
}
}
}
cout<<res<<endl;
for(int i = 0; i < res; i++)
cout<<row[i] + 1<<' '<<cal[i] + 1<<endl;
return 0; }

这种方格上玩一个方块动会对周围的方块产生影响之类的问题,适合用奇偶表示翻转状态~~~


遇见问题还是要多思考多观察~~不要硬碰硬,说不定会有更巧妙的方法~

POJ 2965 The Pilots Brothers' refrigerator【BFS+状压 Or 脑洞】的更多相关文章

  1. 枚举 POJ 2965 The Pilots Brothers' refrigerator

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

  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 暴力 难度:1

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

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

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

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

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

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

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

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

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

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

  9. POJ 2965 The Pilots Brothers' refrigerator (枚举+BFS+位压缩运算)

    http://poj.org/problem?id=2965 题意: 一个4*4的矩形,有'+'和'-'两种符号,每次可以转换一个坐标的符号,同时该列和该行上的其他符号也要随之改变.最少需要几次才能全 ...

随机推荐

  1. codevs 6116 区间素数

     时间限制: 8 s  空间限制: 256000 KB  题目等级 : 白银 Silver 题解       题目描述 Description 小明喜欢研究素数,他想统计两个自然数之间的素数个数,现在 ...

  2. javaee 第14周

    1.web server Web Server中文名称叫网页服务器或web服务器.WEB服务器也称为WWW(WORLD WIDE WEB)服务器,主要功能是提供网上信息浏览服务.Web服务器可以解析( ...

  3. python笔记_magic变量和函数

    前言 先扯一点背景知识 PEP8(Python Enhancement Proposal)是一份python的编码规范,链接:http://www.python.org/dev/peps/pep-00 ...

  4. scriptPubKey and scriptSig

    First of all two matching scripts are used in two different transactions, one that transfers funds t ...

  5. dockerfile note

    dockerfile note reference summary defination docker can build images automatically by reading the in ...

  6. shim和polyfill的区别

    今天看vue的响应原理,突然被提到shim这个词,翻阅些许资料然后整理出以下这些内容. 在JavaScript的世界里,有两个词经常被提到,shim和polyfill. 首先理解这两个词之前我们先来了 ...

  7. 5.1 qbxt 一测 T2

    求和[问题描述] 组合数 C(n,m)是从 n 个物品中取 m 个的方案数. C(n,m)=(n!)/(m!(n-m)!) 斐波那契数列 F 满足,F[0]=F[1]=1,n≥2 时 F[n]=F[n ...

  8. ZooKeeper运行原理和基本编程接口

    什么是ZooKeeper  ZooKeeper作为一个分布式的服务框架(与Google Chubby类似),主要用于解决分布式集群中应用系统的一致性问题,它能提供基于类似于文件系统的目录节点树方式的数 ...

  9. MySQL sys Schema

    MySQL sys Schema 使用sys Schema的先决条件 使用sys Schema sys Schema Progress Reporting sys Schema Object Refe ...

  10. 8. EVENTS

    8. EVENTS EVENTS表提供有关事件管理器事件的信息,这将在"使用事件调度程序"中讨论. EVENTS表有以下列: - EVENT_CATALOG:事件所属目录的名称.这 ...