Advertisement
EeZotop

Untitled

Nov 26th, 2021
1,272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. void base(int A[9][9])
  8. {
  9.     int B[9][9]={
  10.     {1, 3, 9, 7, 6, 8, 5, 2, 4},
  11.     {8, 4, 5, 1, 3, 2, 9, 7, 6},
  12.     {2, 6, 7, 9, 4, 5, 8, 3, 1},
  13.     {5, 1, 3, 2, 7, 6, 4, 8, 9},
  14.     {6, 7, 2, 8, 9, 4, 1, 5, 3},
  15.     {4, 9, 8, 5, 1, 3, 2, 6, 7},
  16.     {7, 2, 6, 4, 5, 1, 3, 9, 8},
  17.     {9, 5, 1, 3, 8, 7, 6, 4, 2},
  18.     {3, 8, 4, 6, 2, 9, 7, 1, 5}};
  19.  
  20.     for(int i=0; i<9; i++)
  21.         for(int j=0; j<9; j++)
  22.             A[i][j]=B[i][j];
  23. }
  24.  
  25. void output(int A[9][9])
  26. {
  27.     cout<<endl;
  28.     for(int i=0; i<9; i++)
  29.     {
  30.         for(int j=0; j<9;j++)
  31.         {
  32.             cout<<" "<<A[i][j];
  33.             if((j+1)%3==0 && j%8!=0)
  34.                 cout<<" |";
  35.             if((j+1)%9==0)
  36.                 cout<<endl;
  37.         }
  38.         if((i+1)%3==0 && i%8!=0)
  39.             cout<<"-------+-------+-------\n";
  40.     }
  41.     cout<<endl;
  42. }
  43.  
  44. void transposing(int A[9][9])
  45. {
  46.     for(int i=0; i<9; i++)
  47.         for(int j=i; j<9; j++)
  48.             swap(A[i][j], A[j][i]);
  49. }
  50.  
  51. void swap_rows_small(int A[9][9], int r, int n)
  52. {
  53.     for(int i=0; i<9; i++)
  54.         swap(A[r%3+(n%3)*3][i], A[(r+1)%3+(n%3)*3][i]);
  55. }
  56.  
  57. void swap_colums_small(int A[9][9], int r, int n)
  58. {
  59.     for(int i=0; i<9; i++)
  60.         swap(A[i][r%3+(n%3)*3], A[i][(r+1)%3+(n%3)*3]);
  61. }
  62.  
  63. void swap_rows_area(int A[9][9], int r)
  64. {
  65.     for(int i=0; i<9; i++)
  66.         for(int j=0; j<3; j++)
  67.             swap(A[r%3*3+j][i], A[(r+1)%3*3+j][i]);
  68. }
  69.  
  70. void swap_colums_area(int A[9][9], int r)
  71. {
  72.     for(int i=0; i<9; i++)
  73.         for(int j=0; j<3; j++)
  74.             swap(A[i][r%3*3+j], A[i][(r+1)%3*3+j]);
  75. }
  76.  
  77. void generate_sudoku(int A[9][9])
  78. {
  79.     cout<<"\nÂâåäèòå êîëè÷åñòâî èòåðàöèé ïåðåìåøèâàíèÿ: ";
  80.  
  81.     string k_str;
  82.     cin >> k_str;
  83.     int k = atoi(k_str.c_str());
  84.  
  85.     for(int i=0; i<k; i++)
  86.     {
  87.         unsigned r = rand() % 5;
  88.         switch(r){
  89.             case 0: transposing(A); break;
  90.             case 1: swap_rows_small(A, i, i); break;
  91.             case 2: swap_colums_small(A, i, i); break;
  92.             case 3: swap_rows_area(A, i); break;
  93.             case 4: swap_colums_area(A, i); break;
  94.         }
  95.     }
  96.     output(A);
  97. }
  98.  
  99. bool ask_to_continue()
  100. {
  101.     string in_line;
  102.     while (true) {
  103.         cout << "\nÏîâòîðèòü? ('Y'-Äà; 'N'-Íåò)\n>> ";
  104.  
  105.         cin >> in_line;
  106.         if (in_line == "Y") {
  107.             return true;
  108.         }
  109.         if (in_line == "N") {
  110.             return false;
  111.         }
  112.     }
  113. }
  114.  
  115. int main()
  116. {
  117.     int A[9][9];
  118.     base(A);
  119.     bool want_to_continue = true;
  120.     while (want_to_continue) {
  121.         system("clear");
  122.         generate_sudoku(A);
  123.         want_to_continue = ask_to_continue();
  124.     }
  125.     system("clear");
  126.     cout<<"\nÇàâåðøåíèå ðàáîòû...\n\n";
  127.     system("pause");
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement