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

从HSPICE到Spectre:手把手教你搞定不同SPICE仿真器的网表转换(附避坑指南) 本文详细介绍了从HSPICE到Spectre的SPICE仿真器网表转换方法,包括核心语法差异、分步转换流程和常见问题排查。通过实战案例和避坑指南,帮助工程师高效解决不同SPICE仿真器间的兼容性问题,提升集成电路设计效率。 阅读详情

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


思路:定义矩阵color[][]表示正方形的颜色,0表示黄色,1表示白色。定义矩阵paint[][],paint[i][j]为1表示格子(i,j)涂色,否则为0表示未涂色。然后枚举paint[1]整行,接下来根据(i-1,j)格子的颜色决定(i,j)格子是否涂色。最终,

判断最后一行涂色是否满足要求正方形的最后一行。


AC代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 20;
const int INF = 0x3f3f3f3f;
int color[MAXN][MAXN];
int paint[MAXN][MAXN];
int n;
int judge() {
    for (int i = 1; i < n; i++) {
        for (int j = 1; j <= n; j++) {
            paint[i + 1][j] = (color[i][j] + paint[i][j] + paint[i - 1][j] + paint[i][j - 1] + paint[i][j + 1]) % 2;    
            //根据上一格的颜色判断本格是否涂色
        }
    }
    for (int i = 1; i <= n; i++) {
        if ((paint[n][i] + paint[n][i - 1] + paint[n][i + 1] + paint[n - 1][i]) % 2 != color[n][i]) {   //判断最后一行是否满足
            return INF;
        }
    }
    int ret = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (paint[i][j] == 1) {
                ret++;
            }
        }
    }
    return ret;
}
int main() {
    int T;
    scanf("%d", &T);
    while (T--) {
        scanf("%d", &n);
        memset(paint, 0, sizeof(paint));
        for (int i = 1; i <= n; i++) {
            char s[MAXN];
            scanf("%s", s);
            for (int j = 0; s[j]; j++) {
                if (s[j] == 'w') {
                    color[i][j + 1] = 1;
                } else {
                    color[i][j + 1] = 0;

                }
            }
        }

        int res = INF;
        for (int i = 0; i < (1 << n); i++) {
            for (int k = 0; k < n; k++) {
                if (i & (1 << k)) {
                    paint[1][k + 1] = 1;    //枚举第一行
                } else {
                    paint[1][k + 1] = 0;
                }
            }
            res = min(res, judge());
        }
        if (res == INF) {
            printf("inf\n");
        } else {
            printf("%d\n", res);
        }
    }
    return 0;
}




手把手拆解DGX GB200 NVL72:1个机柜如何塞进72块GPU?详解NVLink Switch铜缆背板设计 本文深度解析英伟达DGX GB200 NVL72系统如何通过NVLink Switch铜缆背板设计,在标准42U机柜中集成72块B200 GPU,实现1800GB/s的超高带宽。文章详细介绍了模块化架构、铜缆互连技术、液冷散热方案和高密度供电系统等核心创新,为AI算力需求提供突破性解决方案。 阅读详情

相关推荐

基于人脸识别的课堂考勤系统 tkinter+openpyxl+face_recognition

基于face_recognition库实现人脸考勤系统功能。利用Python的tkinter模块来设计考勤系统的图形化界面,此外结合openpyxl模块,实现将学生的信息和考勤打卡数据保存到表格中,方便查看和调用。该系统具备“刷脸打卡”、“新学生注册”等功能。............

lyx4949的博客 6965

POJ - 1716 Integer Intervals

Integer Intervals Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14913   Accepted: 6310 Description An integer interval [a,b], a Write a program that: fin

哇-WA 的博客 6440

通达信短周期指标盘中实时监控与预警选股实战

本文详细介绍了通达信短周期指标在盘中实时监控与预警选股中的实战应用。通过分时指标改造、多股同列监控技巧及预警策略优化,帮助短线交易者捕捉转瞬即逝的交易机会,提升选股效率与准确性。重点解析了如何避免假信号干扰及系统卡顿问题,并探讨了从预警到自动交易的进阶应用。

