题意:输入以16进制的矩阵,先转换成2进制,之后输出形成的图案。
思路:先处理掉无关图案的0,之后一个图案一个图案的遍历,识别图案的方法就是有多少个圈圈。找到一个就全部标记为-1。并且记录圆圈的数目。
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm> using namespace std; int gragh[][];
int dir[][] = {{-,},{,},{,},{,-}};
char s[];
int n,m,ans; void change(char c,int i,int j)
{
if(c >= '' && c <= ''){
int a = c - '';
gragh[i][(j-)*+] = a%;a /= ;
gragh[i][(j-)*+] = a%;a /= ;
gragh[i][(j-)*+] = a%;a /= ;
gragh[i][(j-)*+] = a%;
}
else {
int a = c - 'a' + ;
gragh[i][(j-)*+] = a%;a /= ;
gragh[i][(j-)*+] = a%;a /= ;
gragh[i][(j-)*+] = a%;a /= ;
gragh[i][(j-)*+] = a%;
}
} void dfs1(int x,int y)
{
gragh[x][y] = -;
for(int i = ;i < ; i++){
int xx = x + dir[i][];
int yy = y + dir[i][];
if(xx >= && xx <= n && yy >= && yy <= m && gragh[xx][yy] == ){
dfs1(xx,yy);
}
}
} void dfs2(int x,int y)
{
gragh[x][y] = -;
for(int i = ;i < ; i++){
int xx = x + dir[i][];
int yy = y + dir[i][];
if(xx >= && xx <= n && yy >= && yy <= m && gragh[xx][yy] == ){
dfs1(xx,yy);
ans++;
}
else if(xx >= && xx <= n && yy >= && yy <= m && gragh[xx][yy] == ){
dfs2(xx,yy);
}
}
} int main()
{
// freopen("in.txt","r",stdin);
int ncase = ;
while(scanf("%d%d",&n,&m) != EOF){
if(n == && m == )
break; memset(gragh,,sizeof(gragh));
memset(s,,sizeof(s));
char c[];
for(int i = ;i <= n; i++){
scanf("%s",c+);
for(int j = m;j >= ; j--){
change(c[j],i,j);
}
}
m *=; for(int i = ;i <= n; i++){
if(gragh[i][] == )
dfs1(i,);
if(gragh[i][m] == )
dfs1(i,m);
}
for(int j = ;j <= m; j++){
if(gragh[][j] == )
dfs1(,j);
if(gragh[n][j] == )
dfs1(n,j);
}
int pos = ;
for(int i = ;i <= n; i++){
for(int j = ;j <= m; j++){
if(gragh[i][j] == ){
ans = ;
dfs2(i,j);
if(ans == ){
s[pos++] = 'A';
}
else if(ans == )
s[pos++] = 'J';
else if(ans == )
s[pos++] = 'D';
else if(ans == )
s[pos++] = 'S';
else if(ans == )
s[pos++] = 'W';
else if(ans == )
s[pos++] = 'K';
}
}
} sort(s,s+pos);
printf("Case %d: ",ncase++);
for(int i = ;i < pos; i++)
printf("%c",s[i]);
printf("\n");
}
return ;
}