結果

問題 No.5006 Hidden Maze
ユーザー HaaHaa
提出日時 2022-06-12 16:10:32
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 114 ms / 2,000 ms
コード長 2,995 bytes
コンパイル時間 1,889 ms
実行使用メモリ 22,864 KB
スコア 81,182
平均クエリ数 189.18
最終ジャッジ日時 2022-06-12 16:16:52
合計ジャッジ時間 9,796 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 100
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()
template<typename T> bool chmax(T &x, const T &y) {return (x<y)?(x=y,true):false;};
template<typename T> bool chmin(T &x, const T &y) {return (x>y)?(x=y,true):false;};

const int dx[4]={1,0,-1,0}, dy[4]={0,1,0,-1};
const string dir="RDLU";
map<char,int> rid={{'R',0},{'D',1},{'L',2},{'U',3}};

int H, W, P;
VI rea(20*20,0);
VVI wall(20*20,VI(20*20,1));

inline int id(int x, int y){
    return x*W+y;
}

string newroute(){
    VI pre(H*W,-1); pre[id(0,0)]=-2;
    queue<pair<int,int>> que, sque; que.push({0,0});
    vector<pair<int,int>> ps;
    while(!que.empty()){
        int x=que.front().first;
        int y=que.front().second;
        que.pop();
        VI perm={0,1,2,3};
        random_shuffle(ALL(perm));
        REP(i,4){
            int tx=x+dx[perm[i]];
            int ty=y+dy[perm[i]];
            if(tx<0||tx>=H||ty<0||ty>=W)
                continue;
            if(pre[id(tx,ty)]!=-1)
                continue;
            if(rea[id(tx,ty)]){
                if(!wall[id(x,y)][id(tx,ty)]){
                    pre[id(tx,ty)]=perm[i];
                    que.push({tx,ty});
                }
            }
            else if(wall[id(x,y)][id(tx,ty)]<rand()%10){
                pre[id(tx,ty)]=perm[i];
                ps.push_back({tx,ty});
            }
        }
    }
    random_shuffle(ALL(ps));
    for(auto p:ps) sque.push(p);
    while(!sque.empty()){
        int x=sque.front().first;
        int y=sque.front().second;
        sque.pop();
        VI perm={0,1,2,3};
        random_shuffle(ALL(perm));
        REP(i,4){
            int tx=x+dx[perm[i]];
            int ty=y+dy[perm[i]];
            if(tx<0||tx>=H||ty<0||ty>=W)
                continue;
            if(pre[id(tx,ty)]!=-1)
                continue;
            if(!rea[id(tx,ty)]&&wall[id(x,y)][id(tx,ty)]<rand()%10){
                pre[id(tx,ty)]=perm[i];
                sque.push({tx,ty});
            }
        }
    }
    int px=H-1, py=W-1;
    if(pre[id(px,py)]==-1)
        return newroute();
    string ret="";
    while(px!=0||py!=0){
        int p=pre[id(px,py)];
        ret+=dir[p];
        px-=dx[p];
        py-=dy[p];
    }
    reverse(ALL(ret));
    return ret;
}

void solve(){
    rea[id(0,0)]=1;
    cin >> H >> W >> P;
    int X;
    REP(i,1001){
        string s=newroute();
        cout << s << endl;
        cin >> X;
        if(X==-1)
            break;
        int px=0, py=0;
        REP(j,X+1){
            int npx=px+dx[rid[s[j]]];
            int npy=py+dy[rid[s[j]]];
            if(j<X){
                wall[id(px,py)][id(npx,npy)]=0;
                rea[id(npx,npy)]=1;
            }
            else if(wall[id(px,py)][id(npx,npy)]!=0){
                wall[id(px,py)][id(npx,npy)]++;
            }
            px=npx;
            py=npy;
        }
    }
}

int main(){
    solve();
    return 0;
}
0