提供三组测试数据吧:
5 3
fff
f0f
fff
f0f
fff
5 5
0fff0
0f0f0
fffff
00f00
00f00
5 5
0f0f0
0f0f0
fffff
00f00
00f00
0 0
AC输出是:
Case 1: K
Case 2: A
Case 3: W
做题的时候,发现使用ios::sync_with_stdio(false);就会WA,注释掉就AC,不知道是什么情况,正在研究中。
#include <bits/stdc++.h>
using namespace std;
const string dict[16] = {
"0000", "0001", "0010", "0011",
"0100", "0101", "0110", "0111",
"1000", "1001", "1010", "1011",
"1100", "1101", "1110", "1111",
};
const int dir[4][2] = { { -1, 0 }, { 0, -1 }, { 1, 0 }, { 0, 1 } };
const char alp[8] = { 'W', 'A', 'K', 'J', 'S', 'D' };
map<char, int> cnt;
char tab[256][256];
int H, W, kase, cur;
bool isin(const int r, const int c){
return r >= 0 && r <= H + 1 && c >= 0 && c <= W + 1;
}
void DFS1(const int r, const int c){
if (!isin(r, c) || tab[r][c]!='0') return;
tab[r][c] = '-';
for (int i = 0; i < 4; ++i)
DFS1(r + dir[i][0], c + dir[i][1]);
}
void DFS2(const int r, const int c){
if (!isin(r, c) || tab[r][c] != '1') return;
tab[r][c] = '-';
for (int i = 0; i < 4; ++i){
int r1 = r + dir[i][0], c1 = c + dir[i][1];
if (tab[r1][c1] == '0')
++cur, DFS1(r1, c1);
else DFS2(r1, c1);
}
}
int main()
{
//ios::sync_with_stdio(false);
while (memset(tab, '0', sizeof(tab)), cnt.clear(), cin >> H >> W, H || W){
W *= 4;
for (int i = 1; i <= H; ++i){
string line, res; cin >> line;
for (auto i : line) res += dict[i >= 'a' ? (i - 'a' + 10) : (i - '0')];
memcpy(tab[i]+1, res.c_str(), W);
}
DFS1(0, 0);
for (int i = 1; i <= H; ++i){
for (int j = 1; j <= W; ++j){
if (tab[i][j] != '1') continue;
cur = 0;
DFS2(i, j);
cnt[alp[cur]]++;
}
}
printf("Case %d: ", ++kase);
for (auto i : cnt) while (i.second--) cout << i.first;
cout << endl;
}
return 0;
}