poj1681 高斯消元

关于使用CMT2300A FIFO缓存区间设置为64Byte的问题 帮助你解决在使用CMT2300A 时遇到的各种问题,若有需要,请及时与我交流 阅读详情

Painter's Problem
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 5352 Accepted: 2588

Description

There is a square wall which is made of n*n small square bricks. Some bricks are white while some bricks are yellow. Bob is a painter and he wants to paint all the bricks yellow. But there is something wrong with Bob's brush. Once he uses this brush to paint brick (i, j), the bricks at (i, j), (i-1, j), (i+1, j), (i, j-1) and (i, j+1) all change their color. Your task is to find the minimum number of bricks Bob should paint in order to make all the bricks yellow. 

Input

The first line contains a single integer t (1 <= t <= 20) that indicates the number of test cases. Then follow the t cases. Each test case begins with a line contains an integer n (1 <= n <= 15), representing the size of wall. The next n lines represent the original wall. Each line contains n characters. The j-th character of the i-th line figures out the color of brick at position (i, j). We use a 'w' to express a white brick while a 'y' to express a yellow brick.

Output

For each case, output a line contains the minimum number of bricks Bob should paint. If Bob can't paint all the bricks yellow, print 'inf'.

Sample Input

2
3
yyy
yyy
yyy
5
wwwww
wwwww
wwwww
wwwww
wwwww

Sample Output

0
15


题意:

一个n*n 的木板,每个格子都可以染成白色和黄色,一旦我们对某个格子染色,其上下左右都将改变颜色

求将所有的格子染成黄色最少需要几次,若不能则输出inf。

当有多个自由变元时,需要进行枚举求出最小值

/*
poj1681
类似于一元开关问题。把二维矩阵转换成一维矩阵来看。
在求出答案之后,对于有很多解的考虑枚举自由变元来求解
最小值
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
typedef long double ld;

using namespace std;
const int maxn = 300;

int equ,var;
int a[maxn][maxn];
int x[maxn];
int free_x[maxn];
int free_num;

int Gauss()
{
    int max_r,col,k;
    free_num = 0;
    for(k = 0,col = 0; k < equ && col < var; k++,col++)
    {
        max_r = k;
        for(int i = k+1; i < equ; i++)
        {
            if(abs(a[i][col]) > abs(a[max_r][col]))
                max_r = i;
        }
        if(a[max_r][col] == 0)
        {
            k --;
            free_x[free_num++] = col;
            continue;
        }
        if(max_r != k)
        {
            for(int j = col; j < var+1; j++)
                swap(a[k][j],a[max_r][j]);

        }
        for(int i = k + 1; i < equ; i++)
        {
            if(a[i][col] != 0)
            {
                for(int j = col; j < var+1; j++)
                    a[i][j] ^= a[k][j];
            }
        }

    }
    for(int i = k; i < equ; i++)
        if(a[i][col] != 0)
            return -1;
    if(k < var) return var-k;

    for(int i = var-1; i >= 0; i--)
    {
        x[i] = a[i][var];
        for(int j = i +1; j < var; j++)
            x[i] ^= (a[i][j] && x[j]);

    }
    return 0;

}

int n;
void ini()
{
    memset(a,0,sizeof(a));
    memset(x,0,sizeof(x));
    equ = n*n;
    var = n*n;
    for(int i = 0;i < n;i++)
    {
        for(int j = 0;j < n;j++)
        {
            int tt = i*n+ j;
            a[tt][tt] =1;
            if(i > 0) a[(i-1)*n+j][tt] = 1;
            if(i < n-1) a[(i+1)*n+j][tt] = 1;
            if(j > 0) a[tt-1][tt] = 1;
            if(j < n-1) a[tt+1][tt] =1;
        }
    }
}

char str[30][30];
/*
2
3
yyy
yyy
yyy
5
wwwww
wwwww
wwwww
wwwww
wwwww
*/
int main()
{
    int T;
    char color;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        ini();
        for(int i = 0; i < n; i++)
        {
            scanf("%s",str[i]);
            for(int j = 0; j < n; j++)
            {
                if(str[i][j] == 'y')
                    a[i*n+j][n*n] = 0;
                else
                    a[i*n+j][n*n] = 1;
            }
        }
        int t = Gauss();
        if(t == -1)
        {
            printf("inf\n");
        }
        else if(t == 0)
        {
            int ans = 0;
            for(int i = 0; i < n*n; i++)
                ans += x[i];
            printf("%d\n",ans);
        }
        else
        {
            int ans = 0x3f3f3f3f;
            int tot = (1 << t);
            for(int i = 0; i < tot; i++)
            {
                int cnt = 0;
                for(int j = 0; j < t; j++)
                {
                    if(i & (1 << j))
                    {
                        cnt ++;
                        x[free_x[j]]= 1;
                    }
                    else x[free_x[j]]= 0;
                }

                for(int j = var-t-1; j >= 0; j--)
                {
                    int dex;
                    for(dex = j; dex < var; dex++)
                        if(a[j][dex])
                            break;
                    x[dex] = a[j][var];
                    for(int l = dex +1; l <var ; l++)
                    {
                        if(a[j][l])
                            x[dex] ^= x[l];
                    }
                    cnt += x[dex];
                }
                ans = min(ans,cnt);
            }
            printf("%d\n",ans);
        }
    }
    return 0;
}



