CSP201903-4 消息传递接口(模拟)

挺nt的一个题,除了嗯模拟没想到更好的办法...大概就是每次遍历一遍所有的进程的当前通信,如果

可以收发则直接收发,然后更新当前通信;如果所有进程都不能收发则死锁。细节见代码。

#include <bits/stdc++.h>
using namespace std;	
int t, n;
struct node {
	char op;//这个通信是收还是发
	int to;//这个通信发到的/从哪里接收来的 id
	int id;//这个通信的进程id
};
vector<node> process[100005];
int p[1000005];//进程的当前通信位置

int tran(string s) {//字符串转数字,也可以stringstream
	int ans = 0;
	for(int i = 0; i < s.size(); i++) {
		ans *= 10;
		ans += (s[i] - '0');
	}
	return ans;
}
bool sisuo = 0;
signed main() {
	cin >> t >> n;
	getchar();//必须先读换行符
	while(t--) {
		sisuo = 0;
		for(int i = 0; i < n; i++) {
			process[i].clear();
			p[i] = 0;
		}
		for(int i = 0; i < n; i++) {
			char ss[1000];
			cin.getline(ss, 1000);
			string s(ss);
			s = s + ' ';
			int lst = 0;
			for(int j = 0; j < s.size(); j++) {
				if(s[j] == ' ') {
					string sub = s.substr(lst, j - lst);
					node tmp;
					tmp.op = sub[0];
					tmp.id = tran(sub.substr(1));
					tmp.to = j;
					process[i].push_back(tmp);
					lst = j + 1;
				}
			}
		}
		while(1) {
			bool flag = 0;
			int cnt = 0;
			for(int i = 0; i < n; i++) {
				if(p[i] == process[i].size()) {//这个进程到头了
					cnt++;
					continue;
				}
				if(process[i][p[i]].op == 'S') {//发送
					int to = process[i][p[i]].id;
					if(process[to][p[to]].op == 'R' && process[to][p[to]].id == i) {
						flag = 1;//如果接收的进程恰好能对上
						p[i]++;//更新p数组即当前通信的位置
						p[to]++;
					} else {
						//有可能已经死锁 也有可能to还没执行到
						continue;			
					}
				}
			}
			if(cnt == n) {//全部都到头了
				break;
			} else if(!flag) {//如果这一轮一次对上的都没有则死锁
				sisuo = 1;
				break;
			}
		}
		cout << sisuo << endl;
	}
	return 0;
}
上一篇:Python:对列表分组、拆分


下一篇:1009 说反话