模拟,对于每一个transition,记录从该transition开始到相应的place所导致的place中的token数量的变化,对于某个place而言,place扮演的是输入的角色,那么如果相应的fire发生,则会导致place中的token数量减少1,反之,如果扮演的是接收transition输入的角色,那么相应的fire发生,会导致place中的token数量增加1。然后开始模拟,一共可以模拟的轮数不能超过制定的数量,同时对于每一轮的模拟,要分别分析每一个transition,如果transition所代表的操作能够发生(也就是对于该transition而言,可以得到足够的输入,同时可以产生足够的输出),那么fire数量增1,同时要更新相应的fire发生后,每个place中的token数量。每次结束后要判断当前产生的fire数量是否达到要求,如果达到要求,就可以直接进行最后的输出操作。具体实现见如下代码:
#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
using namespace std;
int Np, Nt, Nf;
int place[110];
int amount[110][110];
int main(){
int Case = 0;
while (cin >> Np){
memset(place,0,sizeof(place));
memset(amount,0,sizeof(amount));
Case++;
if (Np == 0) break;
for (int i = 1; i <= Np; i++) cin >> place[i];
cin >> Nt;
for (int i = 1; i <= Nt; i++){
int t;
while (cin >> t){
if (t == 0) break;
else if (t > 0) amount[i][t]++;
else amount[i][-t]--;
}
}
cin >> Nf;
int round = 0;
for (int i = 1; i <= Nf; i++){
for (int j = 1; j <= Nt; j++){
bool flag = true;
for (int k = 1; k <= Np; k++){
if (place[k] + amount[j][k] < 0){
flag = false;
break;
}
}
if (flag){
round++;
for (int k = 1; k <= Np; k++) place[k] += amount[j][k];
if (round >= Nf) break;
}
}
if (round >= Nf) break;
}
cout << "Case " << Case << ": ";
if (round >= Nf) cout << "still live after "<<Nf<<" transitions" << endl;
else cout << "dead after " << round << " transitions" << endl;
cout << "Places with tokens:";
for (int i = 1; i <= Np; i++){
if (place[i] > 0){
cout << " " << i << " (" << place[i] << ")";
}
}
cout << endl << endl;
}
system("pause");
return 0;
}