QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#663165 | #2345. Karel the Robot | YMH_fourteen | RE | 300ms | 6684kb | C++14 | 2.8kb | 2024-10-21 13:52:06 | 2024-10-21 13:52:06 |
Judging History
answer
// Author: YE Minghan
#include<bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#include "templates/debug.h"
#else
#define dbg(...) (void)0
#define msg(...) (void)0
#endif
#define ll long long
#define endl '\n'
#define PB emplace_back
#define PPB pop_back
#define MP make_pair
#define ALL(Name) Name.begin(),Name.end()
#define PII pair<int,int>
#define VI vector<int>
#define GI greater<int>
#define fi first
#define se second
int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};
int R,C,m,q;
string mp[42];
int did(char c){return c=='n'?0:c=='w'?1:c=='s'?2:3;}
char chr(int d){return d==0?'n':d==1?'w':d==2?'s':'e';}
struct state
{
int x,y,d;
state():x(-1),y(-1),d(-1){}
state(int x,int y,int d):x(x),y(y),d(d){}
void forward()
{
int xx=x+dx[d],yy=y+dy[d];
if(mp[xx][yy]=='.')x=xx,y=yy;
}
void left(){d=(d+1)&3;}
bool cond(char c)
{
if(c=='b')return mp[x+dx[d]][y+dy[d]]=='#';
return d==did(c);
}
bool operator<(const state &rhs)const
{
if(x!=rhs.x)return x<rhs.x;
if(y!=rhs.y)return y<rhs.y;
return d<rhs.d;
}
};
state rd()
{
int x,y;char d;
cin>>x>>y>>d;
return state(x,y,did(d));
}
void output(state x)
{
if(x.x==-2)cout<<"inf\n";
else cout<<x.x<<" "<<x.y<<" "<<chr(x.d)<<endl;
}
void Print_(state x) // debug
{
if(x.x==-2)cerr<<"inf";
else cerr<<"["<<x.x<<","<<x.y<<","<<chr(x.d)<<"]";
}
int fp(string &s,int sp)
{
int d=0;
for(int i=sp;;i++)
if(!~(d+=(s[i]=='(')-(s[i]==')')))return i;
}
string cmd[26];
map<pair<string,state>,state>trs;
set<pair<string,state>>vising;
state exec(state,string);
state exec(state st,char c)
{
if(st.x==-2)return st;
if(c=='m')st.forward();
else if(c=='l')st.left();
else st=exec(st,cmd[c-'A']);
return st;
}
state exec(state st,string s)
{
dbg(st,s);
auto &cr=trs[MP(s,st)];
if(vising.count(MP(s,st)))return state(-2,-2,-2);
if(cr.x!=-1)return cr;
vising.emplace(s,st);
state St=st;
for(int i=0;i<s.size();i++)
{
if(s[i]=='i')
{
int t=fp(s,i+3),u=fp(s,t+2);
st=exec(st,st.cond(s[i+1])?s.substr(i+3,t-i-3):s.substr(t+2,u-t-2));
i=u;
}
else if(s[i]=='u')
{
set<state>vis;
int t=fp(s,i+3);
while(!st.cond(s[i+1]))
{
vis.insert(st);
if(vis.count(st=exec(st,s.substr(i+3,t-i-3)))||st.x==-2)
{
st=state(-2,-2,-2);
break;
}
}
i=t;
}
else st=exec(st,s[i]);
}
vising.erase(MP(s,St));
dbg(st);
return cr=st;
}
int main()
{
ios::sync_with_stdio(false),cin.tie(nullptr);
// int _;cin>>_;while(_--)
cin>>R>>C>>m>>q;
mp[0]=mp[R+1]="#########################################";
for(int i=1;i<=R;i++)cin>>mp[i],mp[i]="#"+mp[i]+"#";
while(m--)
{
string s;cin>>s;
cmd[s[0]-'A']=s.substr(2);
}
while(q--)
{
msg("Q");
state st=rd();string s;cin>>s;
output(exec(st,s));
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3644kb
Test #2:
score: 0
Accepted
time: 1ms
memory: 3844kb
Test #3:
score: 0
Accepted
time: 1ms
memory: 3768kb
Test #4:
score: 0
Accepted
time: 1ms
memory: 3484kb
Test #5:
score: 0
Accepted
time: 1ms
memory: 3840kb
Test #6:
score: 0
Accepted
time: 7ms
memory: 3936kb
Test #7:
score: 0
Accepted
time: 300ms
memory: 6684kb
Test #8:
score: -100
Runtime Error