要求在矩形中填入字母序列, 使得其字典序满足最小,
个人用谈心策略做的,即当前点总是填入能填的最小字母。
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#define MAX 11
using namespace std;
char map[MAX][MAX];
int n,movex[4]={0,1,0,-1},movey[4]={1,0,-1,0};
bool isborder(int x,int y)
{
if(x<0||x>=n||y<0||y>=n)
return true;
return false;
}
int main()
{
int cas,cass=1;
scanf("%d",&cas);
while(cas--)
{
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%s",map[i]);
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
if(map[i][j]!='.')
continue;
for(int k=0;k<26;k++)
{
int flag=0;
for(int l=0;l<4;l++)
{
int x=i+movex[l];
int y=j+movey[l];
if(isborder(x,y))
continue;
if(map[x][y]=='A'+k)
{
flag=1;
break;
}
}
if(!flag)
{
map[i][j]='A'+k;
break;
}
}
}
printf("Case %d:\n",cass++);
for(int i=0;i<n;i++)
printf("%s\n",map[i]);
}
return 0;
}