1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <set> #include <vector>
using namespace std;
char bin[256][5];
const int maxh = 200 + 5; const int maxw = 50 * 4 + 5;
int H, W, pic[maxh][maxw], color[maxh][maxw];
char line[maxw];
void decode(char ch, int row, int col) { for (int i = 0; i < 4; i++) pic[row][col + i] = bin[ch][i] - '0'; }
const int dr[] = {-1, 1, 0, 0}; const int dc[] = {0, 0, -1, 1};
void dfs(int row, int col, int c) { color[row][col] = c; for (int i = 0; i < 4; i++) { int row2 = row + dr[i]; int col2 = col + dc[i]; if (row2 >= 0 && row2 < H && col2 >= 0 && col2 < W && pic[row2][col2] == pic[row][col] && color[row2][col2] == 0) dfs(row2, col2, c); } }
vector<set<int>> neighbors;
void check_neighbors(int row, int col) { for (int i = 0; i < 4; i++) { int row2 = row + dr[i]; int col2 = col + dc[i]; if (row2 >= 0 && row2 < H && col2 >= 0 && col2 < W && pic[row2][col2] == 0 && color[row2][col2] != 1) neighbors[color[row][col]].insert(color[row2][col2]); } }
const char* code = "WAKJSD";
char recognize(int c) { int cnt = neighbors[c].size(); return code[cnt]; }
void print() { for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) printf("%d", pic[i][j]); printf("\n"); } }
int main() { freopen("./UVa1103.txt", "r", stdin);
strcpy(bin['0'], "0000"); strcpy(bin['1'], "0001"); strcpy(bin['2'], "0010"); strcpy(bin['3'], "0011"); strcpy(bin['4'], "0100"); strcpy(bin['5'], "0101"); strcpy(bin['6'], "0110"); strcpy(bin['7'], "0111"); strcpy(bin['8'], "1000"); strcpy(bin['9'], "1001"); strcpy(bin['a'], "1010"); strcpy(bin['b'], "1011"); strcpy(bin['c'], "1100"); strcpy(bin['d'], "1101"); strcpy(bin['e'], "1110"); strcpy(bin['f'], "1111");
int kase = 0;
while (scanf("%d%d", &H, &W) == 2 && H) { memset(pic, 0, sizeof(pic)); for (int i = 0; i < H; i++) { scanf("%s", line); for (int j = 0; j < W; j++) decode(line[j], i + 1, j * 4 + 1); }
H += 2; W = W * 4 + 2; int cnt = 0; vector<int> cc; memset(color, 0, sizeof(color)); for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) if (!color[i][j]) { dfs(i, j, ++cnt); if (pic[i][j] == 1) cc.push_back(cnt); }
std::cout << "================cc vector================" << '\n'; for (auto n : cc) { std::cout << n << ' '; } std::cout << '\n'; std::cout << "=============cc vector ends==============" << '\n';
neighbors.clear();
neighbors.resize(cnt + 1); for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) if (pic[i][j] == 1) check_neighbors(i, j);
vector<char> ans; for (int i = 0; i < cc.size(); i++) ans.push_back(recognize(cc[i])); sort(ans.begin(), ans.end()); printf("Case %d: ", ++kase); for (int i = 0; i < ans.size(); i++) printf("%c", ans[i]); printf("\n");
std::cout << "================pic array================" << '\n'; for (int row = 0; row < H; row++) { for (int col = 0; col < W; col++) { std::cout << pic[row][col] << ' '; } std::cout << '\n'; } std::cout << "=============pic array ends==============" << '\n';
std::cout << "================color array================" << '\n'; for (int row = 0; row < H; row++) { for (int col = 0; col < W; col++) { std::cout << color[row][col] << ' '; } std::cout << '\n'; } std::cout << "=============color array ends==============" << '\n';
std::cout << "================neighbors vector================" << '\n'; for (auto set : neighbors) { for (auto n : set) { std::cout << n << ' '; } std::cout << '\n'; } std::cout << "=============neighbors vector ends==============" << '\n'; }
return 0; }
|