weixin_29230805的博客 897

20-1-14-Prime-POJ1789

占坑 POJ1789, 2020.1.14日题, 2020.1.15日更新

shen253135371的博客 9457

PCB板质量测试和PCB板

PCB板质量测试和板厂

weixin_51370842的博客 515

Unity 接入 Steam 平台

Unity 接入 steam sdk 的登录和应用内购买功能

MarinaTsang的博客 3904

【PAT B1019/A1069】 The Black Hole of Numbers

For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in non-increasing order first, and then in non-decreasing order, a new number can be obtained by taking the second number from the first one. Repeat in this m

zxc0074869的博客 1万+

Halcon实战:5分钟搞定灰度图转热力图(附完整代码)

本文提供了一份Halcon实战指南,详细讲解如何在5分钟内将灰度图转换为直观的热力图。通过核心代码演示了利用HSV颜色模型进行色彩映射的原理与步骤,包括图像归一化、通道构建与空间转换,并附赠了深度图可视化等进阶应用技巧,帮助工程师快速提升图像分析效果。

weixin_29240343的博客 282

poj1789--prim

每天进步一点点~ 今天写的是poj1789 题目的大致意思是: 给很多个长度为7的字符串,判断字符串的最小衍生数 使用的是prime算法 #define LOCAL #include &lt;stdio.h&gt; #include &lt;string.h&gt; #define MAXN 2000 + 10 #define MAXL 7+10 char truckTypes...

kanwoerzi 209

M2DGR数据集在主流SLAM框架中的实战配置指南:从ORB-SLAM到FAST-LIO的完整流程

本文提供了M2DGR多传感器数据集在主流SLAM框架中的实战配置指南。详细介绍了从ORB-SLAM、VINS-Mono等视觉/视觉惯性框架,到A-LOAM、FAST-LIO等激光/激光惯性框架的完整配置流程、参数调整要点及常见问题解决方案。通过实际测试对比,分析了各算法在M2DGR数据集上的表现,为机器人定位与建图研究提供了宝贵的实战经验。

k9l0m1的博客 481

【入门AUTOSAR网络管理测试】CANoe测试T_WakeUp时间

随着智能电动汽车行业的快速发展,目前AUTOSAR网络管理也得到了广泛应用,AUTOSAR网络管理测试也是汽车网络测试中非常重要的环节,NM问题导致的汽车ECU不能唤醒、电瓶亏电等问题,对于新入门AUTOSAR网络管理测试的同学不清楚如何开展测试,那么本章节就主要给大家介绍网络管理中T_WakeUp时间如何测试。【入门AUTOSAR网络管理测试】AUTOSAR网络管理规范需求解读。

测试领域技术分享 2135

POJ 1157 LITTLE SHOP OF FLOWERS

LITTLE SHOP OF FLOWERSTime Limit: 1000MS Memory Limit: 10000KTotal Submissions: 10554 Accepted: 4890DescriptionYou want to arrange the window of your flower shop

bobten2008的专栏 647

Python无法拒绝的表白界面(完整代码)

Python制作一个无法拒绝的表白界面的完整代码。

AIGC 5万+

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

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

POJ 1159 Palindrome

L(longest)C(common)S(sequence)经典问题,要用滚动数组,不然会爆内存。 #include #include #include #include using namespace std; int dp[2][5005]; int main() { string s1, s2; int n; while (cin >> n) {

mayer314的专栏 2710

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

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

lwip-2.0.3

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

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计算出最少需要涂画多少块砖,才能使所有砖的

不若乘风来 2453

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

poj 1617

#include using namespace std; #include int getmin(char s[]); int main() { char s[11]; while(cin>>s) { if(strcmp(s,"THEEND") == 0) b

deng_yu的专栏 1063

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的博客 192

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的博客 391

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

Painter's Problem POJ - 1681

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

a7f650ebd327889c的博客 483

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 386

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

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
上一篇: uva112-Tree Summing[动态建树]
vCodes
博客等级 码龄10年 1粉丝 13原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值