Advertisement
coloriot

HA_42_2

Feb 16th, 2025
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. using namespace std;
  5.  
  6. char colToLetter(int col) {
  7.     if(col == 0) return 'A';
  8.     if(col == 1) return 'B';
  9.     if(col == 2) return 'C';
  10.     if(col == 4) return 'D';
  11.     if(col == 5) return 'E';
  12.     if(col == 6) return 'F';
  13.     return '?';
  14. }
  15.  
  16. class AirplaneSeating {
  17. public:
  18.     AirplaneSeating(int n, const vector<string> &rows)
  19.         : n(n), configuration(rows) { }
  20.  
  21.     void processGroup(int groupSize, const string &side, const string &position) {
  22.         vector<int> sideIndices;
  23.         if(side == "left")
  24.             sideIndices = {0, 1, 2};
  25.         else // "right"
  26.             sideIndices = {4, 5, 6};
  27.  
  28.         int demandedIndex = -1;
  29.         if(side == "left"){
  30.             if(position == "window")
  31.                 demandedIndex = 0; // A
  32.             else if(position == "aisle")
  33.                 demandedIndex = 2; // C
  34.         } else {
  35.             if(position == "aisle")
  36.                 demandedIndex = 4; // D
  37.             else if(position == "window")
  38.                 demandedIndex = 6; // F
  39.         }
  40.  
  41.         bool found = false;
  42.         int foundRow = -1, foundStart = -1;
  43.         for (int i = 0; i < n && !found; i++) {
  44.             int possibleBlocks = 3 - groupSize + 1;
  45.             for (int start = 0; start < possibleBlocks; start++) {
  46.                 bool blockFree = true;
  47.                 bool demandSatisfied = false;
  48.                 for (int j = 0; j < groupSize; j++) {
  49.                     int col = sideIndices[start + j];
  50.                     if (configuration[i][col] != '.'){
  51.                         blockFree = false;
  52.                         break;
  53.                     }
  54.                     if(col == demandedIndex)
  55.                         demandSatisfied = true;
  56.                 }
  57.                 if(blockFree && demandSatisfied) {
  58.                     found = true;
  59.                     foundRow = i;
  60.                     foundStart = start;
  61.                     break;
  62.                 }
  63.             }
  64.         }
  65.  
  66.         if (!found) {
  67.             cout << "Cannot fulfill passengers requirements" << "\n";
  68.             return;
  69.         }
  70.    
  71.         vector<int> chosenCols;
  72.         for (int j = 0; j < groupSize; j++){
  73.             chosenCols.push_back(sideIndices[foundStart+j]);
  74.         }
  75.  
  76.         vector<string> seatLabels;
  77.         for(auto col : chosenCols){
  78.             string lab = to_string(foundRow+1);
  79.             lab.push_back(colToLetter(col));
  80.             seatLabels.push_back(lab);
  81.         }
  82.  
  83.         cout << "Passengers can take seats:";
  84.         for(auto &lab : seatLabels){
  85.             cout << " " << lab;
  86.         }
  87.         cout << "\n";
  88.  
  89.         for(auto col : chosenCols){
  90.             configuration[foundRow][col] = '#';
  91.         }
  92.  
  93.         for (int i = 0; i < n; i++) {
  94.             string outLine;
  95.             for (int j = 0; j < 7; j++) {
  96.                 if (i == foundRow) {
  97.                     bool isCurrent = false;
  98.                     for(auto col : chosenCols){
  99.                         if(j == col){
  100.                             isCurrent = true;
  101.                             break;
  102.                         }
  103.                     }
  104.                     if(isCurrent){
  105.                         outLine.push_back('X');
  106.                         continue;
  107.                     }
  108.                 }
  109.                 outLine.push_back(configuration[i][j]);
  110.             }
  111.             cout << outLine << "\n";
  112.         }
  113.     }
  114.  
  115. private:
  116.     int n;
  117.     vector<string> configuration;
  118. };
  119.  
  120. int main(){
  121.     ios::sync_with_stdio(false);
  122.     cin.tie(nullptr);
  123.  
  124.     int n;
  125.     cin >> n;
  126.     vector<string> rows(n);
  127.     for (int i = 0; i < n; i++){
  128.         cin >> rows[i];
  129.     }
  130.  
  131.     AirplaneSeating airplane(n, rows);
  132.  
  133.     int m;
  134.     cin >> m;
  135.     for (int i = 0; i < m; i++){
  136.         int groupSize;
  137.         string side, position;
  138.         cin >> groupSize >> side >> position;
  139.         airplane.processGroup(groupSize, side, position);
  140.     }
  141.  
  142.     return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement