Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- void base(int A[9][9])
- {
- int B[9][9]={
- {1, 3, 9, 7, 6, 8, 5, 2, 4},
- {8, 4, 5, 1, 3, 2, 9, 7, 6},
- {2, 6, 7, 9, 4, 5, 8, 3, 1},
- {5, 1, 3, 2, 7, 6, 4, 8, 9},
- {6, 7, 2, 8, 9, 4, 1, 5, 3},
- {4, 9, 8, 5, 1, 3, 2, 6, 7},
- {7, 2, 6, 4, 5, 1, 3, 9, 8},
- {9, 5, 1, 3, 8, 7, 6, 4, 2},
- {3, 8, 4, 6, 2, 9, 7, 1, 5}};
- for(int i=0; i<9; i++)
- for(int j=0; j<9; j++)
- A[i][j]=B[i][j];
- }
- void output(int A[9][9])
- {
- cout<<endl;
- for(int i=0; i<9; i++)
- {
- for(int j=0; j<9;j++)
- {
- cout<<" "<<A[i][j];
- if((j+1)%3==0 && j%8!=0)
- cout<<" |";
- if((j+1)%9==0)
- cout<<endl;
- }
- if((i+1)%3==0 && i%8!=0)
- cout<<"-------+-------+-------\n";
- }
- cout<<endl;
- }
- void transposing(int A[9][9])
- {
- for(int i=0; i<9; i++)
- for(int j=i; j<9; j++)
- swap(A[i][j], A[j][i]);
- }
- void swap_rows_small(int A[9][9], int r, int n)
- {
- for(int i=0; i<9; i++)
- swap(A[r%3+(n%3)*3][i], A[(r+1)%3+(n%3)*3][i]);
- }
- void swap_colums_small(int A[9][9], int r, int n)
- {
- for(int i=0; i<9; i++)
- swap(A[i][r%3+(n%3)*3], A[i][(r+1)%3+(n%3)*3]);
- }
- void swap_rows_area(int A[9][9], int r)
- {
- for(int i=0; i<9; i++)
- for(int j=0; j<3; j++)
- swap(A[r%3*3+j][i], A[(r+1)%3*3+j][i]);
- }
- void swap_colums_area(int A[9][9], int r)
- {
- for(int i=0; i<9; i++)
- for(int j=0; j<3; j++)
- swap(A[i][r%3*3+j], A[i][(r+1)%3*3+j]);
- }
- void generate_sudoku(int A[9][9])
- {
- cout<<"\nÂâåäèòå êîëè÷åñòâî èòåðàöèé ïåðåìåøèâàíèÿ: ";
- string k_str;
- cin >> k_str;
- int k = atoi(k_str.c_str());
- for(int i=0; i<k; i++)
- {
- unsigned r = rand() % 5;
- switch(r){
- case 0: transposing(A); break;
- case 1: swap_rows_small(A, i, i); break;
- case 2: swap_colums_small(A, i, i); break;
- case 3: swap_rows_area(A, i); break;
- case 4: swap_colums_area(A, i); break;
- }
- }
- output(A);
- }
- bool ask_to_continue()
- {
- string in_line;
- while (true) {
- cout << "\nÏîâòîðèòü? ('Y'-Äà; 'N'-Íåò)\n>> ";
- cin >> in_line;
- if (in_line == "Y") {
- return true;
- }
- if (in_line == "N") {
- return false;
- }
- }
- }
- int main()
- {
- int A[9][9];
- base(A);
- bool want_to_continue = true;
- while (want_to_continue) {
- system("clear");
- generate_sudoku(A);
- want_to_continue = ask_to_continue();
- }
- system("clear");
- cout<<"\nÇàâåðøåíèå ðàáîòû...\n\n";
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement