CCF-CSP201903-4消息传递接口
试题编号: 201903-4
试题名称: 消息传递接口
时间限制: 1.0s
内存限制: 512.0MB
问题描述:
这是一个来自操作系统的问题,要求判断是否会产生死锁,当发送方与接收方不匹配的时候就陷入了死锁。简单的模拟一下递归过程,用队列来记录每一组操作,p表示从头开始遍历的操作,q表示p指向的操作,如果pq刚好匹配,则出队,若不匹配,则用v数组记录该组已经遍历,若v均为1,则表示所有的操作遍历完毕。此时判断所有队列,若均为空,则输出没有死锁。
#include<bits/stdc++.h>
using namespace std;
struct mmp
{
int num;
bool flag;
};
queue<mmp>str[10005];
void clear(queue<mmp>& q)
{
queue<mmp> empty;
swap(empty, q);
}
int main()
{
int t,n;
cin>>t>>n;
getchar();
while(t--)
{
for(int i=0; i<n; i++)
clear(str[i]);
string line;
for(int i=0; i<n; ++i)
{
getline(cin,line);
stringstream ss(line);
string tmp;
mmp temp;
while(ss>>tmp)
{
temp.flag=tmp.substr(0,1)=="S"?true:false;
temp.num=stoi(tmp.substr(1));
str[i].push(temp);
}
}
int v[10005] = {0},is_change = 1;
while(is_change)
{
int p=0,q;
memset(v,0,sizeof(v));
is_change=0;
while(p < n && str[p].empty())
p++;
if(p==n)
break;
q=str[p].front().num;
while(!is_change&&!v[q]&&!str[q].empty())
{
v[q]=1;
if(str[q].front().num==p&&str[p].front().flag!=str[q].front().flag)
{
is_change=1;
str[p].pop();
str[q].pop();
break;
}
p=q;
q=str[p].front().num;
}
}
bool ans=true;
for(int i=0; i<n; ++i)
if(!str[i].empty())
{
ans=false;
break;
}
if(ans)
cout<<0<<endl;
else
cout<<1<<endl;
}
return 0;
}
/*
3 2
R1 S1
S0 R0
R1 S1
R0 S0
R1 R1 R1 R1 S1 S1 S1 S1
S0 S0 S0 S0 R0 R0 R0 R0
*/
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。