#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
int pic[1000][300],num[7],kase=1,cnt,m,n,s[16][4]=
{
{0,0,0,0},{0,0,0,1},{0,0,1,0},{0,0,1,1},
{0,1,0,0},{0,1,0,1},{0,1,1,0},{0,1,1,1},
{1,0,0,0},{1,0,0,1},{1,0,1,0},{1,0,1,1},
{1,1,0,0},{1,1,0,1},{1,1,1,0},{1,1,1,1}
};
char str[17]="0123456789abcdef",st[6] = {'A', 'D', 'J', 'K', 'S', 'W'};
void dfs(int i,int j)
{
if(i < 0||i > m+1||j <0||j > n+1||pic[i][j] != 0)
return;
pic[i][j] = -1;
dfs(i-1,j);
dfs(i+1,j);
dfs(i,j-1);
dfs(i,j+1);
}
void dfs2(int i,int j)
{
if(pic[i][j] == -1||i < 0||i > m+1||j <0||j > n+1)
return;
if(!pic[i][j])
{
cnt++;
dfs(i,j);
return;
}
pic[i][j] = -1;
dfs2(i-1,j);
dfs2(i+1,j);
dfs2(i,j-1);
dfs2(i,j+1);
}
int main()
{
while(scanf("%d%d",&m,&n)&&m&&n)
{
memset(pic,0,sizeof(pic));
memset(num,0,sizeof(num));
for(int i = 1;i <= m;i++) //第i行
{
int len = 1;
getchar();
for(int j = 1;j <= n;j++) //第j个字符
{
char temp;
scanf("%c",&temp);
for(int k = 0;k<16;k++)
if(temp==str[k])
{
for(int l = 0;l<4;l++)
pic[i][len++]=s[k][l];
break;
}
}
}
n=n*4; //这就是上面为什么不能从i = 0开始算的原因
dfs(0,0); //盲区
for(int i = 1;i <=m;i++)
for(int j = 1;j <=n;j++)
if(pic[i][j] == 1)
{
cnt = 0;
dfs2(i,j);
if(!cnt) num[5]++;
else if(cnt == 1) num[0]++;
else if(cnt == 2) num[3]++;
else if(cnt == 3) num[2]++;
else if(cnt == 4) num[4]++;
else num[1]++;
}
printf("Case %d: ", kase++);
for(int i = 0; i < 6; i++)
while(num[i]--)
printf("%c", st[i]);
puts("");
}
return 0;
}
注意边界情况,注意跳出递归的条件