C# 与华中数控设备通信:数控机床自动化控制与实时监测系统 华中数控系统与C#通信开发指南 核心要点 通信基础 支持串口(RS232/RS485)和以太网两种连接方式 协议选择:通用Modbus协议或华中自定义HNC协议 关键数据映射表明确各参数对应的寄存器地址和功能码 系统架构 四层架构:设备层→通信层→业务层→应用层 包含实时数据采集、指令下发、状态监控、异常处理全流程 支持Modbus和HNC协议动态切换 代码实现 串口通信使用NModbus库简化开发 提供连接管理、数据读取、指令下发完整示例 包含异常处理和超时重连机制 应用价值 实现数控机床远程监控和自动化 阅读详情

相关推荐

告别交越失真:手把手教你设计高性能互补输出级电路(LTspice实测)

本文深入剖析音频放大器中常见的交越失真问题,并提供基于LTspice仿真的实战解决方案。通过对比二极管偏置、Vbe倍增器和集成运放驱动三种经典方案,手把手指导设计高性能互补输出级电路,涵盖原理分析、仿真验证及实物调试要点,助力消除音频失真,提升音质。

weixin_29190573的博客 551

POJ-1222 EXTENDED LIGHTS OUT(高斯消元)

题意: 给你一个5*6的矩阵,每个点上都有一个灯,按下f[i][j]的按钮,f[i][j]位置的灯的状态会改变,它上下左右的灯的状态也会改变(开变关,关变开)。 现在给出这个矩阵的初始状态,输出按下哪些按钮,使所有的灯都关闭。 做法: 利用高斯消元解异或方程组。 用x[i][j]表示第i行第j列的灯是否被按下 x[i][j] ^  x[i+1][j] ^  x[i-1][j]

GoldMoon 1618

TDISP(TEE Device Interface Security Protocol)软件操作流程

/ 1. SPDM会话相关定义(🔶1-37、🔶1-187)// SPDM会话ID// AES-256-GCM加密密钥(TDISP要求)// 初始向量// 会话是否激活// 2. IDE流配置(🔶1-52、🔶1-625)// IDE流ID// 4个子流的AES密钥(覆盖所有子流类型)// 密钥是否已配置// 3. TDI状态与配置(🔶1-207、🔶1-210)TDI_CONFIG_UNLOCKED = 0, // 初始配置态。

inquisiter 1429

POJ 2065-SETI(高斯消元求解同余方程式)

题目地址:POJ 2065 题意:输入一个素数p和一个字符串s(只包含小写字母和‘*’),字符串中每个字符对应一个数字,'*'对应0,‘a’对应1,‘b’对应2....            eg:str[] = "abc", 那么说明 n=3, 字符串所对应的数列为1, 2, 3。            同时题目定义了一个函数:a0*1^0 + a1*1^1+a2*1^2+........

