题目和poj1222差不多,但是解法有一定区别,1222只要求出任意一解,而本题需要求出最少翻转次数。所以需要枚举自由变元,变元数量为n,则枚举的次数为1<<n次

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=500;
char s[maxn][maxn];
int a[maxn][maxn],x[maxn],fre[maxn];
const int INF=1e9; void debug(int n)
{
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
printf("%d ",a[i][j]);
printf(" %d\n",a[i][n]);
}
} void init(int n)
{
int dx[]= {0,0,-1,0,1};
int dy[]= {0,-1,0,1,0};
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
for(int k=0; k<5; k++)
{
int x=i+dx[k];
int y=j+dy[k];
if(x>=0 && x<n && y>=0 && y<n)
a[i*n+j][x*n+y]=1;
}
}
}
} int Guass(int equ,int var)
{
int k,col,num=0;
for(k=0,col=0; k<equ && col<var; k++,col++)
{
int rr=k;
for(int i=k; i<equ; i++)
if(a[i][col]!=0)
{
rr=i;
break;
}
if(rr!=k)
for(int j=col; j<var+1; j++)
swap(a[k][j],a[rr][j]);
if(a[k][col]==0)
{
k--;
fre[num++]=col;
continue;
}
for(int i=k+1; i<equ; i++)
{
if(a[i][col]==0) continue;
for(int j=col; j<var+1; j++)
a[i][j]^=a[k][j];
}
}
//debug(equ);
for(int i=k; i<equ; i++)
if(a[i][var]!=0) return -1;
int sta=1<<(var-k);//自由变元有var-k个
int res=INF;
for(int i=0; i<sta; i++) //枚举所有变元
{
int cnt=0;
int index=i;
for(int j=0; j<num; j++)
{
x[fre[j]]=(index&1);
if(x[fre[j]]) cnt++;
index>>=1;
}
for(int row=k-1; row>=0; row--)
{
x[row]=a[row][var];
for(col=row+1; col<var; col++)
x[row]^=(a[row][col]*x[col]);
if(x[row])cnt++;
}
res=min(cnt,res);
}
return res;
} int main()
{
//freopen("in.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--)
{
memset(a,0,sizeof(a));
memset(fre,1,sizeof(fre));
int n;
scanf("%d",&n);
for(int i=0; i<n; i++)
scanf("%s",s[i]);
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
a[i*n+j][n*n]=(s[i][j]=='y'? 0:1);
init(n);
int ans=Guass(n*n,n*n);
if(ans==-1)
{
printf("inf\n");
continue;
}
printf("%d\n",ans);
}
return 0;
}

POJ 1681 高斯消元 枚举自由变元的更多相关文章

  1. POJ 1681 Painter's Problem(高斯消元+枚举自由变元)

    http://poj.org/problem?id=1681 题意:有一块只有黄白颜色的n*n的板子,每次刷一块格子时,上下左右都会改变颜色,求最少刷几次可以使得全部变成黄色. 思路: 这道题目也就是 ...

  2. POJ 1681 Painter's Problem (高斯消元 枚举自由变元求最小的步数)

    题目链接 题意: 一个n*n 的木板 ,每个格子 都 可以 染成 白色和黄色,( 一旦我们对也个格子染色 ,他的上下左右 都将改变颜色): 给定一个初始状态 , 求将 所有的 格子 染成黄色 最少需要 ...

  3. POJ 1753 Flip Game (高斯消元 枚举自由变元求最小步数)

    题目链接 题意:4*4的黑白棋,求把棋全变白或者全变黑的最小步数. 分析:以前用状态压缩做过. 和上题差不多,唯一的不同是这个终态是黑棋或者白棋, 但是只需要把给的初态做不同的两次处理就行了. 感觉现 ...

  4. POJ 1222 POJ 1830 POJ 1681 POJ 1753 POJ 3185 高斯消元求解一类开关问题

    http://poj.org/problem?id=1222 http://poj.org/problem?id=1830 http://poj.org/problem?id=1681 http:// ...

  5. POJ 3185 The Water Bowls(高斯消元-枚举变元个数)

    题目链接:http://poj.org/problem?id=3185 题意:20盏灯排成一排.操作第i盏灯的时候,i-1和i+1盏灯的状态均会改变.给定初始状态,问最少操作多少盏灯使得所有灯的状态最 ...

  6. poj1222 EXTENDED LIGHTS OUT 高斯消元||枚举

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8481   Accepted: 5479 Description In an ...

  7. POJ SETI 高斯消元 + 费马小定理

    http://poj.org/problem?id=2065 题目是要求 如果str[i] = '*'那就是等于0 求这n条方程在%p下的解. 我看了网上的题解说是高斯消元 + 扩展欧几里德. 然后我 ...

  8. POJ 2065 高斯消元求解问题

    题目大意: f[k] = ∑a[i]*k^i % p 每一个f[k]的值就是字符串上第 k 个元素映射的值,*代表f[k] = 0 , 字母代表f[k] = str[i]-'a'+1 把每一个k^i求 ...

  9. *POJ 1222 高斯消元

    EXTENDED LIGHTS OUT Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9612   Accepted: 62 ...

