ABC 273 D - LRUD Instructions

D-LRUD Instructions

#include <bits/stdc++.h>
#define ll long long
using namespace std;
map<ll, vector<ll>> hh, ww;
ll h, w, rs, cs, r, s, l;
int n, q;
char x;
int main()
{
    cin >> h >> w >> rs >> cs;
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> r >> s;
        hh[r].push_back(s);
        ww[s].push_back(r);
    }
    for (auto &p : hh)p.second.push_back(0),p.second.push_back(w+1);
    for (auto &p : ww)p.second.push_back(0),p.second.push_back(h+1);
    for (auto &p : hh)
        sort(p.second.begin(), p.second.end());
    for (auto &p : ww)
        sort(p.second.begin(), p.second.end());
    cin >> q;
    for (int i = 1; i <= q; i++)
    {
        cin >> x >> l;
        if (x == 'U')
        {
            auto itt = ww.find(cs);
            if (itt != ww.end())
            {
                auto it = lower_bound(ww[cs].begin(), ww[cs].end(),rs);
                if (*it>rs)
                {
                    it--;
                    rs=max(rs-l,*it+1);
                }
                else
                    rs = max((ll)1, rs - l);
            }
            else
                rs = max((ll)1, rs - l);
        }
        if (x == 'D')
        {
            auto itt = ww.find(cs);
            if (itt != ww.end())
            {
                auto it = lower_bound(ww[cs].begin(), ww[cs].end(), rs);
                if (*it > rs && (*it <= min(rs + l, h)))
                {
                    rs = *it - 1;
                }
                else
                    rs = min(rs + l, h);
            }
            else
                rs = min(rs + l, h);
        }
        if (x == 'L')
        {
            auto itt = hh.find(rs);
            if (itt != hh.end())
            {
                auto it = lower_bound(hh[rs].begin(), hh[rs].end(),cs);
                if (*it > cs )
                {
                    it--;
                    cs=max(cs-l,*it+1);
                    
                }
                else
                    cs = max((ll)1, cs - l);
            }
            else
                cs = max((ll)1, cs - l);
        }
        if (x == 'R')
        {
            auto itt = hh.find(rs);
            if (itt != hh.end())
            {
                auto it = lower_bound(hh[rs].begin(), hh[rs].end(), cs);
                if (*it > cs && (*it <= min(w, cs + l)))
                {
                    cs = *it - 1;
                }
                else
                    cs = min(w, cs + l);
            }
            else
                cs = min(w, cs + l);
        }
        cout << rs << " " << cs << endl;
    }
    system("pause");
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值