题目链接

题意:一个冰箱上有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的更多相关文章

  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 (dfs)

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

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

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

  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 (DFS)

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

  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的矩形,有'+'和'-'两种符号,每次可以转换一个坐标的符号,同时该列和该行上的其他符号也要随之改变.最少需要几次才能全 ...

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

随机推荐

  1. RelativeLayout布局

    RelativeLayout用到的一些重要的属性: 第一类:属性值为true或falseandroid:layout_centerHrizontal 水平居中android:layout_center ...

  2. C#3.0 特性

    C#3.0特性 隐式类型的本地变量和数组 对象初始值设定项 集合初始值设定项 扩展方法 匿名类型 lambda表达式 查询关键字 自动实现的属性 分布方法定义 lambda表达式与表达式树 https ...

  3. 《深入浅出WPF》笔记一

    1.项目模板 Visual Studio自动配置编译器参数,并准备好一套基本的源代码. 2.App.xaml/App.xaml.cs 声明程序的进程,并指定程序的主窗体. 3.Attribute和Pr ...

  4. 计算字符数组长度,用strlen 与 sizeof 的原理与区别

    遇到个坑,定义了一个字符数组 unsigned ;i<;i++) { buff[i] = ; } 然后用串口发送函数: write(fd, buff, strlen(buff)); 却发现串口一 ...

  5. shell简单使用

    最近需要用到shell脚本实现关机保护作用,总结下语法 要点: 1.linux下编写的shell脚本不能在window下编写,否则会出现^M的错误,用window编写保存,在linux用vim打开,每 ...

  6. servlet和http请求

    1.servlet servlet是和平台无关的服务器组件,可以交互式的来浏览和修改数据,生成动态的web内容.它运行于 servlet容器中2.servlet容器 servlet容器负责servle ...

  7. jQuery Ajax无刷新操作一般处理程序 ashx

    //前台实例代码 aspx文件 <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="ser ...

  8. 在C语言源程序中的格式字符与空格等效

    #include <stdio.h> #\ i\ n\ c\ l\ u\ d\ e \ <\ s\ t\ d\ l\ i\ b\ .\ h\ > /* *预处理指令这里换行符会 ...

  9. vncserver和Ubuntu Xfce4远程桌面环境的配置,解决不显示图形界面

    vncserver和Ubuntu Xfce4远程桌面环境的配置 参考的http://blog.163.com/thinki_cao/blog/static/8394487520130301453180 ...

  10. [POJ2109]Power of Cryptography

    [POJ2109]Power of Cryptography 试题描述 Current work in cryptography involves (among other things) large ...