CCF认证 201903-4消息传递接口

题目描述

CCF认证 201903-4消息传递接口_第1张图片

方法一:用n个队列来维护n个进程,利用递归思想解决

100分代码:

#include
using namespace std;
const int N=10010;
struct node{
	char aa;
	int bb;
};
queueq[N];
int wait[N];//用来记录当前的进程是处于阻塞状态还是就绪状态
//0代表就绪,1代表阻塞 
int t,n;
int deal(int k);
int rec(int from,int to);
int send(int from,int to);
/*void tostr(string line,char ch,int idd){//以ch分割字符串 
	int t;
	node p;
    for(t = line.find(ch); t != string::npos; t = line.find(ch))
    {
        string ss=line.substr(0,t);
        p.ch=ss[0];
        p.id=(ss[1]-'0');
        q[idd].push(p);
        line = line.substr(t+1);
    }
    p.ch=line[0];
    p.id=(line[1]-'0');
    q[idd].push(p);	
    return;
}*/
int deal(int k){//k进程的处理过程 
	if(wait[k]==1) return -1;//处于阻塞状态,不能执行
	if(q[k].empty()) return 0;//队列为空,不能执行
	//char ch=q[k].front().ch;
	//int id=q[k].front().id;
	node p=q[k].front();
	if(p.aa=='R'){
		wait[k]==1;
		if(rec(k,p.bb)==-1) return -1;
		//如果成功接收 
		
		q[k].pop();
		wait[k]=0;
		if(deal(k)==-1) return -1;//继续处理该进程 
		return 0;
	}else if(p.aa=='S'){
		wait[k]=1;
		if(send(k,p.bb)==-1) return -1;
		q[k].pop();
		wait[k]=0;
		if(deal(k)==-1) return -1;//继续处理该进程 
		return 0;
	}
}
int send(int from,int to){//from给to发送消息 
	if(wait[to]==1) return -1;
	if(q[to].empty()) return -1;
	//char ch=q[to].front().ch;
	//int id=q[to].front().id;
	node p=q[to].front();
	if(p.aa=='R'){
		//wait[to]=1;
		if(p.bb==from){
			//wait[to]=0;
			q[to].pop();
			return 0;
		}
		wait[to]=1;//无法执行,被阻塞
		if(rec(to,p.bb)==-1) return -1;//递归执行该进程时无法执行
		wait[to]=0;//递归结束后,将其状态还原
		q[to].pop();//弹出
		//观察之前的进程是否可以处理
		if(send(from,to)==0) return 0;
		return -1; 
	}else if(p.aa=='S'){//两个都是发送状态,无法进行处理 
		wait[to]=1;
		if(send(to,p.bb)==-1) return -1;
		wait[to]=0;
		q[to].pop();
		if(send(from,to)==0) return 0;
		return -1;
	}
	return -1;
}
int rec(int from,int to){//from接受来自to的消息 
//第一个是自身的参数,第二个是对方的参数 
	if(wait[to]==1) return -1;
	if(q[to].empty()) return -1;
	//char ch=q[to].front().ch;
	//char id=q[to].front().id;
	node p=q[to].front();
	if(p.aa=='R'){
		wait[to]=1;
		if(rec(to,p.bb)==-1) return -1;
		wait[to]=0;
		q[to].pop();
		if(rec(from,to)==0) return 0;
		return -1;
	}else if(p.aa=='S'){//可能相互抵消
	    //wait[to]=1;
		if(p.bb==from){
			//wait[to]=0;
			q[to].pop();
			return 0;
		}
		wait[to]=1;
		if(send(to,p.bb)==-1) return -1;
		wait[to]=0;
		q[to].pop();
		if(rec(from,to)==0) return 0;
		return -1;		
	}
	return -1;
}
int main(){
	//std::ios::sync_with_stdio(false);//优化
	scanf("%d%d",&t,&n);
	getchar();
	for(int k=0;k>tmp){
				node p;
				p.aa=tmp[0];
				tmp=tmp.substr(1);
				p.bb=stoi(tmp);
				q[j].push(p);
			}
		}

		/*char c=' ';
		for(int i=0;i#include
using namespace std;
const int N=10010;
struct node{
	char ch;
	int id;
};
queueq[N];
//vector>ve[N];
int t,n;
void tostr(string line,char ch,int idd){//以ch分割字符串 
	int t;
	//ve[idd].clear(); 
	node p;
    for(t = line.find(ch); t != string::npos; t = line.find(ch))
    {
        string ss=line.substr(0,t);
        //cout<<"ss="<

你可能感兴趣的:(CCF认证)