Advertisement
Nevarkir

5E

Dec 7th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.96 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. void enteringCoordinatesIntoAnArray(ifstream*, int**, int*, int*);
  8. void outputOnDisplay(int**, int*, int*);
  9. void matrixReset(int**, int*, int*);
  10. void enteringCoordinatesIntoAnMatrix(int**, int**, int*, int*);
  11. void maximumSquareSearchAlgorithm(int**, int*, int*, int*, int*, int, int);
  12.  
  13. int main()
  14. {
  15.     setlocale(LC_ALL, "Russian");
  16.     ifstream in("D:\\Users\\Nevarkir\\source\\repos\\Project1\\Project1\\5e\\input.txt");
  17.     int N; // количество отмеченных точек внутри прямоугольника
  18.     in >> N;
  19.     int W; // ширина прямоугольника
  20.     in >> W;
  21.     W++;
  22.     int H; // высота прямоугольника
  23.     in >> H;
  24.     H++;
  25.     int** M; // массив отмеченных точек внутри прямоугольника
  26.     int G = 2;
  27.     M = new int* [N];
  28.     for (int i = 0; i < N; i++)
  29.     {
  30.         M[i] = new int[G];
  31.     }
  32.  
  33.     enteringCoordinatesIntoAnArray(&in, M, &N, &G); // заполнение двумерного динамического массива
  34.     // outputOnDisplay(M, &N, &G); // вывод значений двумерного массива в экран
  35.     in.close();
  36.  
  37.     int** V; // матрица
  38.     // инициализация двумерной динамической матрицы
  39.     V = new int* [H];
  40.     for (int y = 0; y < H; y++)
  41.     {
  42.         V[y] = new int[W];
  43.     }
  44.    
  45.     matrixReset(V, &H, &W);
  46.     //outputOnDisplay(V, &H, &W);
  47.  
  48.     enteringCoordinatesIntoAnMatrix(M, V, &N, &H);
  49.     //outputOnDisplay(V, &H, &W);
  50.  
  51.     int count = 0;
  52.     int max = 0;
  53.     int di = 0;
  54.     int dj = 0;
  55.     int num = 1;
  56.     while (count != ((W * H) - N))
  57.     {
  58.         int F = 1;
  59.         // cout << "============================================================" << endl;
  60.         // cout << "Start - y = " << di << " и x = " << dj << ": длина стороны квадрата = " << F << "; максимальная длина стороные квадрата = " << max << "; отмечено точек = " << count << endl;
  61.         maximumSquareSearchAlgorithm(V, &H, &W, &F, &count, di, dj);
  62.         if (F > max) max = F;
  63.         for (int y = 0; y < H; y++) // подбор следующего индекса с непосещенной вершиной
  64.         {
  65.             for (int x = 0; x < W; x++)
  66.             {
  67.                 if (V[y][x] == 0)
  68.                 {
  69.                     di = y;
  70.                     dj = x;
  71.                 }
  72.             }
  73.         }
  74.         // cout << "End - следующие y = " << di << " и x = " << dj << "; конечные итоги: длина стороны квадрата = " << F << "; максимальная длина стороные квадрата = " << max << "; отмечено точек = "<< count << endl;
  75.     }
  76.     ofstream out;
  77.     out.open("D:\\Users\\Nevarkir\\source\\repos\\Project1\\Project1\\5e\\output.txt");
  78.     //cout << "============================================================" << endl;
  79.     out << max << endl;
  80.     //cout << "============================================================" << endl;
  81.     // outputOnDisplay(V, &H, &W);
  82.     out.close();
  83.     system("PAUSE");
  84.     for (int i = 0; i < N; i++) {
  85.         delete[] M[i];
  86.     }
  87.     delete[] M;
  88.     for (int y = 0; y < H; y++) {
  89.         delete[] V[y];
  90.     }
  91.     delete[] V;
  92.     system("PAUSE");
  93.     return 0;
  94. }
  95.  
  96. void enteringCoordinatesIntoAnArray(ifstream* in, int** M, int* N, int* G)
  97. {
  98.     for (int i = 0; i < (*N); i++)
  99.     {
  100.         for (int j = 0; j < (*G); j++)
  101.         {
  102.             (*in) >> M[i][j];
  103.         }
  104.     }
  105. }
  106.  
  107. void outputOnDisplay(int** M, int* N, int*G)
  108. {
  109.     for (int i = 0; i < (*N); i++)
  110.     {
  111.         for (int j = 0; j < (*G); j++)
  112.         {
  113.             cout << M[i][j] << "  ";
  114.         }
  115.         cout << endl;
  116.     }
  117.     cout << "\n" << endl;
  118. }
  119.  
  120. void matrixReset(int** V, int* H, int* W)
  121. {
  122.     for (int y = 0; y < (*H); y++)
  123.     {
  124.         for (int x = 0; x < (*W); x++)
  125.         {
  126.             V[y][x] = 0;
  127.         }
  128.     }
  129. }
  130.  
  131. void enteringCoordinatesIntoAnMatrix(int** M, int** V, int* N, int* H)
  132. {
  133.     for (int i = 0; i < (*N); i++)
  134.     {
  135.         V[(*H) - 1 - M[i][1]][M[i][0]] = 1;
  136.     }
  137. }
  138.  
  139. void maximumSquareSearchAlgorithm(int** V, int* H, int* W, int* F, int* count, int i, int j)
  140. {
  141.     // cout << (*F) << endl;
  142.     if (V[i][j] == 0)
  143.     {
  144.         if (((i + (*F) - 1) < (*H)) && ((j + (*F) - 1) < (*W)))
  145.         {
  146.             int k = 0;
  147.             for (int ki = i; ((ki < (i + (*F))) && (ki < (*H))); ki++)
  148.             {
  149.                 for (int kj = j; ((kj < (j + (*F))) && (kj < (*W))); kj++)
  150.                 {
  151.                     if (V[ki][kj] != 1)
  152.                     {
  153.                         k++;
  154.                     }
  155.                     // cout << "В точке [y = " << ki << " и x = " << kj << "] со значением " << V[ki][kj] << ", значение k = " << k << ", со значением F = " << (*F) << endl;
  156.                 }
  157.             }
  158.             if (k == (*F) * (*F))
  159.             {
  160.                 // cout << "Рекурсия!" << endl;
  161.                 (*F) = (*F) + 1;
  162.                 maximumSquareSearchAlgorithm(V, H, W, F, count, i, j);
  163.             }
  164.             else
  165.             {
  166.                 for (int ki = i; ki < (i + (*F) - 1); ki++)
  167.                 {
  168.                     for (int kj = j; kj < (j + (*F) - 1); kj++)
  169.                     {
  170.                         if (V[ki][kj] == 0)
  171.                         {
  172.                             (*count)++;
  173.                             V[ki][kj] = 2;
  174.                             (*F) = (*F) - 1;
  175.                         }
  176.                     }
  177.                 }
  178.             }
  179.         }
  180.         else
  181.         {
  182.             V[i][j] = 2;
  183.             (*count)++;
  184.             (*F) = (*F) - 1;
  185.         }
  186.     }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement