博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Painter's Problem
阅读量:7212 次
发布时间:2019-06-29

本文共 2988 字,大约阅读时间需要 9 分钟。

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 5378   Accepted: 2601

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

23yyyyyyyyy5wwwwwwwwwwwwwwwwwwwwwwwww

Sample Output

  0

   15

 

题解:

   构造矩阵高斯消元后可以得到一组解,但是题目中要求的是求出最小染色次数。所以要对其中不确定的方案进行枚举。

1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 using namespace std;10 int T,N,ANS;11 int a[300][300];12 bool gauss(){13 int now=1;14 for(int i=1;i<=N*N;i++){15 int to=now;16 while(to<=N*N&&a[to][i]==0) to++;17 if(to>N*N) continue;18 if(to!=now){19 for(int j=1;j<=N*N+1;j++) swap(a[to][j],a[now][j]);20 }21 for(int j=1;j<=N*N;j++){22 if(j!=now&&a[j][i]){23 for(int k=1;k<=N*N+1;k++){24 a[j][k]^=a[i][k];25 }26 }27 }28 now++;29 }30 for(int i=now;i<=N*N;i++)31 if(a[i][N*N+1]!=0) return false;32 return true;33 }34 35 int v[300],cnt;36 void dfs(int x){37 if(cnt>=ANS) return ;//已经比目前的答案大了,没有必要再搜 38 if(x==0){39 ANS=min(cnt,ANS);40 return ;41 }42 if(a[x][x]!=0){43 int num=a[x][N*N+1];//num表示第x块砖染色不染色 44 for(int i=x+1;i<=N*N;i++){45 if(a[x][i]!=0) num=num^v[i];//已经枚举过的x+1~N*N中某块砖如果可以对x产生影响且已染色,就让num改变一次 46 }47 v[x]=num;48 if(num==1) cnt++;49 dfs(x-1);50 if(num==1) cnt--;51 }52 else{ //枚举按或不按两种情况 53 v[x]=0; dfs(x-1);54 v[x]=1; cnt++; dfs(x-1); cnt--;55 }56 }57 58 int main(){59 scanf("%d",&T);60 while(T--){61 memset(a,0,sizeof(a));62 scanf("%d",&N);63 for(int i=1;i<=N*N;i++){64 a[i][i]=1;65 if(i%N!=1) a[i][i-1]=1;66 if(i%N!=0) a[i][i+1]=1;67 if(i>=N+1) a[i][i-N]=1;68 if(i<=N*(N-1)) a[i][i+N]=1; 69 }70 for(int i=1;i<=N;i++){71 char s[300];72 scanf("%s",s+1);73 for(int j=1;j<=N;j++){74 if(s[j]=='w') a[(i-1)*N+j][N*N+1]=1; 75 }76 }77 if(gauss()==false){78 puts("inf");79 continue;80 }81 ANS=1<<28;82 dfs(N*N);83 printf("%d\n",ANS);84 }85 return 0;86 }

 

转载于:https://www.cnblogs.com/CXCXCXC/p/5226382.html

你可能感兴趣的文章
小米正式开源 Istio 管理面板 Naftis
查看>>
Zabbix 添加WEB监控(学习笔记十一)
查看>>
ImageIO 框架详细解析
查看>>
查询一个ID出现2种结果的情况
查看>>
亚马逊 OpenJDK 发行版 Corretto GA
查看>>
kaldi 源码分析(七) - HCLG 分析
查看>>
SpaceVim 1.1.0 发布,模块化 Vim IDE
查看>>
Java 设计模式六大原则
查看>>
CentOS7 搭建Ambari-Server,安装Hadoop集群(一)
查看>>
Python爬虫基础:验证码的爬取和识别详解
查看>>
WPF 可触摸移动的ScrollViewer控件
查看>>
mysql事务
查看>>
HBase基本操作-java api
查看>>
PostgreSQL的时间/日期函数使用
查看>>
PostgreSQL 10.1 手册_部分 II. SQL 语言_第 12 章 全文搜索_12.4. 额外特性
查看>>
十字星文化获数千万元A轮融资,腾讯持续下注
查看>>
cron和crontab
查看>>
从阿里云数据库入选Gartner谈数据库的演化
查看>>
【Unity Shader】(六) ------ 复杂的光照(上)
查看>>
Android开发小技巧之商品属性筛选与商品筛选
查看>>