Rocky0429 2323

如何使用AKShare作为OpenBB的数据源

`openbb_akshare`项目是 OpenBB 的数据源扩展。可以用于查询A股和港股数据

FinAnalyzer金融分析工具 3195

history 历史命令记录功能如何防坏人(高级)

在 Linux 下面可以使用 history 命令查看用户的所有历史操作,同时 shell 命令操作记录默认保存在用户目录的 .bash_history 文件中。通过这个文件可以查询 shell 命令的执行历史,有助于运维人员进行系统审计和问题排查,同时在服务器遭受黑客攻击后,也可以查询黑客登录服务器的历史命令操作。但是黑客在入侵后,为了抹除痕迹,会删除 .bash_history 文件,这个就需...

墨痕诉清风的博客 1655

POJ 1166(Gauss消元 取模)

Problem |——-| |——-| |——-| | | | | | | | |—O | |—O | | O | | | | | | | |——-| |——-| |——-| ​ A B C ...

Feynman1999的博客 655

华为OD面试八股真题 - 操作系统相关 - 2

华为OD技术面,操作系统相关八股真题提供详细解答,这篇博客主要涉及于进程和线程相关知识点

qq_45776114的博客 965

poj 1681 Painter's Problem 【高斯消元 + 状压枚举自由变元】

Painter's Problem Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5075   Accepted: 2458 Description There is a square wall which is made of n*n small square

世界很大 878

【100%通过率】华为od机试C卷【贪吃的猴子】Java&Go 实现

一只贪吃的猴子,来到一个果园,发现许多串香蕉排成一行,每串香蕉上有若干根香蕉。每串香蕉的根数由数组numbers给出。猴子获取香蕉,每次都只能从行的开头或者未尾获取,并且只能获取N次,求猴子最多能获取多少根香蕉。第一次获取香蕉为行的开头,第二次获取为行的末尾,因此最终根数为4+3 =7。全部获取所有的香蕉,因此最终根数为1+2+3 = 6。第二行为数组numbers的值每个数字通过空格分开。1:简单题目,两个指针分别指向头尾,逐个对比即可。第一行为数组numbers的长度。第三行输入为N,表示获取的次数。

MISAYAONE的博客 1435

常用元器件使用方法13:物理层接口芯片DP83848的使用方法

介绍: DP83848是以太网物理层接口芯片,具有多种型号,从便于焊接角度考虑LQFP-48封装较为合适,采用LQFP-48封装的芯片,有2种型号,分别为DP83848I和DP83848C,如下图所示。 其主要区别是使用温度不同,如下图所示。 这2种芯片芯片网上均有销售,按理说DP83848C价格应该更加便宜,实际不然,DP83848I的价格低于DP83848C,考虑或与芯片的出货量...

snmplink的博客 1万+

Painter's Problem【高斯消元】

Description There is a square wall which is made of n*n small square bricks. Some bricks are white while some bricks are yellow. Bob is a painter and he wants to paint all the bricks yellow. But th

zhang_di233的专栏 504

【SystemVerilog基础】SystemVerilog Assertion 概述

断言主要是描述复杂的设计行为;是传统验证方法学一个很好的补充,可以检查到电路内部细节,相当于一种白盒验证,传统的验证方法学是纯粹的黑盒验证!property跟上面的sequence差别不大,唯一有差别可能多了两个implication(|->,|=>)注意,当有前置条件时,不满足前置条件不会fail,而是伪真!上面这个因为没有前置条件,每个周期都会去检查!注意:当没有前置条件时,不满足条件会直接fail!

MoorePlus的博客 331

poj1681(高斯消元)

题目链接:http://poj.org/problem?id=1681 题目分析:本题是有的存在自由变元的高斯消元,而且存在无解的情况,还是有一定难度的; 代码如下: #include #include #include #include #include #include #include #include #include #include #include #in

北门的智慧 1640

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

poj1681-Painter's-Problem ( 高斯消元)

题目链锁 : http://poj.org/problem?id=1681 #include #include #include #include #include #include #include #include #include #include using namespace std; #define inf 0x3f3f3f3f #define eps

