标签:led scanf 出现 演示 include cpp 进制 并且 思路
10 3
000
778
548
748
578
700
000
7f0
1e0
000
#include <bits/stdc++.h>
using namespace std;
int pic[200][200], ascii[6]={87,65,75,74,83,68},H, W,n = 0,
t[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}};
void filled()//输入图
{
char temp;
for (int i = 0; i < H; i++)
for (int k = 0, c = 0; k < W; k++)
{
scanf(" %c", &temp); //空格吸收回车,字符串读入
if (temp > 96) temp -= 87; //97为a
else temp -= 48;
for (int f = 0; f < 4; f++) //输入的转为4个二进制
pic[i][c++] = t[temp][f]; //c为当前行第几个,换行时清零
}
}
void dfs(int y, int x, int f, int a) //y行 x列 f需要标记的值 a要连通的格子类型
{
if (x >= 4 * W || x < 0 || y >= H || y < 0 || pic[y][x] != a) return; //出界OR不是要爬的格子
pic[y][x] = f; //标记当前格为f
dfs(y - 1, x, f, a);
dfs(y + 1, x, f, a);
dfs(y, x - 1, f, a);
dfs(y, x + 1, f, a);
}
int main()
{
while (cin >> H >> W) //H行 W列
{
if (H == 0)
break; //结束条件0 0
filled();
cout << "Case " << ++n << ": ";
int n = 4, c[100] = {0},cnt=0; //c数组存放符号n的圈的个数
for (int i = 0; i < H; i++) //功能:遍历四条边的每一点
{
int q = 4 * W;
if (i == 0 || i == H - 1) q = 2;
for (int j = 0; j < 4 * W; j += q - 1)
dfs(i, j, -1, 0);
}
for (int i = 0; i < H; i++) //功能:标记每个黑块组成的符号
for (int j = 0; j < 4 * W; j++)
if (pic[i][j] == 1)
dfs(i, j, n++, 1); //防止混淆,n初始为4,当前符号标记为4
for (int i = 0; i < H; i++) //功能:找白块
for (int j = 0; j < 4 * W; j++)
if (pic[i][j] == 0)
{
c[pic[i][j - 1]]++; //直接找前一个黑块的值作为该符号的号码,表示增加一个圈
dfs(i, j, -2, 0); //用-2标记已经计算过的圈内白块
}
for (int i = 4; i < n; i++) c[i]=ascii[c[i]]; //将值换成ASCII码存放
sort(c+4,c+n);//对c数组第4个到第n-1个升序排序
for (int i = 4; i < n; i++) printf("%c",c[i]); //字典序输出 n初始是从4开始算
cout << endl;
}
return 0;
}
标签:led scanf 出现 演示 include cpp 进制 并且 思路
原文地址:https://www.cnblogs.com/linme/p/11978278.html