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

华为OD干了两个月,还是跑了 ​本人工作了快13年,base南京。2024年2月份由于公司业务变动第一次拿到了公司发的大礼包,同时也面临着失业的问题,我对自己的技术还是很有信心的,但是,找了大概有一个多月时间,发现心意的工作的确不太好找,当然收到挺多家offer,就是薪资不太满意,外包也一直不想去,不过说实话外包给的薪资普遍要比自研的单位要高一些,最后为了钱还是进华为(既然选择外包那就选择好一些的吧),毕竟中年老男人工作不能断太久。 阅读详情
Painter's Problem
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 3462 Accepted: 1710

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

Source

╮(╯▽╰)╭,因为数组开的不够大RE了几次
 
 
#include<iostream>
#include<cstring>
using namespace std;
const int mm=937;
int map[mm][mm+1];
int ans[mm];
int var,equ;
int max_ans;
void debug();
void swap(int&a,int &b)
{
  int tmp;tmp=a;a=b;b=tmp;
}
/*
int abs(int x)
{
  if(x<0)return -x;return x;
}*/
int gcd(int a,int b)
{
  int t;
  while(b!=0)
  {
    t=b;b=a%b;a=t;
  }return a;
}
int lcm(int a,int b)
{
  return a*b/gcd(a,b);
}
void dfs(int z,int dep,int k,int kans)
{
  ans[var-dep]=z;
  if(dep==k)
  {
    for(int i=var-k-1;i>=0;i--)
    {
    int tmp;
    tmp=map[i][var]%2;
    for(int j=i+1;j<var;j++)
    if(map[i][j])
    tmp=(tmp-(map[i][j]*ans[j])%2+2)%2;
    ans[i]=(tmp/map[i][i])%2;if(ans[i])++kans;
    }
    if(kans<max_ans)max_ans=kans;
    //cout<<"ans="<<kans<<endl;
  }
  else
  {
     dfs(1,dep+1,k,kans+1);dfs(0,dep+1,k,kans);
  }
}
int Guss()
{
  int k,col;;
  for(k=0,col=0;k<equ&&col<var;k++,col++)
  {/**行变换消元*/
    int max_i=k;
    for(int i=k+1;i<equ;i++)
    if(map[i][col]>map[max_i][col])max_i=i;
    if(max_i!=k)
    {//for(int i=k;i<var+1;i++)好像也OK
      for(int i=col;i<var+1;i++)//是K不是col因为k--;
      swap(map[k][i],map[max_i][i]);
    }
    /**除去对角线0,列变换*/
for(int i = 0; i <equ; ++i)//每一行主元素化为非零
if(!map[i][i])
{
int j;
for(j = i+1;j<var;++j)
if(map[i][j])
break;
if(j == var)
break;
for(int kk = 0;kk < equ; ++kk)
swap(map[kk][i],map[kk][j]);
}
/**遇到对角线0,k退1*/
    if(map[k][col]==0)
    {
      k--;continue;
    }
    /**消元*/
    for(int i=k+1;i<equ;i++)
    if(map[i][col]!=0)
     {
      int LCM=lcm(map[k][col],map[i][col]);
      int ta,tb;
      ta=LCM/map[i][col];tb=LCM/map[k][col];
      if(map[i][col]*map[k][col]<0)tb=-tb;
      for(int j=col;j<var+1;j++)
      {
        map[i][j]=((map[i][j]*ta)%2-(map[k][j]*tb)%2+2)%2;
      }
     }
    // cout<<"k="<<k<<endl;
  }
  //debug();
  ///判断回代
  for(int i=k;i<equ;i++)
  if(map[i][col])return -1;
  if(var==k)
  for(int i=k-1;i>=0;i--)
  {
    int tmp;
    tmp=map[i][var]%2;
    for(int j=i+1;j<var;j++)
    if(map[i][j])
    tmp=(tmp-(map[i][j]*ans[j])%2+2)%2;
    ans[i]=(tmp/map[i][i])%2;if(ans[i])max_ans++;
  }
  else
  {    max_ans=999999;
      dfs(1,1,var-k,1);dfs(0,1,var-k,0);
  }
  return 0;
}
void debug()
{
  for(int i=0;i<var;i++)
  {for(int j=0;j<var+1;j++)
  cout<<map[i][j];
  cout<<"\n";
  }
}
int main()
{
  int cas,num;
  cin>>cas;
  char s;
  while(cas--)
  {memset(map,0,sizeof(map));
  memset(ans,0,sizeof(ans));
  max_ans=0;
    cin>>num;
    var=equ=num*num;
    for(int i=0;i<num;i++)
    {
      for(int j=0;j<num;j++)
      {  map[i*num+j][i*num+j]=1;
        if(i!=0)map[i*num+j][(i-1)*num+j]=1;
        if(j!=0)map[i*num+j][i*num+j-1]=1;
        if(i!=num-1)map[i*num+j][(i+1)*num+j]=1;
        if(j!=num-1)map[i*num+j][i*num+j+1]=1;
        cin>>s;
        if(s=='y')map[i*num+j][var]=0;
        else map[i*num+j][var]=1;
        //cout<<map[i*num+j][var];
        //cout<<"i="<<i<<"j="<<j<<endl;
      }
    }
    int flag=Guss();
    //debug();
    if(flag==-1)cout<<"inf\n";
    else
    {
      cout<<max_ans<<"\n";
    }
  }
}

