您的位置:首页 > 其它

UVa 11520 - Fill the Square (填充正方形)

2013-05-29 20:52 267 查看
FilltheSquareInput:StandardInput

Output:StandardOutput

Inthisproblem,youhavetodrawasquareusinguppercaseEnglishAlphabets.

Tobemoreprecise,youwillbegivenasquaregridwithsomeemptyblocksandothersalreadyfilledforyouwithsomeletterstomakeyourtaskeasier.Youhavetoinsertcharactersineveryemptycellsothatthewholegridisfilledwithalphabets.Indoingsoyouhavetomeetthefollowingrules:

Makesurenoadjacentcellscontainthesameletter;twocellsareadjacentiftheyshareacommonedge.

Therecouldbemanywaystofillthegrid.Youhavetoensureyoumakethelexicographicallysmallestone.Here,twogridsarecheckedinrowmajororderwhencomparinglexicographically.

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

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


SampleInputOutputforSampleInput

2

3

...

...

...

3

...

A..

...

Case1:

ABA

BAB

ABA

Case2:

BAB

ABA

BAB

我的代码:

#include<cstdio>
#include<cstring>
usingnamespacestd;

intmain()
{
intt,n,count=1;
inti,j,k;
charst[20][20];
intvis[27];
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
getchar();
for(i=0;i<n;i++)
gets(st[i]);
printf("Case%d:\n",count++);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(st[i][j]>='A'&&st[i][j]<='Z')
continue;
memset(vis,0,sizeof(vis));
if(i>0&&st[i-1][j]!='.')vis[st[i-1][j]-'A']=1;//上
if(j<n-1&&st[i][j+1]!='.')vis[st[i][j+1]-'A']=1;//右
if(i<n-1&&st[i+1][j]!='.')vis[st[i+1][j]-'A']=1;//下
if(j>0&&st[i][j-1]!='.')vis[st[i][j-1]-'A']=1;//左
for(k=0;k<26;k++)
{
if(!vis[k])
{
st[i][j]=k+'A';
break;
}
}
}
}
for(i=0;i<n;i++)
puts(st[i]);
}
return0;
}


ViewCode
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: