1008. Image Encoding

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;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值