1008. Image Encoding @ Timus Online Judge
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define pii pair<int,int>
string s;
bool pd(string s)
{
for (int i = 0; i < s.size(); i++)
if (s[i] == ' ' && i < s.size() - 1)
return 0;
return 1;
}
bool Map[15][15];
int minx = 999999999, miny = 999999999;
queue<pair<int, int>> q;
int px[5] = { 0,1,0,-1,0 };
int py[5] = { 0,0,1,0,-1 };
char p[5] = { 0,'R','T','L','B' };
int t;
void fset(string s)
{
int n = 0;
for (int i = 0; i < s.size() && s[i] >= '0' && s[i] <= '9'; i++)
{
n *= 10;
n += s[i] - '0';
}
for (int i = 1; i <= n; i++)
{
int x, y;
cin >> x >> y;
Map[x][y] = 1;
if (i == 1)
minx = x, miny = y;
}
pair<int, int > start;
start.first = minx, start.second = miny;
q.push(start);
Map[minx][miny] = 0;
cout << minx << ' ' << miny << endl;
while (q.size())
{
pii now = q.front();
for (int i = 1; i <= 4; i++)
{
int cmpx = now.first + px[i];
int cmpy = now.second + py[i];
if (Map[cmpx][cmpy] == 1)
{
cout << p[i];
Map[cmpx][cmpy] = 0;
pii next;
next.first = cmpx;
next.second = cmpy;
q.push(next);
}
}
if (q.size() == 1)
cout << '.' << endl;
else
cout << "," << endl;
q.pop();
}
return;
}
void sset(string s)
{
int i = 0;
int x = 0, y = 0;
while (s[i] != ' ')
{
x *= 10;
x += s[i] - '0';
i++;
}
i++;
while (i<s.size())
{
y *= 10;
y += s[i] - '0';
i++;
}
pii start;
start.first = x;
start.second = y;
q.push(start);
Map[x][y] = 1;
t++;
while (q.size())
{
string s;
cin >> s;
pii now = q.front();
for (int i = 0; i < s.size(); i++)
{
if (s[i] == 'L')
{
pii cmp;
cmp.first = now.first -1;
cmp.second = now.second;
Map[cmp.first][cmp.second] = 1;
q.push(cmp);
t++;
}
if (s[i] == 'T')
{
pii cmp;
cmp.first = now.first;
cmp.second = now.second+1;
Map[cmp.first][cmp.second] = 1;
q.push(cmp);
t++;
}
if (s[i] == 'R')
{
pii cmp;
cmp.first = now.first +1;
cmp.second = now.second;
Map[cmp.first][cmp.second] = 1;
q.push(cmp);
t++;
}
if (s[i] == 'B')
{
pii cmp;
cmp.first = now.first ;
cmp.second = now.second-1;
Map[cmp.first][cmp.second] = 1;
q.push(cmp);
t++;
}
}
q.pop();
}
cout << t << endl;
for (int i = 1; i <= 10; i++)
for (int j = 1; j <= 10; j++)
if (Map[i][j] == 1)
cout << i << ' ' << j << endl;
return;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
// freopen(".in", "r", stdin);
getline(cin, s);
if (pd(s))//第一种表示转第二种表示
fset(s);
else
sset(s);//第二种表示转第一种表示
return 0;
}