Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //http://www.youtube.com/user/thecplusplusguy
- #include <iostream>
- #include <fstream>
- #include <cmath>
- using namespace std;
- bool sor(int s,int o,int n);
- bool oszlop(int s,int o,int n);
- bool negyzet(int s,int o,int n);
- int a[9][9];
- int main()
- {
- int un=0,s,o,tmp;
- bool b=1;
- char c[20];
- cout << "write it manually (k) or read from file (f)? ";
- cin >> c[0];
- if(c[0]=='f')
- {
- cout << "give the sudoku filename: ";
- cin >> c;
- cout << endl;
- ifstream in(c);
- for(int i=0;i<9;i++)
- {
- for(int j=0;j<9;j++)
- {
- in >> a[i][j];
- if(!a[i][j])
- un++;
- }
- }
- in.close();
- }else if(c[0]=='k')
- {
- for(int i=0;i<9;i++)
- for(int j=0;j<9;j++)
- {
- do{
- cout << "Please enter " << i+1 << ". row " << j+1 << ". number: ";
- cin >> a[i][j];
- }while(a[i][j]<=0 && a[i][j]>10);
- if(a[i][j]==0)
- un++;
- }
- }else
- {
- cout << "exiting..." << endl;
- return 0;
- }
- while(un && b)
- {
- b=0;
- for(int i=0;i<9;i++)
- {
- for(int j=0;j<9;j++)
- {
- if(a[i][j]!=0)
- continue;
- tmp=0;
- for(int x=1;x<10;x++)
- {
- if(sor(i,j,x) && oszlop(i,j,x) && negyzet(i,j,x))
- {
- if(tmp==0)
- tmp=x;
- else{
- tmp=0;
- break;
- }
- }
- }
- if(tmp!=0)
- {
- a[i][j]=tmp;
- b=1;
- un--;
- }
- }
- }
- }
- if(!b)
- cout << "It cannot be solved" << endl;
- else if(!un)
- cout << "solved" << endl;
- cout << endl;
- for(int i=0;i<9;i++)
- {
- for(int j=0;j<9;j++)
- {
- cout << a[i][j] << " ";
- }
- cout << endl;
- }
- cout << endl;
- }
- bool sor(int s,int o,int n)
- {
- for(int g=0;g<9;g++)
- {
- if(a[s][g]==n)
- {
- return 0;
- }
- }
- return 1;
- }
- bool oszlop(int s,int o,int n)
- {
- for(int g=0;g<9;g++)
- {
- if(a[g][o]==n)
- return 0;
- }
- return 1;
- }
- bool negyzet(int s,int o,int n)
- {
- int sor=ceil((s+1)/3.);
- int oszlop=ceil((o+1)/3.);
- for(int g=(sor-1)*3;g<((sor-1)*3+3);g++)
- for(int h=(oszlop-1)*3;h<((oszlop-1)*3+3);h++)
- {
- if(a[g][h]==n)
- return 0;
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement