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.
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]);
}
}
}
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 |
思路:暴力涂色。
#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]);
}
}
}
相关文章推荐
- uva 11520 - Fill the Square(枚举,2级)
- UVA11520 Fill the Square 枚举
- UVa 11520 Fill the Square(枚举)
- UVA11520 Fill the Square 枚举
- uva 11520 Fill the Square(枚举)
- uva 11520 - Fill the Square(贪心+枚举)
- uva 11520 - Fill the Square(贪心+枚举)
- UVa 11520 Fill the Square (贪心&字典序)
- UVA - 11520 Fill the Square 暴力
- UVA 11520 - Fill the Square (贪心)
- UVA 11520 Fill the Square
- UVA - 11520 - Fill the Square(贪心)
- uva 11520 - Fill the Square
- 例题1.14 填充正方形 Fill the Square UVA - 11520 搜索
- UVa 11520 - Fill the Square (填充正方形)
- UVA 11520 Fill the Square
- UVa 11520 - Fill the Square (填充正方形)
- UVa 11520 Fill the Square (水题,暴力)
- uva - 11520 - Fill the Square(贪心、字典序)
- UVA - 11520 Fill the Square 水题