Uva - 1103 - Ancient Messages




图像被压缩过,所以先解压成01矩阵,把所有的连通块填充颜色,背景色为1,黑色连通块的标号存放在cc中。neighbors是存放白洞的数组,最后根据白洞来判断属于那个字符。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <stack>
#include <queue>
#include <bitset> 
#include <cassert> 

using namespace std;

const int maxh = 205;
const int maxw = 205;

char bin[256][5];

int H, W, pic[maxh][maxw], color[maxh][maxw];
char line[maxw];

// 解码成一个01矩阵
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 };

// DFS遍历,把连通块标记为C
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 checkNeighbors(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()
{
	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; // 黑色连通块的标号存放在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);
					}
				}
			}
		}
		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) {
					checkNeighbors(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");
	}

	return 0;
}




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值