Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.18 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. string grid[3][3];
  6. int numbers[3][3];
  7. string S;
  8. int a;
  9.  
  10. void fillIn()
  11. {
  12.     for (int t{ 0 }; t < 2; t++)
  13.     {
  14.         // Fills rows
  15.  
  16.         for (int i{ 0 }; i < 3; i++)
  17.         {
  18.             int xcount = 0;
  19.             for (int j{ 0 }; j < 3; j++)
  20.             {
  21.                 if (grid[i][j] == "X")
  22.                 {
  23.                     a = j;
  24.                     xcount++;
  25.                 }
  26.             }
  27.             if (xcount == 1)
  28.             {
  29.                 if (a == 0)
  30.                 {
  31.                     numbers[i][a] = numbers[i][a + 1] - (numbers[i][a + 2] - numbers[i][a + 1]);
  32.                 }
  33.                 if (a == 1)
  34.                 {
  35.                     numbers[i][a] = numbers[i][a + 1] - ((numbers[i][a + 1] - numbers[i][a - 1]) / 2);
  36.                 }
  37.                 if (a == 2)
  38.                 {
  39.                     numbers[i][a] = numbers[i][a - 1] + (numbers[i][a - 1] - numbers[i][a - 2]);
  40.                 }
  41.                 grid[i][a] = "Done";
  42.             }
  43.         }
  44.  
  45.         // Fills columns
  46.  
  47.         for (int i{ 0 }; i < 3; i++)
  48.         {
  49.             int xcount = 0;
  50.             int a;
  51.             for (int j{ 0 }; j < 3; j++)
  52.             {
  53.                 if (grid[j][i] == "X")
  54.                 {
  55.                     a = j;
  56.                     xcount++;
  57.                 }
  58.             }
  59.             if (xcount == 1)
  60.             {
  61.                 if (a == 0)
  62.                 {
  63.                     numbers[a][i] = numbers[a + 1][i] - (numbers[a + 2][i] - numbers[a + 1][i]);
  64.                 }
  65.                 if (a == 1)
  66.                 {
  67.                     numbers[a][i] = numbers[a + 1][i] - ((numbers[a + 1][i] - numbers[a - 1][i]) / 2);
  68.                 }
  69.                 if (a == 2)
  70.                 {
  71.                     numbers[a][i] = numbers[a - 1][i] + (numbers[a - 1][i] - numbers[a - 2][i]);
  72.                 }
  73.                 grid[a][i] = "Done";
  74.             }
  75.         }
  76.     }
  77. }
  78.  
  79. void fillLine(int line)
  80. {
  81.     int xcount{ 0 };
  82.     int lone;
  83.  
  84.     for (int i{ 0 }; i < 3; i++)
  85.     {
  86.         if (grid[line][i] == "X") xcount++;
  87.         else lone = i;
  88.     }
  89.  
  90.     if (xcount == 2)
  91.     {
  92.         for (int i{ 0 }; i < 3; i++)
  93.         {
  94.             numbers[line][i] = numbers[1][lone];
  95.             grid[line][i] = "Done";
  96.         }
  97.     }
  98.     if (xcount == 3)
  99.     {
  100.         for (int i{ 0 }; i < 3; i++)
  101.         {
  102.             numbers[line][i] = 0;
  103.             grid[line][i] = "Done";
  104.         }
  105.     }
  106. }
  107.  
  108. int main()
  109. {
  110.     for (int i{ 0 }; i < 3; i++)
  111.     {
  112.         for (int j{ 0 }; j < 3; j++)
  113.         {
  114.             cin >> S;
  115.             grid[i][j] = S;
  116.             if (S != "X")
  117.             {
  118.                 numbers[i][j] = stoi(S, nullptr);
  119.             }
  120.         }
  121.     }
  122.  
  123.     fillIn();
  124.     fillLine(1);
  125.  
  126.     fillIn();
  127.     fillLine(0);
  128.  
  129.     fillIn();
  130.  
  131.     for (int i{ 0 }; i < 3; i++)
  132.     {
  133.         for (int j{ 0 }; j < 3; j++)
  134.         {
  135.             cout << numbers[i][j] << " ";
  136.         }
  137.         cout << "\n";
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement