QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#766530#2345. Karel the Robot_CLY_AC ✓284ms42524kbC++143.4kb2024-11-20 17:38:312024-11-20 17:38:31

Judging History

This is the latest submission verdict.

  • [2024-11-20 17:38:31]
  • Judged
  • Verdict: AC
  • Time: 284ms
  • Memory: 42524kb
  • [2024-11-20 17:38:31]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;
inline long long read(){
    long long x=0; char ch; bool f=0;
    while(((ch=getchar())<'0'||ch>'9')&&ch!='-') ;
    if(ch=='-') f=1;
    else x=ch^48;
    while((ch=getchar())>='0'&&ch<='9') x=(x<<1)+(x<<3)+(ch^48);
    return f?-x:x;
}
const int N=105;
const int dx[4]={-1,1,0,0};
const int dy[4]={0,0,-1,1};
int n,m,k1,k2;
char a[N][N];
map<string,int> fx;
string s[N];
map<char,int> rk;
char zh(int x){
	if(!x) return 'n';
	else if(x==1) return 's';
	else if(x==2) return 'w';
	else return 'e';
}
int lt(int x){
	if(!x) return 2;
	if(x==2) return 1;
	if(x==1) return 3;
	if(x==3) return 0;
}
int ok(int x,int y){
	return x>=1&&x<=n&&y>=1&&y<=m&&a[x][y]!='#';
}
struct node{
	int x,y,f;
	string v;
	bool operator<(const node &t)const{
		if(x^t.x) return x<t.x;
		if(y^t.y) return y<t.y;
		if(f^t.f) return f<t.f;
		if(v.size()!=t.v.size()) return v.size()<t.v.size();
		return v<t.v;
	}
};
struct nd{
	int x,y,f;
}to[45][45][4][28];
map<node,int> mp;
pair<string,int> get(string cz,int fi){
	string t="";
	int c=0,i=fi;
	for(i=fi;;i++){
		if(cz[i]=='('){
			c++; 
		}
		if(cz[i]==')'){
			c--;
			if(!c) break;
		}
		if(i!=fi) t+=cz[i];
	}
	return {t,i};
}
nd op(nd st,string cz);
int vis[45][45][5][28];
nd dfs(nd st,int c){
	int x=st.x,y=st.y,f=st.f;
	nd &p=to[x][y][f][c];
	if(p.x) return p;
	if(vis[x][y][f][c]){
		p.x=-1; return p;
	}
	vis[x][y][f][c]=1;
	p=op(st,s[c]);
	vis[x][y][f][c]=0;
	return p;
}

nd op(nd st,string cz){
	if(!cz.size()) return st;
	nd ts=st;
	for(int i=0;i<cz.size();){
		if(st.x==-1) return st;
		if(cz[i]=='m'){
			if(ok(st.x+dx[st.f],st.y+dy[st.f])) st.x+=dx[st.f],st.y+=dy[st.f];
			i++;
		}
		else if(cz[i]=='l'){
			st.f=lt(st.f);
			i++;
		}
		else if(cz[i]=='u'){
			string ck=""; ck+=cz[i+1];
			pair<string,int> tn=get(cz,i+2);
			string nex=tn.first; 
			int tx=st.x,ty=st.y,tf=st.f;
			bool in[45][45][5]; memset(in,0,sizeof(in));
			if(ck=="b"){
				while(ok(st.x+dx[st.f],st.y+dy[st.f])){
					if(in[st.x][st.y][st.f]){
						return {-1,-1,-1};
					}
					in[st.x][st.y][st.f]=1;
					st=op(st,nex);
					if(st.x==-1) break;
				}
			}
			else{
				while(st.f!=fx[ck]){
					if(in[st.x][st.y][st.f]){
						return {-1,-1,-1};
					}
					in[st.x][st.y][st.f]=1;
					st=op(st,nex);
					if(st.x==-1) break;
				}
			}
			i=tn.second;
			i++;
		}
		else if(cz[i]=='i'){
			string ck=""; ck+=cz[i+1];
			pair<string,int> t1=get(cz,i+2);
			string ne1=t1.first;
			i=t1.second;
			i++;
			pair<string,int> t2=get(cz,i);
			string ne2=t2.first;
			i=t2.second;
			if(ck=="b"){
				if(!ok(st.x+dx[st.f],st.y+dy[st.f])) st=op(st,ne1);
				else st=op(st,ne2);
			}
			else{
				if(st.f==fx[ck]) st=op(st,ne1);
				else st=op(st,ne2);
			}
			i++;
		}
		else{
			st=dfs(st,rk[cz[i]]);
			i++;
		}
	}
//	cout<<ts.x<<" "<<ts.y<<" "<<ts.f<<": "<<cz<<" -> "<<st.x<<" "<<st.y<<" "<<st.f<<"\n";
	return st;
}
int main(){
//	freopen("secret-06-many-steps.in","r",stdin);	 
//	freopen("t1.out","w",stdout);
	fx["n"]=0,fx["s"]=1,fx["w"]=2,fx["e"]=3;
	n=read(),m=read(),k1=read(),k2=read();
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++) scanf(" %c",&a[i][j]);
	}
	for(int i=1;i<=k1;i++){
		string S; cin>>S;
		rk[S[0]]=i;
		for(int j=2;j<S.size();j++) s[i]+=S[j];
	}
	while(k2--){
		int x=read(),y=read(); string c; cin>>c; int f=fx[c];
		string cz; cin>>cz;
		mp.clear();
		nd ans=op({x,y,f},cz);
		if(ans.x==-1) puts("inf");
		else cout<<ans.x<<" "<<ans.y<<" "<<zh(ans.f)<<"\n";
	}
	return 0;
}

Details

Test #1:

score: 100
Accepted
time: 1ms
memory: 3760kb

Test #2:

score: 0
Accepted
time: 0ms
memory: 3868kb

Test #3:

score: 0
Accepted
time: 0ms
memory: 3708kb

Test #4:

score: 0
Accepted
time: 0ms
memory: 3620kb

Test #5:

score: 0
Accepted
time: 0ms
memory: 3728kb

Test #6:

score: 0
Accepted
time: 1ms
memory: 4156kb

Test #7:

score: 0
Accepted
time: 18ms
memory: 7036kb

Test #8:

score: 0
Accepted
time: 13ms
memory: 42524kb

Test #9:

score: 0
Accepted
time: 284ms
memory: 7252kb

Test #10:

score: 0
Accepted
time: 1ms
memory: 5972kb

Test #11:

score: 0
Accepted
time: 0ms
memory: 3616kb

Test #12:

score: 0
Accepted
time: 0ms
memory: 3888kb

Test #13:

score: 0
Accepted
time: 0ms
memory: 3940kb

Test #14:

score: 0
Accepted
time: 0ms
memory: 3932kb

Test #15:

score: 0
Accepted
time: 3ms
memory: 5604kb

Test #16:

score: 0
Accepted
time: 4ms
memory: 6972kb

Test #17:

score: 0
Accepted
time: 257ms
memory: 7660kb

Test #18:

score: 0
Accepted
time: 282ms
memory: 7224kb

Test #19:

score: 0
Accepted
time: 16ms
memory: 7484kb

Test #20:

score: 0
Accepted
time: 187ms
memory: 7480kb