To_c_t_w 381

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

POJ.1681 Painter's Problem(高斯消元 异或方程组)

POJ.1681 Painter’s Problem(高斯消元 异或方程组)标签(空格分隔): 高斯消元题意分析和 POJ.1222 EXTENDED LIGHTS OUT 几乎一样,区别只是规定了维数,不再是5×65×6的矩阵,而是n×nn×n的矩阵。并且直接输出需要修改的次数即可。 需要注意的是,此题多了一个判断无解的情况,即无解时要输出infinf,做到这点也很容易,直接判断Gauss()G

Pengwill's Blog 406

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

POJ 1681 Painter's Problem 高斯消元 枚举自由变量

题目链接:http://poj.org/problem?id=1681和POJ 1753 一样代码:#include <iostream> #include <cstring> #include <algorithm> #include <cstdio> #define sf scanf #define pf printf using namespace std; const int maxn =

SolarDomo的博客 558

Painter's Problem POJ1681 高斯消元

传送门:POJ1681 题意:给定n*n矩阵,

WA是一笔财富 320

POJ1681 Painter's Problem 高斯消元+枚举

题目链接:http://poj.org/problem?id=1681 题目大意:有一个n*n的砖块,可以涂成白色或黄色,每次对格子(i,j)染色时会时它上下左右的四个格子也染上色,已知砖块的起始状态,求最少需要多少次染色,才能使整个砖块变成黄色。 分析:高斯消元,对于解不唯一的情况,枚举所有可能的解,逐一计算即可。 实现代码如下: #include #inclu

AC_Gibson的专栏 567

poj 1681(高斯消元。。。)

再次认真学习高斯消元。。。。感觉还有好多不会处理的。。。自由基的处理还没遇到(discuss里说不用处理也能过的。。。就没处理。。参照了别人的模板。。。自己写的很撮)。。。。收获很多。。。继续ing。。。。 #include #include #include usin

水杯 2200

poj 1681 Painter's Problem(高斯消元)

http://poj.org/problem?id=1681 求最少经过的步数使得输入的矩阵全变为y。 思路:高斯消元求出自由变元,然后枚举自由变元,求出最优值。 注意根据自由变元求其他解及求最优值的方法。 #include #include #include #include #include #include #include #define LL

1980

POJ 1681 Painter's Problem (高斯消元)

题目地址:POJ 1681 跟前两题几乎一模一样的。。。不多说了。高斯消元+自由元枚举。 代码如下: #include #include #include #include #include #include #include #include #include using namespace std; #define LL __int64 #define pi acos(

scf0920 1077

poj 1681 Painter's Problem 【高斯消元 枚举自由变元】

链接:http://poj.org/problem?id=1681 题意:与poj1222相似 输出变为求最小步数。 分析:由于求最小步数,当我们存在自由变元时,我们可以得到解,但是不知道需要的最小步数,所以我们枚举所有的变元。 代码: #include #include #include #include #include #include #include #include #incl

Fb_by 644

Poj--1681(数学,高斯消元)

2014-09-1700:29:08 思路:和poj1222很像,建立n×n个方程,高斯消元即可,注意还要判断无解的情况。(一开始不会判无解Wrong了一发,QAQ) 1 /************************************************************************* 2 3 > File Name:...

dingdi3021的博客 105

poj1681 高斯消元+dfs枚举

wa  N多次后,终于把高斯消元的模板给弄出来了,并且精简了许多。 用dfs枚举变元比二进制枚举变元简单多了。 (也终于明白 变元代表只得解的个数。) 上模板。 #include #include using namespace std; int a[400][400]; int ans[400],x[400];//ans[]用于记录哪几种情况。x[]保存结果。 int equ,v

wishcxz_acm 1051

poj1681 Painter's Problem 高斯消元

Language: Default Painter's Problem Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4398   Accepted: 2134 Description There is a square wall which is mad

AC_kereo的专栏 842
上一篇: poj 1830 高斯消元
下一篇: poj 1222 高斯消元
Tyuio28
博客等级 码龄11年 12粉丝 279原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值