题目链接

题意:一个冰箱上有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. vector族函数

     本文原创,转载请注明出处,本人Q1273314690 vector(mode = "logical", length = 0) as.vector(x, mode = " ...

  2. 关于QString::toWCharArray 无法解析的外部符号

    1>CommManger.obj : error LNK2019: 无法解析的外部符号 "public: int __thiscall QString::toWCharArray(un ...

  3. PHP基础OOP(一)

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  4. CodeForces 55D Beautiful numbers

    D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  5. html 图像映射

    个人先做而一个例子 <body> <img src="图像映射/enterdesk.com-D69055E2B422567CB273963EA05FF7D4.jpg&quo ...

  6. Windows下几个常用的和进程有关的命令

    在windows下,进入cmd,有几个常用的和进程有关的命令: netstat -ano:查看所有进程 netstat -ano|findstr  “端口号”:查看端口号对应的进程PID taskli ...

  7. DAY3 python群发短信

    手机轰炸,burpsuit 抓取注册页面输入的手机号,然后每点击一次forword ,都开开始放行,发短信.也可以发到repeat 里面进行 ,重复发送短信. import requests impo ...

  8. Sound Generator 原理

    Sound Generator 原理 旨在简单的阐述声音如何通过单片机模块来产生. 声音 声音的种类有千千万,但归根到底还是属于波.在对声音进行模拟转数字的时候,采样越高, 声音被还原的越逼真. 声音 ...

  9. Tomcat无法启动问题

    检查环境变量设置 再打开一个cmd, 1.输入if not exist "%JRE_HOME%\bin\java.exe" echo no jre java 回车    结果:no ...

  10. Python学习笔记一

    原来Python的文件配置好环境变量直接双击就可以运行,当然也可以控制台+编辑器 first try: import turtle window=turtle.Screen() babbage=tur ...