POJ1671--Painer'sProblem
2015-12-19 15:52
423 查看
Painter’s Problem
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 5261 Accepted: 2549
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
几经波折,最终终于通过了,总是因为变量初始化的位置不对导致我改bug用了很长时间。。。。
下面是我拙劣的代码
这是大神的代码
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 5261 Accepted: 2549
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
几经波折,最终终于通过了,总是因为变量初始化的位置不对导致我改bug用了很长时间。。。。
下面是我拙劣的代码
#include<iostream> #include<cstring> using namespace std; int n; int wall[16][17]; int paint[16][17]; int mark=0; int count=0; bool solution() { count=0; for(int j=1;j<=n;j++) { if(((wall [j]+paint [j]+paint[n-1][j]+paint [j-1]+paint [j+1])%2)==0) return false; } for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { if(paint[i][j]) count++; } return true; } int main() { int t; int mmax; int mmin=300; char xy[16][17]; cin>>t; while(t--) { memset(wall,0,sizeof(wall)); memset(paint,0,sizeof(paint)); cin>>n; //数据输入 for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { cin>>xy[i][j]; if(xy[i][j]=='y') wall[i][j]=1; } mmax=1<<n; mark=0; mmin=300; for(int k=1;k<=mmax;k++) { for(int i=2;i<=n;i++) for(int j=1;j<=n;j++) { if(((wall[i-1][j]+paint[i-1][j]+paint[i-1][j-1]+paint[i-1][j+1]+paint[i-2][j])%2)==0) paint[i][j]=1; else paint[i][j]=0; } if(solution()) { mark=1; /// cout<<mmin<<endl; if(mmin>count) mmin=count; // cout<<count<<endl; } //枚举第一行 int c=1; paint[1][1]++; while(paint[1][c]>1) { paint[1][c]=0; c++; paint[1][c]++; } } if(mark) cout<<mmin<<endl; else cout<<"inf"<<endl; } return 0; }
这是大神的代码
#include <iostream> using namespace std; bool wall[18][18]; bool paint[18][18]; int count=0; int mmin=300; bool allYellow(int n) { int i,j; count=0; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if(wall[i][j]^paint[i][j]^paint[i][j-1]^paint[i][j+1]^paint[i-1][j]^paint[i+1][j])return false; if(paint[i][j])count++; } } return true; } int main() { freopen("in.txt","r",stdin); int t,n; cin>>t; int i,j,k,p; char wy[16]; int mmax,tmp; bool possible=false; for(i=0;i<t;i++) { cin>>n; memset(wall,0,sizeof(wall)); for(j=1;j<=n;j++) { cin>>wy; for(k=0;k<n;k++) { if(wy[k]=='w')wall[j][k+1]=true; } } mmax=1<<n; mmin=300; possible=false; for(j=0;j<mmax;j++) //第一行的状态 0000 - 1111 枚举 { memset(paint,0,sizeof(paint)); tmp=j; for(k=1;k<=n;k++) //第一行的状态 { paint[1][k]=tmp&1; tmp=tmp>>1; //右移一位 } for(k=2;k<=n;k++) { for(p=1;p<=n;p++) //根据wall[k-1][p]的paint后状态 决定paint[k][p]值 { if(wall[k-1][p]^paint[k-1][p]^paint[k-1][p-1]^paint[k-1][p+1]^paint[k-2][p])paint[k][p]=true; } } if(allYellow(n)) { possible=true; if(mmin>count)mmin=count; } } if(possible)cout<<mmin<<endl; else cout<<"inf"<<endl; } return 0; }
相关文章推荐
- Foxmail重复收取邮件解决方案
- foxmail重复收取邮件解决办法及删除重复邮件办法
- 线程同步(同步对象、同步访问对象)http://blog.csdn.net/akof1314/article/details/5764200
- 性能测试中AIX服务器资源监控与瓶颈分析-1
- 能测试中AIX服务器资源监控与瓶颈分析-2
- RAID级别和实现实例操作详解
- 高斯定时器导致的NotHttpResponseException:The target server failed to repond异常 -- 解决
- RAID各层级特性
- 创建一个10G可用空间的RAID5
- 全局对象和头文件的处理http://zhidao.baidu.com/link?url=w4sdxKWPK_Wxto1ypQGxMPjDVd_fmQCCBr_Jvkmjns8px5GuBZmBCq9n
- RAID
- [BetterExplained]书写是为了更好的思考
- [BetterExplained]遇到问题为什么应该自己动手
- AIX 分页替换技术入门
- #ifndef/#define/#endif使用详解http://blog.csdn.net/abc5382334/article/details/18052757
- BaiduMapsApiASDemo :编译错误、baidumapsdk.demo key验证出错
- 人工智能初体验(一):使用图灵机器人智能获取问题回答
- UVa 253 Cube painting (骰子涂色)
- C++父类与子类关系以及函数重载、覆盖和隐藏规则 http://blog.csdn.net/xingyu19871124/article/details/7640131
- Daily Scrumming* 2015.12.18(Day 10)