句向量(句嵌入)模型sent2vec的训练及应用 本文讨论NLP中的句向量模型(sent2vec),也就是将一个完整的句子映射成一个实数向量的技术。这方面已经发表的成果也是比较多的,本文中所涉及的技术主要源自NAACL 2018的一篇论文,其中提出的sent2vec模型可以理解为词嵌入技术中经典的CBOW方法的扩展,该所谓的扩展中一个重要改进就是引入了n-gram 阅读详情

相关推荐

VirtualBox增强功能安装全指南:从下载到配置复制粘贴功能

本文详细介绍了VirtualBox增强功能的完整安装与配置流程,重点解决了虚拟机与主机间复制粘贴、文件拖放等核心协作难题。通过分步指南和故障排查,帮助用户在不同操作系统(如Windows、Linux)中实现无缝集成,提升虚拟化工作效率与体验。

weixin_29206669的博客 434

poj1681

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

◆◇丶生如夏花。谁来订阅我的悲伤 726

Linux:在使用UEFI固件的计算机上内核是如何被启动的

启动计算机通常不是一件难事:按下电源键,稍等片刻,你就能看到一个登录界面,再输入正确的密码,就可以开启一天的网上冲浪之旅了。但偶尔这件事没那么顺利,有时候迎接你的不是熟悉的登录界面,而是一个令人生畏的命令提示符界面,一闪一闪的提示符告诉你:“你碰到麻烦了”。于是你对着错误提示查找解决方法,按照网页上的步骤,你对着提示符输入并执行了几条你完全不理解的命令,计算机又能正常启动了,但同时你发现你那存有大学舍友糗照的硬盘分区被清空了。

imred的专栏 3073

POJ 1681 Painter's Problem(画家问题)

总时间限制: 1000ms 内存限制: 65536kB 描述有一个正方形的墙,由N*N个正方形的砖组成,其中一些砖是白色的,另外一些砖是黄色的。Bob是个画家,想把全部的砖都涂成黄色。但他的画笔不好使。当他用画笔涂画第(i, j)个位置的砖时, 位置(i-1, j)、 (i+1, j)、 (i, j-1)、 (i, j+1)上的砖都会改变颜色。请你帮助Bob计算出最少需要涂画多少块砖,才能使所有砖的

不若乘风来 2456

NetApp 扩展柜 Disk Shelf 扩容方法步骤

本文详细介绍了NetApp扩容磁盘柜的方法和注意事项,并用DS2246作为例子做了主要步骤的描述。

存储服务专家 2479

SAP 分期付款,付款条件的要件定义方法

