zoukankan      html  css  js  c++  java
  • 201903-4 消息传递接口(队列模拟)

    样例1

    输入:

    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

    输出:

    0

    1

    0

    样例二:

    输入:

    2 3
    R1 S1
    R2 S0 R0 S2
    S1 R1
    R1
    R2 S0 R0
    S1 R1

    输出:

    0

    1

    思路:

    1.建一个结构体,存x S/R y  ,注意,x 可能不止一位数。用队列顺序存储每个进程。

    2.用getchar()来取字符串后面的字符,如果取到的是换行,那么该进程结束,去取下一个进程。

    3.如果顺序遍历每个进程都没有匹配成功的话,匹配结束,如果还存在队列不为空的话,则发生了死锁。

    #include <algorithm>
    #include <iostream>
    #include <cstdio>
    #include <queue>
    using namespace std;
    const int maxn = 1e5 + 5;
    struct Node {
        int x,z;
        char y;
    };
    int main() {
        int t,n;
        cin >> t >> n;
        while(t--) {
            queue<Node>q[n+1];
           for(int i = 0; i < n; i++) {
            while(!q[i].empty())
                q[i].pop();
           }
            for(int i = 0; i < n; i++) {
                string s;
                char c;
                while(cin >> s) {
                    c = getchar();
                    Node temp;
                    temp.x = i;
                    temp.y = s[0];
                    temp.z = 0;
                    for(int j = 1; j < s.length(); j++){
                        temp.z = temp.z*10 + s[j] - '0';
                    }
                    q[i].push(temp);
                    if(c == '
    ') {
                        break;
                    }
                }
            }
            int flag = 0;
            int k = 0;
            while(1) {
    
                k = 0;
                for(int i = 0; i < n; i++){
                if(q[i].empty()) continue;
                Node temp = q[i].front();
                int x = temp.x;
                char y ;
                int z = temp.z;
                if(temp.y == 'R')  y = 'S';
                else y = 'R';
                if(q[z].empty()) {
                    flag = 1;
                    break;
                }
                Node temp2 = q[z].front();
                if(temp2.z == x && temp2.y == y) {
                    q[i].pop();
                    q[z].pop();
                    i--;
                    k++;
                }
                else {
                    continue;
                }
            }
            if(k == 0) break;
            }
            k = 0;
            for(int i = 0; i < n; i++) {
                    if(!q[i].empty()){
                      flag = 1;
                      break;
                    }
                }
            if(flag == 1) cout << 1 << endl;
            else cout << 0 <<endl;
        }
        return 0;
    }
  • 相关阅读:
    beego——过滤器
    beego——session控制
    Differentiation 导数和变化率
    验证码识别
    pip 下载慢
    ORB
    决策树
    机器学习第二章 配对网站
    K-近邻算法
    ubuntu下安装配置OpenCV
  • 原文地址:https://www.cnblogs.com/LLLAIH/p/11664182.html
Copyright © 2011-2022 走看看