ural 1008 Image Encoding

本文探讨了一种使用广搜算法解决图编码问题的方法,通过输入的编码方式,推导出另一种编码方式的路径。算法涉及图的遍历、节点标记与输出编码路径等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:给一幅图的两种编码方式,现在任意给出一种编码方式,求另一种编码方式

思路:广搜。。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;

struct Point
{
	int x, y;
	Point() {}
	Point(int _x, int _y): x(_x), y(_y) {}
};
bool cmp(Point a, Point b)
{
	if (a.x != b.x) return a.x < b.x;
	return a.y < b.y;
}
Point st, cur, nxt;
string data;
bool black[110][110];
bool vis[110][110];
int n, x, y;
int dir[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
int CharToInt(char c)
{
	if (c == 'R') return 0;
	if (c == 'T') return 1;
	if (c == 'L') return 2;
	if (c == 'B') return 3;
}
char IntToChar(int c)
{
	if (c == 0) return 'R';
	if (c == 1) return 'T';
	if (c == 2) return 'L';
	if (c == 3) return 'B';
}
void bfs1()
{
	printf("%d %d\n", st.x, st.y);
	int cnt = 0;
	queue <Point> Q;
	Q.push(st);
	while (!Q.empty())
	{
		cur = Q.front();
		Q.pop();
		for (int i = 0; i < 4; ++ i)
		{
			nxt.x = cur.x + dir[i][0];
			nxt.y = cur.y + dir[i][1];
			if (black[nxt.x][nxt.y] && !vis[nxt.x][nxt.y])
			{
				vis[nxt.x][nxt.y] = true;
				printf("%c", IntToChar(i));
				Q.push(nxt);
			}
		}
		printf(++cnt == n ? ".\n" : ",\n");
	}
}
vector <Point> ans;
void bfs2()
{
	queue <Point> Q;
	ans.push_back(st);
	Q.push(st);
	y = 1;
	while (!Q.empty())
	{
		cur = Q.front();
		Q.pop();
		getline(cin, data);
		y += data.length() - 1;
		for (int i = 0; i < data.length()-1; ++ i)
		{
			x = CharToInt(data[i]);
			nxt.x = cur.x + dir[x][0];
			nxt.y = cur.y + dir[x][1];
			Q.push(nxt);
			ans.push_back(nxt);
		}
	}
	sort(ans.begin(), ans.end(), cmp);
	printf("%d\n", y);
	for (int i = 0; i < ans.size(); ++ i)
		printf("%d %d\n", ans[i].x, ans[i].y);
}
int main()
{
	getline(cin, data);
	if (data.find(' ') == -1)
	{
		memset(black, false, sizeof(black));
		memset(vis, false, sizeof(vis));
		n = 0;
		for (int i = 0; i < data.length(); ++ i)
			n = n*10 + data[i]-'0';
		scanf("%d %d", &st.x, &st.y);
		black[st.x][st.y] = true;
		vis[st.x][st.y] = true;
		for (int i = 1; i < n; ++ i)
		{
			scanf("%d %d", &x, &y);
			black[x][y] = true;
		}
		bfs1();
	}
	else
	{
		ans.clear();
		x = y = 0;
		int i = 0;
		while (data[i] != ' ')
			x = x*10 + data[i++]-'0';
		i += 1;
		while (data[i] != '\0')
			y = y*10 + data[i++]-'0';
		st = Point(x,y);
		bfs2();
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值