算法竞赛入门经典 例题6-13

UVa1103

Ancient Messages

识别象形文字。

由于一个象形文字中黑色连通像素块只有一个,且象形文字互不覆盖,所以可以根据黑色联通像素块分割象形文字;又因为每个象形文字中白色连通像素块的数量是不同的,所以可以根据白色连通像素块的数量来识别象形文字,因此问题转化为识别黑白连通像素块的问题,DFS可解。

这道题提交了22次才完全AC,但即使是这样还是不知道错误在哪里,只能按照题解提交。为了在统计白色连通像素块的过程中尽可能的缩小象形文字图片的大小,代码在遍历黑色连通像素块的过程中,记录了当前需要识别的象形文字的上下左右边界,然后将得到的矩形拷贝到了一个新的图片中,并在四周添加了一圈白色像素作为外边界。

正确与错误的差别就在于拷贝象形文字图片的代码(69~72行)。如果只保留第70行,也就是直接拷贝矩形的象形文字,然后统计其中白色连通像素块的数量,提交后就是WA(但是可以通过uDebug的测试用例),但是只拷贝此象形文字中的黑色像素(也就是此次DFS中状态被标记为PixelState::VISITING的像素),提交后就是AC。二者的区别就在于矩形象形文字中可能包含其它象形文字一部分的黑色连通像素块,感觉应该是不会对统计白色连通像素块的数量造成影响的,但是却举不出反例。

最后,网上的很多代码都是8个方向的DFS,但其实题目中的意思应该是4个方向的DFS。

#include <iostream>
#include <algorithm>
#include <bitset>
#include <map>
#include <string>
#include <vector>

using namespace std;

class Solution
{
public:
	Solution(size_t H, size_t W, istream &is)
		: image(vector<string>(H)), ImageVisitingState(vector<vector<PixelState>>(H, vector<PixelState>(4 * W, PixelState::UNVISITED)))
	{
		for (size_t i = 0; i < H; i++)
		{
			string line;
			is >> line;
			for (char ch : line)
			{
				bitset<4> hex(stoi(string(1, ch), NULL, 16));
				image[i].append(hex.to_string());
			}
		}
		for (size_t i = 0; i < H; i++)
		{
			for (size_t j = 0; j < image[i].length(); j++)
			{
				if (image[i][j] == '1' && ImageVisitingState[i][j] == PixelState::UNVISITED) {
					size_t left = j, right = j, top = i, bottom = i;
					GetCharacterBoundary(i, j, left, right, top, bottom);
					char ch = RecognizeCharacter(left, right, top, bottom);
					CharacterFrequency[ch]++;
				}
			}
		}
	}
	map<char, int> CharacterFrequency;
private:
	enum class PixelState
	{
		UNVISITED, VISITING, VISITED
	};
	vector<string> image, duplicate;
	vector<vector<PixelState>> ImageVisitingState, DupVisitingState;
	static array<string, 16> binary;
	static vector<char> WhiteHole2Character;
	void GetCharacterBoundary(size_t i, size_t j, size_t &left, size_t &right, size_t &top, size_t &bottom)
	{
		if (image[i][j] != '1' || ImageVisitingState[i][j] != PixelState::UNVISITED) return;
		ImageVisitingState[i][j] = PixelState::VISITING;
		left = min(j, left), right = max(j, right);
		top = min(i, top), bottom = max(i, bottom);
		if (i > 0) GetCharacterBoundary(i - 1, j, left, right, top, bottom);
		if (j > 0) GetCharacterBoundary(i, j - 1, left, right, top, bottom);
		if (j + 1 < image[i].length()) GetCharacterBoundary(i, j + 1, left, right, top, bottom);
		if (i + 1 < image.size()) GetCharacterBoundary(i + 1, j, left, right, top, bottom);
	}
	char RecognizeCharacter(size_t left, size_t right, size_t top, size_t bottom)
	{
		size_t row = bottom - top + 3, col = right - left + 3;
		duplicate.assign(row, string(col, '0'));
		DupVisitingState.assign(row, vector<PixelState>(col, PixelState::UNVISITED));
		for (size_t i = top; i <= bottom; i++)
		{
			for (size_t j = left; j <= right; j++)
			{
				if (ImageVisitingState[i][j] == PixelState::VISITING) {
					duplicate[i - top + 1][j - left + 1] = image[i][j];
					ImageVisitingState[i][j] = PixelState::VISITED;
				}
			}
		}

		int WhiteHole = 0;
		for (size_t i = 0; i < duplicate.size(); i++)
		{
			for (size_t j = 0; j < duplicate[i].length(); j++)
			{
				if (duplicate[i][j] == '0' && DupVisitingState[i][j] == PixelState::UNVISITED) {
					CountWhiteHole(i, j);
					WhiteHole++;
				}
			}
		}
		return WhiteHole2Character[WhiteHole - 1];
	}
	void CountWhiteHole(size_t i, size_t j)
	{
		if (duplicate[i][j] != '0' || DupVisitingState[i][j] == PixelState::VISITED) return;
		DupVisitingState[i][j] = PixelState::VISITED;
		if (i > 0) CountWhiteHole(i - 1, j);
		if (j > 0) CountWhiteHole(i, j - 1);
		if (j + 1 < duplicate[i].length()) CountWhiteHole(i, j + 1);
		if (i + 1 < duplicate.size()) CountWhiteHole(i + 1, j);
	}
};

vector<char> Solution::WhiteHole2Character = { 'W', 'A', 'K', 'J', 'S', 'D' };

ostream& operator<<(ostream &os, const Solution &solution)
{
	for (auto p : solution.CharacterFrequency)
	{
		os << string(p.second, p.first);
	}
	return os;
}

int main()
{
	int cases = 0;
	size_t H, W;
	while (cin >> H >> W) {
		if (H == 0 && W == 0) break;
		Solution solution(H, W, cin);
		cout << "Case " << ++cases << ": " << solution << endl;
	}
	return 0;
}
/*

*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值