#include <vector>
#include <map>
#include<algorithm>
#include<cmath>
#include<set>
#include<cstdio>
#include <sstream>
#include <string>
#include<iostream>
#include<iomanip>
#include<stack>
#include<utility>
#include<unordered_map>
#include<queue>
#include<cstring>
#include <list>
#include<deque>
using namespace std;
const int maxn = 200+5;
int pic[maxn][maxn];//原图
int m, n;
int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 };
char word[7] = { "WAKJSD" };
int cur;
const int offset = 2;//防止拷贝后的黑色联通块触及边界
bool isin(const int r, const int c)
{
return r >= 0 && r <= m + offset && c >= 0 && c <= 4 * n + offset;
}
void DFS1(const int r, const int c) {
if (!isin(r, c) || pic[r][c] != 0) return;
pic[r][c] = '-';
for (int i = 0; i < 4; ++i)
DFS1(r + dx[i], c + dy[i]);
}
void DFS2(const int r, const int c) {
if (!isin(r, c) || pic[r][c] != 1) return;
pic[r][c] = '-';
for (int i = 0; i < 4; ++i){
int r1 = r + dx[i], c1 = c + dy[i];
if (pic[r1][c1] == 0) {
++cur;
DFS1(r1, c1);
}
else DFS2(r1, c1);
}
}
int main()
{
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int kase = 0;
while (scanf("%d%d", &m, &n) == 2 && m && n)
{
vector<char>out;
getchar();
char ch;
int num;
int cnt = 0;
memset(pic, 0, sizeof(pic));
for (int k = 0; k < m; ++k)
{
for (int i = 0; i < n; ++i)
{
ch = getchar();
if (isalpha(ch))
{
num = ch - 'a' + 10;
}
else
num = ch - '0';
for (int j = 3; j >= 0; --j)
{
pic[k+1][i * 4 +j+1] = num % 2;
num /= 2;
}
}
getchar();
}
DFS1(0, 0);
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= 4*n; ++j) {
if (pic[i][j] != 1) continue;
cur = 0;
DFS2(i, j);
out.push_back(word[cur]);
}
}
printf("Case %d: ", ++kase);
sort(out.begin(), out.end());
for (char elem : out)
{
printf("%c", elem);
}
puts("");
}
}
紫书UVa1103
最新推荐文章于 2021-07-30 23:15:01 发布