UVA-804 模拟
生活随笔
收集整理的這篇文章主要介紹了
UVA-804 模拟
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
???? 將每個translation的輸入和輸出place全部記錄下來,模擬即可,當所有translation都不能工作時,就說明dead了。
AC代碼:
#include<cstdio> #include<vector> using namespace std;const int maxn = 100 + 5;struct node{vector<int> in, out; }tran[maxn];int p[maxn]; //the number of tokens in all placesint main(){int pn, tn, nf, kase = 1;while(scanf("%d", &pn) == 1 && pn){for(int i = 1; i <= pn; ++i){scanf("%d", &p[i]);}scanf("%d", &tn);for(int i = 1; i <= tn; ++i){int x;while(scanf("%d", &x) == 1 && x){if(x < 0) tran[i].in.push_back(-x);else tran[i].out.push_back(x);}}scanf("%d",&nf);bool dead = 0;int h;for(h = 0; h < nf; ++h){int cnt = 0;for(int i = 1; i <= tn; ++i){bool flag = 1;vector<int> &in = tran[i].in, &out = tran[i].out;for(int j = 0; j < in.size(); ++j){if(p[in[j]] == 0) {flag = 0;while(j) p[in[--j]]++; //break;}else p[in[j]]--;}if(!flag) ++cnt;else {for(int k = 0; k < out.size(); ++k) p[out[k]]++;break;}}if(cnt == tn) {dead = 1;break;}}if(dead) printf("Case %d: dead after %d transitions\n", kase++, h);else printf("Case %d: still live after %d transitions\n", kase++, nf);printf("Places with tokens:");for(int i = 1; i <= pn ; ++i){if(p[i]) printf(" %d (%d)", i, p[i]);}printf("\n\n");for(int i = 1; i <= tn ; ++i) {tran[i].in.clear();tran[i].out.clear();}}return 0; }如有不當之處歡迎指出!
轉載于:https://www.cnblogs.com/flyawayl/p/8305466.html
總結
以上是生活随笔為你收集整理的UVA-804 模拟的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java service wrapper
- 下一篇: (五)Oracle函数 序列 约束 索引