```c #include <stdio.h> #include <stdbool.h> #include <string.h> bool isSafeState(int available[], int max[][3], int allocated[][3], int n) { int need[n][3]; bool finish[n]; int work[3]; // Initialize need matrix for(int i=0; i<n; i++) { for(int j=0; j<3; j++) { need[i][j] = max[i][j] - allocated[i][j]; } finish[i] = false; } // Initialize work vector for(int i=0; i<3; i++) { work[i] = available[i]; } int safeSeq[n]; int count = 0; while (count < n) { bool found = false; for (int i = 0; i < n; i++) { if (finish[i] == false) { int j; for (j = 0; j < 3; j++) { if (need[i][j] > work[j]) { break; } } if (j == 3) { for (int k = 0; k < 3; k++) { work[k] += allocated[i][k]; } safeSeq[count++] = i; finish[i] = true; found = true; } } } if (found == false) { return false; } } printf("Safe Sequence is: "); for (int i = 0; i < n-1; i++) { printf("%d, ", safeSeq[i]); } printf("%d\n", safeSeq[n-1]); return true; } int main() { int resource[3]; int n = 4; // Number of processes // Input resource total availability scanf("%d %d %d", &resource[0], &resource[1], &resource[2]); int max[n][3], allocated[n][3]; char processName[n][10]; // Input max and allocated resources for each process for(int i=0; i<n; i++) { scanf("%s %d %d %d %d %d %d", processName[i], &max[i][0], &max[i][1], &max[i][2], &allocated[i][0], &allocated[i][1], &allocated[i][2]); } if(isSafeState(resource, max, allocated, n)) { printf("true\n"); } else { printf("false\n"); } return 0; } ``` [2024-11-14 16:44:57 | AI写代码神器 | 1390点数解答]