题解:P8070 [BalticOI 2002] Moving Robots (Day2)

· · 题解

随到的题目,看没有题解,先来一发

P8070 [BalticOI 2002] Moving Robots (Day2)

题目描述

R 个机器人在二维平面运动,可前进或转向,删除最小的命令个数,使得所有机器人停在同一位置

数据范围

2 \le R \le 10$,$1 \le n \le 50$,$-50 \le x, y \le 50
#include<bits/stdc++.h>
using namespace std;
int r,n,x,y,c,ans[11][210][210][4],d,dd[210][210];
struct wz{int x,y,c,w;};
void wd(int r){
    vector<wz>q;
    for(int x=1;x<=200;x++)
        for(int y=1;y<=200;y++)
            for(c=0;c<4;c++)
                if(ans[r][x][y][c]<=n and ans[r][x][y][c]>0){
                    wz kk={x,y,c,ans[r][x][y][c]};
                    q.emplace_back(kk); }                               

    for(auto t:q){
        int x=t.x,y=t.y,c=t.c,w=t.w;
        if(c==0)ans[r][x+1][y][c]=min(ans[r][x+1][y][c],w-1);
        if(c==1)ans[r][x][y+1][c]=min(ans[r][x][y+1][c],w-1);   
        if(c==2)ans[r][x-1][y][c]=min(ans[r][x-1][y][c],w-1);
        if(c==3)ans[r][x][y-1][c]=min(ans[r][x][y-1][c],w-1);           
    }
}
void turn(int r,int d){
    vector<wz>q;
    for(int x=1;x<=200;x++)
        for(int y=1;y<=200;y++)
            for(c=0;c<4;c++)
                if(ans[r][x][y][c]<=n and ans[r][x][y][c]>0){
                    wz kk={x,y,c,ans[r][x][y][c]};
                    q.emplace_back(kk); }
    for(auto t:q){
        int x=t.x,y=t.y,c=t.c,w=t.w;    
        ans[r][x][y][(c+d)%4]=min(ans[r][x][y][(c+d)%4],w-1);
    }       
}
int main(){
    cin>>r;
    for(int i=1;i<=r;i++)
        for(int x=1;x<=200;x++)
            for(int y=1;y<=200;y++){
                for(c=0;c<4;c++)ans[i][x][y][c]=100;
            }
    for(int i=1;i<=r;i++){
        cin>>x>>y>>c>>n;
        x+=100,y+=100,c/=90;
        ans[i][x][y][c]=n;
        for(int k=1;k<=n;k++){
            char s;
            cin>>s;
            if(s=='S'){
                wd(i);
            }else {
                cin>>d;
                d/=90;
                turn(i,d);
            }
        }
    }
    for(int x=1;x<=200;x++)
        for(int y=1;y<=200;y++)
            for(int i=1;i<=r;i++){
                int kk=100;
                for(c=0;c<4;c++)kk=min(kk,ans[i][x][y][c]);
                if(kk>=100){dd[x][y]=-1;break;}
                dd[x][y]+=kk;
            }
    int mi=100,mx=0,my=0;
    for(int x=1;x<=200;x++){
        for(int y=1;y<=200;y++){
            if(dd[x][y]<=mi and dd[x][y]>=0 and dd[x][y]<100){
                mi=dd[x][y];
                mx=x-100,my=y-100;
            }
        }   
    }   
    if(mi==100)cout<<-1;
    else {
        cout<<mi<<endl;
        cout<<mx<<' '<<my;
    }
    return 0;
}