您的位置:首页 > 大数据 > 人工智能

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用了很长时间。。。。

下面是我拙劣的代码

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: