您的位置:首页 > 其它

uva 11520 - Fill the Square(枚举,2级)

2013-05-27 20:35 387 查看
ProblemA

FilltheSquare

Input:
StandardInput

Output:StandardOutput

Inthisproblem,youhavetodrawasquareusinguppercaseEnglishAlphabets.

Tobemoreprecise,youwillbegivenasquaregridwithsomeemptyblocksandothersalreadyfilledforyouwithsomeletterstomakeyourtaskeasier.Youhavetoinsertcharactersineveryemptycellsothatthewholegridisfilledwithalphabets.In
doingsoyouhavetomeetthefollowingrules:

Makesurenoadjacentcellscontainthesameletter;twocellsareadjacentiftheyshareacommonedge.
Therecouldbemanywaystofillthegrid.Youhavetoensureyoumakethelexicographicallysmallestone.Here,twogridsarecheckedinrowmajororderwhencomparinglexicographically.

Input

Thefirstlineofinputwillcontainanintegerthatwilldeterminethenumberoftestcases.Eachcasestartswithanintegern(n<=10),thatrepresentsthedimensionofthegrid.Thenextnlineswillcontainncharacters
each.Everycellofthegridiseithera‘.’oraletterfrom[A,Z].Herea‘.’Representsanemptycell.

Output
Foreachcase,firstoutputCase#:(#replacedbycasenumber)andinthenextnlinesoutputtheinputmatrixwiththeemptycellsfilledheedingtherulesabove.


SampleInputOutputforSampleInput

2

3

...

...

...

3

...

A..

...

Case1:

ABA

BAB

ABA

Case2:

BAB

ABA

BAB

Problemsetter:SohelHafiz

思路:暴力涂色。

#include<iostream>
#include<cstring>
#include<cstdio>
usingnamespacestd;
constintmm=15;
chars[mm][mm];
intn;
boolok(intx,inty,intz)
{charzz='A'+z;
if(x&&s[x-1][y]==zz)return0;
if(y&&s[x][y-1]==zz)return0;
if(y<n-1&&s[x][y+1]==zz)return0;
if(x<n-1&&s[x+1][y]==zz)return0;
return1;
}
voidaddcol(intx,inty)
{
for(inti=0;i<26;++i)
if(ok(x,y,i))
{s[x][y]='A'+i;return;}
}
intmain()
{intcas;
while(~scanf("%d",&cas))
{intz=0;
while(cas--)
{++z;
scanf("%d",&n);
for(inti=0;i<n;++i)
scanf("%s",s[i]);
for(inti=0;i<n;++i)
for(intj=0;j<n;++j)
if(s[i][j]=='.')
addcol(i,j);
printf("Case%d:\n",z);
for(inti=0;i<n;++i)
printf("%s\n",s[i]);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: