https://zhidao.baidu.com/question/495929025214224484.html

#include<bits/stdc++.h>
#include<windows.h> 
using namespace std;
/*

执行语句
m		前进1(前方有障碍物,操作无效)
l		左转90
X		调用X语句
i		如果
u		直到

条件表达式
b		当前方向前面一格是否有障碍物
n,s,e,w	四个方向前面一格是否有障碍物


*/
int r,c,d,e;
int Kx,Ky;char Kf;
bool Flag=0;int dep=0;
map<char,string>def;
char Map[45][45];
string get_r(string s,int st,int end=-1)
{
	if(end==-1)end=s.size()-1;
	string g="";
	for(int i=st;i<=end;i++)
		g+=s[i];
	return g;
}
bool crash_check(int x,int y)
{
	if(x<1 || x>r || y<1 || y>c)return 1;
	if(Map[x][y]=='#')return 1;
	return 0;
}
void forward()
{
	if(Kf=='n' && !crash_check(Kx-1,Ky))Kx-=1;
	if(Kf=='s' && !crash_check(Kx+1,Ky))Kx+=1;
	if(Kf=='e' && !crash_check(Kx,Ky+1))Ky+=1;
	if(Kf=='w' && !crash_check(Kx,Ky-1))Ky-=1;
}
void left()
{
	if(Kf=='n')Kf='w';
	if(Kf=='e')Kf='n';
	if(Kf=='s')Kf='e';
	if(Kf=='w')Kf='s';
}
bool check(char f)
{
	if(f=='b')return check(Kf);
	if(f=='n' && !crash_check(Kx-1,Ky))return 1;
	if(f=='s' && !crash_check(Kx+1,Ky))return 1;
	if(f=='e' && !crash_check(Kx,Ky+1))return 1;
	if(f=='w' && !crash_check(Kx,Ky-1))return 1;
	return 0;
}
string get_kh(string rs,int st)
{
	string g;int id;
	stack<char>kh;
	kh.push('(');
	for(int i=st+1;i<rs.size();i++)
	{
		if(rs[i]==')')
			kh.pop();
		if(rs[i]=='(')
			kh.push('(');
		if(kh.empty())
			break;
		g+=rs[i];
	}
	return g;
}
void run_code(string rs)
{
	if(rs=="")return;
	if(Flag)return;
	if(++dep>1000)
	{
		Flag=1;return;
	}
	cout<<"["<<rs[0]<<","<<dep<<"]";
	if(rs[0]=='m')
	{
		forward();
		run_code(get_r(rs,1));
	}
	else if(rs[0]=='l')
	{
		left();
		run_code(get_r(rs,1));
	}
	else if(rs[0]=='i')
	{
		char pd=rs[1];
		string b1="",b2="";
		b1=get_kh(rs,2);
		b2=get_kh(rs,4+b1.size());
		if(!check(pd))
			run_code(b1);
		else
			run_code(b2);
		if(Flag)return;
		run_code(get_r(rs,6+b1.size()+b2.size()));
	}
	else if(rs[0]=='u')
	{
		char pd=rs[1];
		string b1="";
		b1=get_kh(rs,2);
		while(check(pd))
			run_code(b1);
		if(Flag)return;
		run_code(get_r(rs,4+b1.size()));
	}
	else
	{
		string rpr=def[rs[0]];
		run_code(rpr);
		if(Flag)return;
		run_code(get_r(rs,1));
	}
}
int main()
{
//	ios::sync_with_stdio(0);
//	cin.tie(0);
	Sleep(1000);
	keybd_event(VK_LWIN,0,0,0);
	keybd_event(58,0,0,0);
	keybd_event(55,0,0,0);
	keybd_event(55,0,0,0);
	cin>>r>>c>>d>>e;
	for(int i=1;i<=r;i++)
		for(int j=1;j<=c;j++)
			cin>>Map[i][j];
	for(int i=1;i<=d;i++)
	{
		string s;
		cin>>s;
		char name=s[0];
		string pr=get_r(s,2);
		def[name]=pr;
	}
	for(int i=1;i<=e;i++)
	{
		dep=0;
		cin>>Kx>>Ky>>Kf;
		string rpr;cin>>rpr;
		run_code(rpr);
		if(Flag)(cout<<"inf\n"),Flag=0;
		else cout<<Kx<<" "<<Ky<<" "<<Kf<<"\n";
	}
	return 0;
}
/*
4 8 5 7
.......#
..#....#
.###...#
.....###
A=ub(m)l
B=ub(l)
C=ub(m)lub(m)l
D=ub(m)lub(m)lD
E=E
*/