以上内容做好后,可以在F-02或者F-60里看设置后的结果。找到相关路径选择【维护支付条件】点击进入后,选择【新条目】再设置一个总的支付规则,注意这里【付款条款】一定要选择。例:默认付款条件SP00,改成刚定义好的0028即可。将刚才基础创建的付款条件,以以下方式录入,注意。028A- 30天到期,过账日期基础。028B- 60天到期,过账日期基础。028C- 90天到期,过账日期基础。目的:新建支付条款(本次以。的选择,然后保存后,完成。,【基准日期缺省值】选择。基础创建好了以后,选择【

晴天的博客分享技术知识 2948

Painter's Problem POJ1681

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

天堂人间 2261

【Python程序开发】湖南省职业院校技能大赛python程序开发赛项样题二

【Python程序开发】湖南省职业院校技能大赛python程序开发赛项样题二

况老师话竞赛&blog 2254

Painter's Problem (高斯消元)

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

weixin_30876945的博客 194

后悔没早点知道!用AI只需四步,一次性讲清高质量论文引言写法(附公式+AI指令)

帮我写SCI 论文撰写引言的“研究背景与重要性”,要求从本研究所属领域的小背景切入,使用现在时态描述该领域的总体状况与发展趋势,突出其在学术研究、实际应用或社会层面的重要价值,并通过一个吸引人的钩子开头,如关键事实、现实问题或学术热点,引导读者关注本主题的重要性。梳理你所在领域和你研究相关的关键成果,但不能罗列所有文献,重点抓核心脉络,这部分最重要的实际上是让你了解别人的研究脉络,然后在每个关键的节点上,引用几篇关键参考文献作支撑,来体现你对领域的熟悉度就可以了。3)研究缺口模糊,没法突出问题的真实价值;

qq_64378896的博客 130

华为ensp、思科eve、华为ensp pro中通过cloud桥接本地网络和联通互联网的方法

华为ensp、思科eve中通过cloud桥接本地网络和联通互联网的方法

suofen9703的博客 485

POJ1681:Painter's Problem(高斯消元 & 格子转换 & 二进制)

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

junior19的博客 392

电脑技巧:Hyper-V安装UOS操作系统图文教程

今天给大家分享微软官方Hyper-v虚拟机管理软件安装UOS操作系统图文教程,感兴趣的朋友也可以下载试一试,当然有啥问题也可以互相沟通交流!

IT技术分享社区 4711

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的专栏 505

温度PID控制PLC程序

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

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

Painter's Problem POJ - 1681

题意: 涂色,每涂一个格子就会将其临近的格子都改变,黄的变成白的,白的变成黄的, 给出画板初始情况,问最少多少次操作可以使得画板全部变成黄色 思路: 消元后,自由变元的取值情况的变化会造成后边的解的变化 剩下最后n个变元,遍历每种取值情况后,回溯找到解中操作次数最少的 #include #include #include #include using namespace std; co

a7f650ebd327889c的博客 484

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

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

POJ - 1681 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 the...

FIVE 390

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

POJ 1681 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 ther

小坏蛋_千千 729

POJ1681-Painter's Problem[十字枚举]

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

vCodes的博客 627

POJ1681-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

豆腐脑是咸的 495

【POJ1681】Painter's Problem 高斯消元,求最小∑系数的异或方程组

-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 题意: 多组数据、 有个n*n的正方形,然后你要对某些位置进行操作,使得最后灯的状态都变成y。 操作:这个灯位置的上下左右以及自己这五盏灯状态都取反。 然后求最小操作次数。 -+-+-+-+-+-+-+-+-+-+-+-+-+-+

辗转山河弋流歌 1658

POJ1681 Painter's Problem

总时间限制:5000ms内存限制:65536kB 描述 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 bri...

weixin_30609331的博客 144

[POJ1681]Painter's Problem(高斯消元+dfs)

题目描述传送门 题意:同POJ1222,如果有多解,输出修改点数最少的,如果无解,输出inf题解和POJ1222很类似,只需要再判断多解和无解的情况 如果出现自由元的话,就看它后面消出来的如果和b相等,那么就是有多解,如果和b不相等就是无解 因为要输出点数最少的,所以需要将所有的自由元的取值指数级暴搜一下 bitset传参不知道为什么老是炸掉,所以干脆回溯了…代码#include<algor

zyf2000 644

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

世界很大 880

POJ1681——Painter\'s Problem

DescriptionThere 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 the

Alex 799

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

Description 一个n*n的棋盘,可以给任一格子染成黄色,但染某个格子时会让其上下左右的格子也变成黄色,现给出一个棋盘的初始状态,问如何染色能使得整个棋盘全变成黄色 Input 第一行一整数T表示用例组数,每组用例首先输入一整数n表示棋盘规模,之后一个n*n矩阵表示棋盘的初始状态(T<=20,n<=15) Output 对于每组用例,如果存在一个方案使得所有格子都变成黄色则输出最少

ZSQ 583

poj 1681 Painter's Problem 高斯消元

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

cstdio_hao的专栏 630
上一篇: 林大陈宇老师的原创高斯
下一篇: pku 1753 Flip Game(高斯消元+枚举)
剑不飞
博客等级 码龄15年 86粉丝 505原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址