随机推荐

  1. python3.7[列表] 索引切片

    python3.7[列表] 索引  切片 排序     #### 列表.sort 永久排序   sorted(列表) 临时排序   ### >>> print(sorted(a))[ ...

  2. Select Screen 0 with xrandr Ask QuestionScreen 0" here describes your whole virtual display made of these two outputs: eDP-1-

    Screen 0" here describes your whole virtual display made of these two outputs: eDP-1-1: physica ...

  3. Boa Web Server 缺陷报告及其修正方法

    综述 Boa 作为一种轻巧实用的 WEB 服务器广泛应用于嵌入式设备上, 但 Boa 对实现动态网页的 CGI  的支持上仍存在一些缺陷, 本文描述了 Boa 对 CGI 的 Status/Locat ...

  4. IDEA 快速上手指南(全配置)(Day_23)

    Idea快速入门指南 1.安装 1.1.安装 我们使用的是2017.3.4版本: 双击打开, 选择一个目录,最好不要中文和空格: 然后选择桌面快捷方式,请选择64位: 然后选择安装: 开始安装: 然后 ...

  5. 【Azure 应用服务】由 Azure Functions runtime is unreachable 的错误消息推导出 ASYNC(异步)和 SYNC(同步)混用而引起ThreadPool耗尽问题

    问题描述 在Azure Function Portal上显示: Azure Functions runtime is unreachable,引起的结果是Function App目前不工作,但是此前一 ...

  6. Git如何下载clone指定的tag

    Git如何下载clone指定的tag 如上图,我想下载Tags标签为solution-4 的代码,如何处理呢? 命令如下: git clone --branch solution-4 git@gith ...

  7. 固态LiDAR,半固态混合LiDAR,机械LiDAR

    固态LiDAR,半固态混合LiDAR,机械LiDAR 1. APD/SPAD 2轴MEMS扫描镜+ SPAD图像传感器在混合固态LiDAR中的应用 APD的工作模式分为线性模式和盖革模式两种.当APD ...

  8. ML Pipelines管道

    ML Pipelines管道 In this section, we introduce the concept of ML Pipelines. ML Pipelines provide a uni ...

  9. 3D-LaneNet:端到端三维多车道检测ICCV2019

    3D-LaneNet:端到端三维多车道检测ICCV2019 3D-LaneNet: End-to-End 3D Multiple Lane Detection 论文链接: http://openacc ...

  10. 目标检测中特征融合技术(YOLO v4)(上)

    目标检测中特征融合技术(YOLO v4)(上) 论文链接:https://arxiv.org/abs/1612.03144 Feature Pyramid Networks for Object De ...