POJ 2965 The Pilots Brothers' refrigerator
题意:一个冰箱上有4*4共16个开关,改变任意一个开关的状态(即开变成关,关变成开)时,此开关的同一行、同一列所有的开关都会自动改变状态。要想打开冰箱,要所有开关全部打开才行。 输入:一个4×4的矩阵,+表示关闭,-表示打开;输出:使冰箱打开所需要执行的最少操作次数,以及所操作的开关坐标。
题解:核心其实就是,把开关本身以及其同一行同一列的开关(总共7个)都进行一次操作,结果是,开关本身状态改变了7次,开关同一行、同一列的开关状态改变了4次,其他开关状态改变了2次。这就相当于只改变了本身的状态。当然这道题也可以用万能的高斯消元来做。给出两种代码。
暴力代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
int main()
{
char c;
int a[][];
memset(a,,sizeof(a));
for(int i=;i<;i++)
for(int j=;j<;j++)
{
cin>>c;
if(c=='+')
{
for(int k=;k<;k++)
{
a[i][k]++;
a[k][j]++;
}
a[i][j]--;
}
}
int sum=;
for(int i=;i<;i++)
for(int j=;j<;j++)
if(a[i][j]%) sum++;
printf("%d\n",sum);
for(int i=;i<;i++)
for(int j=;j<;j++)
if(a[i][j]%)
printf("%d %d\n",i+,j+);
return ;
}
高消代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
#include <ctime>
using namespace std;
const int maxn=;
//有equ个方程,var个变元。增广矩阵行数为equ,列数为var+1,分别为0到var
int equ,var;
int a[maxn][maxn]; //增广矩阵
int x[maxn]; //解集
int free_x[maxn];//用来存储自由变元(多解枚举自由变元可以使用)
int free_num;//自由变元的个数
//返回值为-1表示无解,为0是唯一解,否则返回自由变元个数
int gauss()
{
int max_r,col,k;
free_num=;
for(k=,col=; k<equ&&col<var; k++,col++)
{
max_r=k;
for(int i=k+; i<equ; i++)
if(abs(a[i][col])>abs(a[max_r][col]))
max_r=i;
if(!a[max_r][col])
{
k--;
free_x[free_num++]=col;
continue;
}
if(max_r!=k)
for(int j=col; j<var+; j++)
swap(a[k][j],a[max_r][j]);
for(int i=k+; i<equ; i++)
{
if(a[i][col])
{
for(int j=col; j<var+; j++)
a[i][j]^=a[k][j];
}
}
}
for(int i=k; i<equ; i++)
if(a[i][col])
return -;
if(k<var) return var-k;
for(int i=var-; i>=; i--)
{
x[i]=a[i][var];
for(int j=i+; j<var; j++)
x[i]^=(a[i][j]&&x[j]);
}
return ;
}
int n;
void init()
{
memset(a,,sizeof(a));
memset(x,,sizeof(x));
equ=n*n;
var=n*n;
for(int i=; i<n; i++)
for(int j=; j<n; j++)
{
int t=i*n+j;
a[t][t]=;
for(int k=;k<n;k++)
{
a[t][k*n+j]=;
a[t][i*n+k]=;
}
}
}
int solve()
{
int t=gauss();
if(t==-)
{
return -;
}
else if(t==)
{
int ans=;
for(int i=; i<n*n; i++)
ans+=x[i];
return ans;
}
else
{
//枚举自由变元
int ans=0x3f3f3f3f;
int tot=(<<t);
for(int i=; i<tot; i++)
{
int cnt=;
for(int j=; j<t; j++)
{
if(i&(<<j)) //注意不是&&
{
x[free_x[j]]=;
cnt++;
}
else x[free_x[j]]=;
}
for(int j=var-t-; j>=; j--)
{
int idx;
for(idx=j; idx<var; idx++)
if(a[j][idx])
break;
x[idx]=a[j][var];
for(int l=idx+; l<var; l++)
if(a[j][l])
x[idx]^=x[l];
cnt+=x[idx];
}
ans=min(ans,cnt);
}
return ans;
}
}
char str[][];
int main()
{
n=;
for(int i=;i<;i++)
scanf("%s",str[i]);
init();
for(int i = ; i < ; i++)
for(int j = ; j < ; j++)
{
if(str[i][j] == '-')a[i*+j][] = ;
else a[i*+j][] = ;
}
int ans = solve();
printf("%d\n",ans);
for(int i=;i<n*n;i++)
{
if(x[i])
{
printf("%d %d\n",i/+,i%+);
}
}
return ;
}
POJ 2965 The Pilots Brothers' refrigerator的更多相关文章
- 枚举 POJ 2965 The Pilots Brothers' refrigerator
题目地址:http://poj.org/problem?id=2965 /* 题意:4*4的矩形,改变任意点,把所有'+'变成'-',,每一次同行同列的都会反转,求最小步数,并打印方案 DFS:把'+ ...
- 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 (dfs)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17450 ...
- 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 位运算枚举
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 151 ...
- 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(压位+bfs)
The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to op ...
- poj 2965 The Pilots Brothers' refrigerator枚举(bfs+位运算)
//题目:http://poj.org/problem?id=2965//题意:电冰箱有16个把手,每个把手两种状态(开‘-’或关‘+’),只有在所有把手都打开时,门才开,输入数据是个4*4的矩阵,因 ...
- POJ 2965 The Pilots Brothers' refrigerator (枚举+BFS+位压缩运算)
http://poj.org/problem?id=2965 题意: 一个4*4的矩形,有'+'和'-'两种符号,每次可以转换一个坐标的符号,同时该列和该行上的其他符号也要随之改变.最少需要几次才能全 ...
- POJ 2965 The Pilots Brothers' refrigerator【枚举+dfs】
题目:http://poj.org/problem?id=2965 来源:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=26732#pro ...
随机推荐
- spring 缓存(spring自带Cache)(入门)
spring的缓存机制,是方法纬度的缓存机制, 这就意味着我们并不用关注 底层是否使用了数据库以及通过什么方式访问的数据库: 因此,此缓存方法既适用于dao层,也适用于service层. spring ...
- Docker容器基础知识学习
Docker作为操作系统层面的轻量级的虚拟化技术,凭借简易的使用.快速的部署以及灵活敏捷的集成等优势,迅速发展目前最为火热的技术. 1.云计算服务是一种资源管理的资源服务,该模式可以实现随时随地.便捷 ...
- putchar和puts
#include<stdio.h> int main() { char a = 'h'; char b[] = "hello"; putchar(a); //putch ...
- 实体ip 虚拟ip 固定ip 动态ip
实体 IP:在网络的世界里,为了要辨识每一部计算机的位置,因此有了计算机 IP 位址的定义.一个 IP 就好似一个门牌!例如,你要去微软的网站的话,就要去『 207.46.197.101 』这个 IP ...
- APPCAN MAS接口之SOAP
APPCAN MAS接口中使用webservice接口形式,示例代码如下: 1 var MEAP=require("meap"); 2 3 function run(Par ...
- MapServer+TileCache+Apache+Python24 构建KS数据服务器
刚刚配置好TileCache,准备开工. 期间碰到多种配置的问题,罗列一下. 1.mod_python的一个最主要优点就是在性能上超越传统CGI.所以使用mod_python替代CGI.前提是安装好a ...
- Codeforces Round #340 Watering Flowers
题目: http://www.codeforces.com/contest/617/problem/C 自己感觉是挺有新意的一个题目, 乍一看挺难得(= =). 其实比较容易想到的一个笨办法就是:分别 ...
- Unity3D性能优化--- 收集整理的一堆
http://www.cnblogs.com/willbin/p/3389837.html 官方优化文档--优化图像性能http://docs.unity3d.com/Documentation/Ma ...
- Opencv SkinOtsu皮肤检测
void SkinRGB(IplImage* rgb, IplImage* _dst) { assert(rgb->nChannels == && _dst->nChann ...
- static小结
1.隐藏:编译多个文件时,所有未加static的全局变量.全局函数都具有全局可见性. 如果加了static,就会对其他源文件隐藏,利用这一特性可以在不同文件中定义相同的 变量名或函数名,而不用担心冲突 ...