银行家算法-安全序列全列出实现

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<string>
#include<queue>
#include<climits>
#include<map>
#include<stack>
#include<list>
#include<set>
#include<ctime>
#include<cstdlib>
#include<sstream>
#define file_in freopen("input.txt","r",stdin)
#define MAX 100005
#define INF 0x3f3f3f3f
#define HASH 100019
#define MAX_C 100
#define MAP_IT(type1,type2) map<type1,type2>::iterator
using namespace std;
#define ll long long
#define FF(x,y,i) for(int i=x;i<y;i++)
struct process {
     int num;
     vector<int>max;
     vector<int>occupied;
     vector<int>need;
};
vector<int>ava;
vector<process>processList;
bool test(vector<int>pmu) {
     vector<process>tempPro = processList;
     vector<int>tempava = ava;
     for (int i = 0; i < pmu.size(); i++) {
         int currentPro = pmu[i];
         for (int i = 0; i < tempPro[currentPro].need.size(); i++) {
             if (tempPro[currentPro].need[i] > tempava[i])
                 return 0;
         }
         for (int i = 0; i < tempPro[currentPro].occupied.size(); i++) {
             tempava[i] += tempPro[currentPro].occupied[i];
         }
     }
     return true;
}
int main() {
     int numProcess, numRes;
     cout << "Please input number of processes" << endl;
     cin >> numProcess;
     processList.resize(numProcess);
     cout << "please input the number of resource types" << endl;
     cin >> numRes;
     for (int i = 0; i < numProcess; i++)
     {
         processList[i].max.resize(numRes);
         processList[i].occupied.resize(numRes);
         processList[i].need.resize(numRes);
     }
     ava.resize(numRes);
     cout << "for each kind of resource, please input it's number of remaining instances" << endl;
     for (int i = 0; i < numRes; i++)
         cin >> ava[i];
     cout << "for each process, input the occuied instance it owns by the order" << endl;
     for (int i = 0; i < numProcess; i++) {
         cout << "process" << i <<":"<< endl;
         for (int j = 0; j < numRes; j++) {
             cin >> processList[i].occupied[j];
            
         }
     }
     cout << "for each process, input the maxium instance it needs" << endl;
     for (int i = 0; i < numProcess; i++) {
         cout << "process" << i << ":" << endl;
         for (int j = 0; j < numRes; j++) {
             cin >> processList[i].max[j];
             processList[i].need[j] = processList[i].max[j] - processList[i].occupied[j];
         }
     }
     vector<int>possiblePermu;
     for (int i = 0; i < processList.size(); i++)possiblePermu.push_back(i);
     while (next_permutation(possiblePermu.begin(), possiblePermu.end())) {
         if (test(possiblePermu)) {
             for (int i = 0; i < possiblePermu.size(); i++)
                 cout << possiblePermu[i] << " ";
             cout << endl;
         }
    
     }

   


}

转载于:https://www.cnblogs.com/stultus/p/8024972.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值