题意:给一幅图的两种编码方式,现在任意给出一种编码方式,求另一种编码方式
思路:广搜。。。
#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();
}
}