-
Notifications
You must be signed in to change notification settings - Fork 1
/
201903-4.cpp
85 lines (81 loc) · 1.93 KB
/
201903-4.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//
// Created by Onwaier Lee on 2019-11-04.
//
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <deque>
#include <sstream>
using namespace std;
#define MAX 10005
struct Node{
bool flag;
int num;
};
vector<deque<Node>>v(MAX);
bool mark[MAX];
int getIdx(int n){//获取第一个非空下标
for(int i = 0; i < n; ++i){
if(!v[i].empty()){
return i;
}
}
return -1;
}
bool deal(int n){
int idx = 0;
while((idx = getIdx(n)) != -1){
int cnt = 0;
while(cnt < n - 1){//避免死锁导致的死循环
bool ch = v[idx].front().flag;
int num = v[idx].front().num;
if(v[num].empty()){//空
return false;
}
if((v[num].front().flag != ch) && v[num].front().num == idx){//成对指令出队
v[idx].pop_front();
v[num].pop_front();
break;
}
else {
idx = num;//递进寻找成对指令
}
++cnt;
}
if(cnt == n - 1){//死锁
break;
}
}
return idx == -1;
}
int main(){
//freopen("//Users//onwaier//CLionProjects//CCFSolutions//a.txt", "r", stdin);
int T, n;
string s, str;
scanf("%d%d", &T, &n);
getchar();
for(int i = 0; i < T; ++i){
for(int j = 0; j < n; ++j){
if(!v[j].empty())//清空队列
v[j].clear();
getline(cin, s);
stringstream ss(s);
while(ss >> str){//将字符串分割 获取数字及指令
Node tmp;
tmp.flag = str[0] == 'R'?true:false;
tmp.num = stoi(str.substr(1));
v[j].push_back(tmp);
}
}
if(deal(n)){
printf("0\n");
}
else{
printf("1\n");
}
}
return 0;
}