Sudoku Solver Helper Functions in C++

This code snippet includes definitions and utility functions for solving Sudoku puzzles using C++. It showcases useful macros and a function to check if a number can be placed in a given position of the Sudoku grid. The snippet also defines a sieve for prime numbers and other helpful utilities.
 avatar
swarup
c_cpp
a month ago
2.0 kB
1
Indexable
Never
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define test cout<<"ok"<<endl; 
#define check(n) for(auto x:n)cout<<x<<" "
#define MX 1e9+7
//  p.push_back(make_pair(a,b));
// pr.push_back({1,3});
//factor and divisor are same which is limited & multiple are unlimited
// lcm = (a /__gcd(a,b)) * b;
#define debug(x) cout << #x <<" " << x << endl;
#define Faster ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define MOD 1000000007
#define setpre(x,y) fixed<<setprecision(y)<<x
/*vector < bool > is_prime(MX+1, true);
is_prime[0] = is_prime[1] = false;

for(int i = 2; i*i<= MX; i++){
    if(is_prime[i] == true)
        {
            for(int j = i * i ; j <= MX ;j+= i)
            {       
                is_prime[j] = false;
            }
        }

}*/
bool CanWePlace(int number , vector < string >& sudoku, int ipos, int jpos){
    for(int j = 0; j < 4; j++){
        if(sudoku[ipos][j] == '0'+ number)
            return false;
    }
    for(int i = 0; i < 4; i++){
        if(sudoku[i][jpos] == '0'+ number)
            return false;
    }

    return true;
}
   

void solveThesudoku(vector < string > &sudoku, int ipos, int jpos){
    if(ipos == 5)
    {       // print the sudoku
        return;
    }
    ipos %= 4;
    if(j)
    jpos %= 4;

    if(ipos == n)
        return;
    if(sudoku[i][j] == '*'){
            for(int num = 1; num <= 4; num++)
                {
                    if(CanWePlace(num,sudoku,ipos,jpos)){
                        solveThesudoku(sudoku, ipos, jpos+1);
                        }
                }
            }
    else {

    }
        

}

void solve(){
    vector < string > sudoku(4);
    for(int i = 0; i < 4; i++)
        cin >> sudoku[i];

    

}

int main(){
Faster;
#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
#endif
    int t;
    cin >> t;
    while(t--)
    solve();
}
Leave a Comment