From cd2aee9f1c15956775943649da5a19b0a98b6d38 Mon Sep 17 00:00:00 2001 From: Abhishek Parihar Date: Wed, 14 Oct 2020 14:12:23 +0530 Subject: [PATCH] This is Sudoku Solver Code. Please Accept it... --- C++/Sudoku Solver Code | 96 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 C++/Sudoku Solver Code diff --git a/C++/Sudoku Solver Code b/C++/Sudoku Solver Code new file mode 100644 index 0000000..d427348 --- /dev/null +++ b/C++/Sudoku Solver Code @@ -0,0 +1,96 @@ +#include +using namespace std; +#define fastio ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL) +#define t_times int t; cin >> t; while(t--) +#define all(c) (c).begin(), (c).end() +#define sci(n) scanf("%d", &n) +#define scl(n) scanf("%lld", &n) +#define prin(n) printf("%d\n", n) +#define prln(n) printf("%lld\n", n) +#define mk make_pair +#define F first +#define S second +#define pb push_back +typedef long long int LL; typedef unsigned long long ULL; typedef unsigned int uint; +typedef long double LD; typedef pair< int, int> ii; typedef vector< int> vi; +const int mod = 1e9 + 7; const LL INF = 9e18 + 2e17; const int inf = 2e9; +const int N = 1e3 + 22; const double eps = 1e-10; const double PI = 3.14159265; + +void print(vector>& b) +{ + for(int i=0;i<9;i++) + { + for(int j=0;j<9;j++){ + cout<>& b,int row,int col,char c) + { + for(int i=0;i<9;i++){ + if(b[i][col]==c){ + return false; + } + } + for(int j=0;j<9;j++){ + if(b[row][j]==c) return false; + } + int r1,r2,c1,c2; + r1=row/3; r1*=3; + r2=r1+2; + c1=col/3; c1*=3; + c2=c1+2; + for(int i=r1;i<=r2;i++){ + for(int j=c1;j<=c2;j++){ + if(b[i][j]==c) + return false; + } + } + return true; + } + bool solve(vector>& b) + { + for(int i=0;i<9;i++) + { + for(int j=0;j<9;j++) + { + if(b[i][j]=='.') + { + for(char c='1';c<='9';c++) + { + if(!isSafe(b,i,j,c)) + continue; + b[i][j]=c; + if(solve(b)) + return true; + b[i][j]='.'; + } + return false; + } + } + } + return true; + } + +void solve() +{ + vector > b(9,vector(9)); + for(int i=0;i<9;i++){ + for(int j=0;j<9;j++){ + cin>>b[i][j]; + } + } + solve(b); + print(b); + + return; +} + +int main() +{ + fastio; + //t_times + solve(); + return 0; +}