CCF-CSP认证 201903-4

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

1、 使用n个队列,开个无限循环,每次遍历所有队列里面的元素,如果有消去现象,则循环继续,否则已经进入死锁,于是跳出循环break。

2、 最后判断所有队列是否为空,来确定是否消去完毕。

对于一行的输入问题,我这里使用的流操作,因为这是我想到的最简单的方式了,使用stringstream虽然方便,但是效率较低,但是这道题没有超时问题,我一次提交直接100分╰(°▽°)╯
建议大家学一学我这个代码里面的流操作,用起来很舒服的,我做题目的时候发现还很常用的。
如果喜欢这篇博文或则看了这篇博文对你的学习有帮助,恳请你给个赞?b( ̄▽ ̄)d
有什么问题可以在下面留言啊

#include <iostream>
#include <cstdio>
#include <queue>
#include <sstream>
#include <cstdlib>
#include <algorithm>
using namespace std;
int T,n;
const int maxn=10005;
string str[maxn];
queue<int> que[maxn]; //maxn个队列
void clear_que(queue<int> &q)
{
    queue<int> emp;
    swap(emp,q);
}
int main()
{
    cin>>T>>n;
    getchar();
    for(int i=0;i<T;i++)
    {
        for(int j=0;j<n;j++) //清空
            clear_que(que[j]);
        for(int j=0;j<n;j++)
        {
            getline(cin,str[j]);
            stringstream ss(str[j]);
            string s;
            while(ss>>s)
            {
                char c=s[0];
                s.erase(s.begin());
                int num=atoi( s.c_str() );
                if(c=='R')
                    num=num+maxn; //大于maxn的表示是回复
                que[j].push(num);
            }
        }
        while(true)
        {
            bool bk=true;
            for(int j=0;j<n;j++)
            {
                while(!que[j].empty())
                {
                    int a=que[j].front();
                    if(a>=maxn && !que[a-maxn].empty() && que[a-maxn].front()==j)
                    {
                        bk=false;
                        que[j].pop();
                        que[a-maxn].pop();
                    }
                    else if(a<maxn&& !que[a].empty() && que[a].front()==j+maxn){
                        bk=false;
                        que[j].pop();
                        que[a].pop();
                    }
                    else
                        break;
                }
            }
            if(bk) //没有队列出队
                break;
        }
        bool OK=true;
        for(int j=0;j<n;j++)
            if(!que[j].empty())
        {
            OK=false;
            break;
        }
        if(OK)
            printf("0\n");
        else
            printf("1\n");
    }
    return 0;
}

发布了174 篇原创文章 · 获赞 18 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_41173604/article/details/100671254