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.
我的代码:
ViewCode
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
相关文章推荐
- UVa 11520 Fill the Square 填充正方形
- 例题1.14 填充正方形 Fill the Square UVA - 11520 搜索
- UVa 11520 - Fill the Square (填充正方形)
- UVa 11520 Fill the Square 填充正方形
- UVA11520 Fill the Square 枚举
- UVA - 11520 Fill the Square 暴力
- 例题1.14 填充正方形 UVa11520
- UVa 11520 Fill the Square(枚举)
- UVA 11520 Fill the Square
- UVa 11520 填充正方形
- UVA11520填充正方形
- UVA 11520 Fill the Square
- uva11520 填充正方形
- uva 11520 - Fill the Square(枚举,2级)
- UVA 11520 Fill the Square .
- 水题(填充正方形,uva 11520)
- uva 11520 - Fill the Square(枚举,2级)
- UVa 11520 填充正方形
- uva 11520 - Fill the Square(贪心+枚举)
- Uva 11520 填充正方形