Advertisement
teofarov13

Untitled

Dec 24th, 2023
728
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <random>
  5. #include <algorithm>
  6.  
  7. // Constants
  8. const int TIME_STEPS = 1000;
  9. const int SWAP_INTERVAL = 10;
  10. const int HEALTHY = 1;
  11. const int VACCINATED = 2;
  12. const int MILDLY_SICK = 3;
  13. const int HEAVILY_SICK = 4;
  14.  
  15. // Function to simulate contagion
  16. void simulateContagion(std::vector<std::vector<int>>& seatingPlan, std::default_random_engine& generator) {
  17.     std::uniform_int_distribution<int> distribution(0, seatingPlan.size() - 1);
  18.     for (int i = 0; i < TIME_STEPS; i++) {
  19.         int row = distribution(generator);
  20.         int col = distribution(generator);
  21.         if (seatingPlan[row][col] == MILDLY_SICK || seatingPlan[row][col] == HEAVILY_SICK) {
  22.             // Propose contagion for each of the 4 nearest neighbors
  23.             for (int j = std::max(0, row - 1); j <= std::min((int)seatingPlan.size() - 1, row + 1); j++) {
  24.                 for (int k = std::max(0, col - 1); k <= std::min((int)seatingPlan[j].size() - 1, col + 1); k++) {
  25.                     if (j != row || k != col) {
  26.                         if (seatingPlan[j][k] == HEALTHY) {
  27.                             seatingPlan[j][k] = seatingPlan[row][col] == MILDLY_SICK ? MILDLY_SICK : HEAVILY_SICK;
  28.                         }
  29.                     }
  30.                 }
  31.             }
  32.         }
  33.     }
  34. }
  35.  
  36. // Function to simulate swapping
  37. void simulateSwapping(std::vector<std::vector<int>>& seatingPlan, std::default_random_engine& generator) {
  38.     std::uniform_int_distribution<int> distribution(0, seatingPlan.size() - 1);
  39.     for (int i = 0; i < TIME_STEPS; i++) {
  40.         if (i % SWAP_INTERVAL == 0) {
  41.             int row1 = distribution(generator);
  42.             int col1 = distribution(generator);
  43.             int row2 = distribution(generator);
  44.             int col2 = distribution(generator);
  45.             if (seatingPlan[row1][col1] != HEALTHY && seatingPlan[row2][col2] == HEALTHY) {
  46.                 std::swap(seatingPlan[row1][col1], seatingPlan[row2][col2]);
  47.             }
  48.         }
  49.     }
  50. }
  51.  
  52. // Function to calculate statistics
  53. void calculateStatistics(const std::vector<std::vector<int>>& seatingPlan, std::ofstream& file) {
  54.     int totalSeats = seatingPlan.size() * seatingPlan[0].size();
  55.     int emptySeats = 0;
  56.     int vaccinated = 0;
  57.     int mildlySick = 0;
  58.     int heavilySick = 0;
  59.     for (const auto& row : seatingPlan) {
  60.         for (int seat : row) {
  61.             switch (seat) {
  62.                 case HEALTHY:
  63.                     emptySeats++;
  64.                     break;
  65.                 case VACCINATED:
  66.                     vaccinated++;
  67.                     break;
  68.                 case MILDLY_SICK:
  69.                     mildlySick++;
  70.                     break;
  71.                 case HEAVILY_SICK:
  72.                     heavilySick++;
  73.                     break;
  74.             }
  75.         }
  76.     }
  77.     file << "Empty/Healthy seats: " << (double)emptySeats / totalSeats * 100 << "%\n";
  78.     file << "Vaccinated people: " << (double)vaccinated / totalSeats * 100 << "%\n";
  79.     file << "Mildly sick people: " << (double)mildlySick / totalSeats * 100 << "%\n";
  80.     file << "Heavily sick people: " << (double)heavilySick / totalSeats * 100 << "%\n";
  81. }
  82.  
  83. int main() {
  84.     std::ifstream file("in_theatre.dat");
  85.     std::ofstream outFile("out_theatre.dat");
  86.     std::vector<std::vector<int>> seatingPlan;
  87.  
  88.     // Read seating plan from file
  89.     std::string line;
  90.     while (std::getline(file, line)) {
  91.         std::vector<int> row;
  92.         for (char c : line) {
  93.             row.push_back(c - '0');
  94.         }
  95.         seatingPlan.push_back(row);
  96.     }
  97.  
  98.     // Initialize random number generator
  99.     std::default_random_engine generator;
  100.  
  101.     // Simulate contagion and swapping
  102.     simulateContagion(seatingPlan, generator);
  103.     simulateSwapping(seatingPlan, generator);
  104.  
  105.     // Calculate and output statistics
  106.     calculateStatistics(seatingPlan, outFile);
  107.  
  108.     return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement