题解:ABC273-D - LRUD Instructions

题解:ABC273-D - LRUD Instructions

·题目

链接:Atcoder。

链接:洛谷。

·难度

算法难度:普及。

思维难度:普及。

调码难度:提高。

综合评价:困难。

·算法

二分+模拟。

·思路

把每个障碍物分别按照先行再列与先列再行双关键字排序,每次通过二分查找找到在它前进路上离他最近的障碍物,若找不到,移动后就在边界和正常移动后位置中就近选择,反之,则在障碍物往前一格与正常移动后位置中就近选择。这样一步一步推出每一步的位置。

·细节

排序用cmp+sort,不同排序用hyx和lyx分别记录。

·代码

#include
#define N 220000
using namespace std;
struct Info{
    long long x,y;
};
Info hyx[N]={},lyx[N]={};
long long hr=0,wc=0;
int c[N]={},r[N]={},cnt=0,h=0,n=0,w=0,zq=0;
bool cmp_h(Info a,Info b);
bool cmp_l(Info a,Info b);
int main(){
    scanf("%d%d%lld%lld%d",&h,&w,&hr,&wc,&n);
    for(int i=1;i<=n;i++){
        scanf("%d%d",&r[i],&c[i]);
        cnt++;
        hyx[cnt]=lyx[cnt]={r[i],c[i]};
    }
    sort(hyx+1,hyx+1+cnt,cmp_h);
    sort(lyx+1,lyx+1+cnt,cmp_l);
    scanf("%d",&zq);
    for(int i=1;i<=zq;i++){
        assert(getchar()=='\n');
        int s=0;
        char d='\0';
        scanf("%c%d",&d,&s);
        if(d=='L'){
            int l1=0,r1=cnt+1;
            bool flag=false;
            while(l1+1wc){
                flag=true;
            }
            if(flag==true){
                wc-=s;
                if(wc<=0){
                    wc=1;
                }
            }else{
                wc-=s;
                if(wc<=hyx[l3].y){
                    wc=hyx[l3].y+1;
                }
            }
            printf("%lld %lld\n",hr,wc);
        }else{
            if(d=='R'){
                int l1=0,r1=cnt+1;
                bool flag=false;
                while(l1+1=w+1){
                        wc=w;
                    }
                }else{
                    wc+=s;
                    if(wc>=hyx[r3].y){
                        wc=hyx[r3].y-1;
                    }
                }
                printf("%lld %lld\n",hr,wc);
            }else{
                if(d=='U'){
                    int l1=0,r1=cnt+1;
                    bool flag=false;
                    while(l1+1hr){
                        flag=true;
                    }
                    if(flag==true){
                        hr-=s;
                        if(hr<=0){
                            hr=1;
                        }
                    }else{
                        hr-=s;
                        if(hr<=lyx[l3].x){
                            hr=lyx[l3].x+1;
                        }
                    }
                    printf("%lld %lld\n",hr,wc);
                }else{
                    int l1=0,r1=cnt+1;
                    bool flag=false;
                    while(l1+1=h+1){
                            hr=h;
                        }
                    }else{
                        hr+=s;
                        if(hr>=lyx[r3].x){
                            hr=lyx[r3].x-1;
                        }
                    }
                    printf("%lld %lld\n",hr,wc);
                }
            }
        }
    }
    return 0;
}
bool cmp_h(Info a,Info b){
    if(a.x==b.x){
        return a.y

·注意

分情况讨论时一定要理清思路,慢慢写,或者写一个函数直接调用,不要搞乱。

你可能感兴趣的:(算法,二分查找)