Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <random>
- #include <algorithm>
- // Constants
- const int TIME_STEPS = 1000;
- const int SWAP_INTERVAL = 10;
- const int HEALTHY = 1;
- const int VACCINATED = 2;
- const int MILDLY_SICK = 3;
- const int HEAVILY_SICK = 4;
- // Function to simulate contagion
- void simulateContagion(std::vector<std::vector<int>>& seatingPlan, std::default_random_engine& generator) {
- std::uniform_int_distribution<int> distribution(0, seatingPlan.size() - 1);
- for (int i = 0; i < TIME_STEPS; i++) {
- int row = distribution(generator);
- int col = distribution(generator);
- if (seatingPlan[row][col] == MILDLY_SICK || seatingPlan[row][col] == HEAVILY_SICK) {
- // Propose contagion for each of the 4 nearest neighbors
- for (int j = std::max(0, row - 1); j <= std::min((int)seatingPlan.size() - 1, row + 1); j++) {
- for (int k = std::max(0, col - 1); k <= std::min((int)seatingPlan[j].size() - 1, col + 1); k++) {
- if (j != row || k != col) {
- if (seatingPlan[j][k] == HEALTHY) {
- seatingPlan[j][k] = seatingPlan[row][col] == MILDLY_SICK ? MILDLY_SICK : HEAVILY_SICK;
- }
- }
- }
- }
- }
- }
- }
- // Function to simulate swapping
- void simulateSwapping(std::vector<std::vector<int>>& seatingPlan, std::default_random_engine& generator) {
- std::uniform_int_distribution<int> distribution(0, seatingPlan.size() - 1);
- for (int i = 0; i < TIME_STEPS; i++) {
- if (i % SWAP_INTERVAL == 0) {
- int row1 = distribution(generator);
- int col1 = distribution(generator);
- int row2 = distribution(generator);
- int col2 = distribution(generator);
- if (seatingPlan[row1][col1] != HEALTHY && seatingPlan[row2][col2] == HEALTHY) {
- std::swap(seatingPlan[row1][col1], seatingPlan[row2][col2]);
- }
- }
- }
- }
- // Function to calculate statistics
- void calculateStatistics(const std::vector<std::vector<int>>& seatingPlan, std::ofstream& file) {
- int totalSeats = seatingPlan.size() * seatingPlan[0].size();
- int emptySeats = 0;
- int vaccinated = 0;
- int mildlySick = 0;
- int heavilySick = 0;
- for (const auto& row : seatingPlan) {
- for (int seat : row) {
- switch (seat) {
- case HEALTHY:
- emptySeats++;
- break;
- case VACCINATED:
- vaccinated++;
- break;
- case MILDLY_SICK:
- mildlySick++;
- break;
- case HEAVILY_SICK:
- heavilySick++;
- break;
- }
- }
- }
- file << "Empty/Healthy seats: " << (double)emptySeats / totalSeats * 100 << "%\n";
- file << "Vaccinated people: " << (double)vaccinated / totalSeats * 100 << "%\n";
- file << "Mildly sick people: " << (double)mildlySick / totalSeats * 100 << "%\n";
- file << "Heavily sick people: " << (double)heavilySick / totalSeats * 100 << "%\n";
- }
- int main() {
- std::ifstream file("in_theatre.dat");
- std::ofstream outFile("out_theatre.dat");
- std::vector<std::vector<int>> seatingPlan;
- // Read seating plan from file
- std::string line;
- while (std::getline(file, line)) {
- std::vector<int> row;
- for (char c : line) {
- row.push_back(c - '0');
- }
- seatingPlan.push_back(row);
- }
- // Initialize random number generator
- std::default_random_engine generator;
- // Simulate contagion and swapping
- simulateContagion(seatingPlan, generator);
- simulateSwapping(seatingPlan, generator);
- // Calculate and output statistics
- calculateStatistics(seatingPlan, outFile);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement