求助#1TLE
查看原帖
求助#1TLE
736237
codingwen楼主2025/2/2 12:44

rt,大样例过了。

#include<bits/stdc++.h>
using namespace std;
string cmd[110][11];
string lst;
int l[110],r[110];
int tri=0;
int stp=0,n,m,k;
int getnum(string str,int p,int &num){//在str中,从p开始,将数存到num里,返回数后空格的下标。 
	while(isdigit(str[p])){
		num=num*10+str[p]-'0';
		p++;
	}
	return p;
}
void mk(char &c){c=(!(c-'0'))+'0';}//字符取反 
string mirase(string str){//将str执行mirror操作,返回执行后的串 
	if(str[0]=='S' && str[1]=='W' || str[0]=='M' && str[1]=='O')mk(str[5]);//swap或move 
	if(str[0]=='M' && str[1]=='I')mk(str[7]);//mirror
	if(str[0]=='R')mk(str[8]);//replace
	if(str[0]=='A')mk(str[9]);//activate
	return str;
}
void mirror(string &str){
	if(str[0]!='T')str=mirase(str);//不是trigger 
	else{
		int w=str.find(":");//查找冒号 
		str=str.substr(0,w+2)+mirase(str.substr(w+2));//拼接 
	}
}
void go(int id);
void excu(int id,string str){
	if(str[0]=='T')return ;//是trigger 
	tri=0;//trigger不是上一个执行的了 
	stp++;//步数加一 
	if(str[0]=='S' && str[1]=='L'){//slackoff
		printf("Robot %d slacks off.\n",id);
		lst="SLACKOFF";//上一个执行的指令叫slackoff 
	}
	if(str[0]=='M' && str[1]=='O'){//move
		int h=0,z=0;
		int p=getnum(str,5,h);
		getnum(str,p+1,z);//获取两个数 
		if(!h){//左 
			l[id]=(l[id]+z)%n;
			printf("Robot %d moves its left hand towards Robot %d.\n",id,l[id]);
		}else{//右 
			r[id]=(r[id]+z)%n;
			printf("Robot %d moves its right hand towards Robot %d.\n",id,r[id]);
		}
		lst="MOVE";//同上 
	}
	if(str[0]=='S' && str[1]=='W'){//swap
		int h=0,x=0,y=0;
		int p=getnum(str,5,h);
		p=getnum(str,p+1,x);
		getnum(str,p+1,y);//获取
		//执行 
		if(!h)swap(cmd[l[id]][x],cmd[id][y]);
		else swap(cmd[r[id]][x],cmd[id][y]);
		//-------
		printf("Robot %d swaps a line of command with Robot %d.\n",id,(h?r[id]:l[id]));
		lst="SWAP";//同上 
	}
	if(str[0]=='M' && str[1]=='I'){//mirror
		int h=0,x=0;
		int p=getnum(str,7,h);
		getnum(str,p+1,x);//获取 
		printf("Robot %d modifies a line of command of Robot %d.\n",id,(h?r[id]:l[id]));
		mirror(cmd[(h?r[id]:l[id])][x]);//执行mirror 
		lst="MIRROR";//同上 
	}
	if(str[0]=='R'){//replace
		int h=0,x=0;
		int p=getnum(str,8,h);
		p=getnum(str,p+1,x);//获取 
		printf("Robot %d replaces a line of command of Robot %d.\n",id,(h?r[id]:l[id]));
		cmd[(h?r[id]:l[id])][x]=str.substr(p+1);//修改 
		lst="REPLACE";//同上 
	}
	if(str[0]=='A'){//activate
		int h=0;
		getnum(str,9,h);//获取 
		printf("Robot %d activates Robot %d.\n",id,(h?r[id]:l[id]));
		go((h?r[id]:l[id]));//执行所有代码 
		lst="ACTIVATE";//同上 
	}
	if(stp==k)exit(0);//步数到了 
}
int chtri(int i){//查找并运行trigger指令,返回下一个可以递归的机器人 
	for(int j=1;j<=m;j++){
		string str=cmd[i][j];
		if(str[0]=='T'){
			int w=str.find(":");
			string ding=str.substr(8,w-8);//要求的指令 
			if(ding==lst || (ding=="TRIGGER" && tri)){//相同或者上一个是trigger指令 
				excu(i,str.substr(w+2));//运行trigger 
				tri=1;//运行过trigger 
				if(r[i]!=i)return r[i];//可以递归 
				break;//因为是最前面的,所以跳出循环 
			}
		}
	}
	return -1;//没有可递归的机器人 
}
void go(int id){//激活,运行所有代码 
	if(stp==k)exit(0);//步数到了 
	for(int i=1;i<=m;i++){
		excu(id,cmd[id][i]);//运行当前指令 
		if(cmd[id][i][0]!='T' && r[id]!=id){//如果执行了,并且右手不指向自己 
			int now=r[id];//当前递归到哪个机器人 
			while((now=chtri(now))!=-1);//无限循环,直到无法继续递归 
		}
	}
}
int main(){
	//////////读入开始//////////////
	string emppp="";//过渡串 
	cin>>n>>m>>k;
	for(int i=0;i<n;i++){
		cin>>l[i]>>r[i];
		getline(cin,emppp); //读入l和r的空行 
		for(int j=1;j<=m;j++)getline(cin,cmd[i][j]);//输入指令 
	}
	//////////读入结束&运行开始//////////////
	int now=0;//当前机器人 
	while(true){//无限循环 
		go(now);//激活第 now 个机器人 
		now=(now+1)%n;//下一个 
	}
	return 0;
} 
2025/2/2 12:44
加载中...