mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-05 03:29:46 +08:00
102
Backtracking/sudoku_solve.cpp
Normal file
102
Backtracking/sudoku_solve.cpp
Normal file
@@ -0,0 +1,102 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
///N=9;
|
||||
int n=9;
|
||||
|
||||
|
||||
bool isPossible(int mat[][9],int i,int j,int no){
|
||||
///Row or col nahin hona chahiye
|
||||
for(int x=0;x<n;x++){
|
||||
if(mat[x][j]==no||mat[i][x]==no){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// Subgrid mein nahi hona chahiye
|
||||
int sx = (i/3)*3;
|
||||
int sy = (j/3)*3;
|
||||
|
||||
for(int x=sx;x<sx+3;x++){
|
||||
for(int y=sy;y<sy+3;y++){
|
||||
if(mat[x][y]==no){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
void printMat(int mat[][9]){
|
||||
|
||||
for(int i=0;i<n;i++){
|
||||
for(int j=0;j<n;j++){
|
||||
cout<<mat[i][j]<<" ";
|
||||
if((j+1)%3==0){
|
||||
cout<<'\t';
|
||||
}
|
||||
|
||||
}
|
||||
if((i+1)%3==0){
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool solveSudoku(int mat[][9],int i,int j){
|
||||
///Base Case
|
||||
if(i==9){
|
||||
///Solve kr chuke hain for 9 rows already
|
||||
printMat(mat);
|
||||
return true;
|
||||
}
|
||||
|
||||
///Crossed the last Cell in the row
|
||||
if(j==9){
|
||||
return solveSudoku(mat,i+1,0);
|
||||
}
|
||||
|
||||
///Blue Cell - Skip
|
||||
if(mat[i][j]!=0){
|
||||
return solveSudoku(mat,i,j+1);
|
||||
}
|
||||
///White Cell
|
||||
///Try to place every possible no
|
||||
for(int no=1;no<=9;no++){
|
||||
if(isPossible(mat,i,j,no)){
|
||||
///Place the no - assuming solution aa jayega
|
||||
mat[i][j] = no;
|
||||
bool aageKiSolveHui = solveSudoku(mat,i,j+1);
|
||||
if(aageKiSolveHui){
|
||||
return true;
|
||||
}
|
||||
///Nahin solve hui
|
||||
///loop will place the next no.
|
||||
}
|
||||
}
|
||||
///Sare no try kr liey, kisi se bhi solve nahi hui
|
||||
mat[i][j] = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
int main(){
|
||||
|
||||
int mat[9][9] =
|
||||
{{5,3,0,0,7,0,0,0,0},
|
||||
{6,0,0,1,9,5,0,0,0},
|
||||
{0,9,8,0,0,0,0,6,0},
|
||||
{8,0,0,0,6,0,0,0,3},
|
||||
{4,0,0,8,0,3,0,0,1},
|
||||
{7,0,0,0,2,0,0,0,6},
|
||||
{0,6,0,0,0,0,2,8,0},
|
||||
{0,0,0,4,1,9,0,0,5},
|
||||
{0,0,0,0,8,0,0,7,9}};
|
||||
|
||||
printMat(mat);
|
||||
cout<<"Solution "<<endl;
|
||||
solveSudoku(mat,0,0);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
41
sleep sort.c
Normal file
41
sleep sort.c
Normal file
@@ -0,0 +1,41 @@
|
||||
#include <stdio.h>
|
||||
#include <windows.h>
|
||||
#include <process.h>
|
||||
|
||||
void routine(void *a)
|
||||
{
|
||||
int n = *(int *) a; // typecasting from void to int
|
||||
|
||||
// Sleeping time is proportional to the number
|
||||
// More precisely this thread sleep for 'n' milliseconds
|
||||
Sleep(n);
|
||||
|
||||
// After the sleep, print the number
|
||||
printf("%d ", n);
|
||||
|
||||
void sleepSort(int arr[], int n)
|
||||
{
|
||||
// An array of threads, one for each of the elements
|
||||
// in the input array
|
||||
HANDLE threads[n];
|
||||
|
||||
// Create the threads for each of the input array elements
|
||||
for (int i = 0; i < n; i++)
|
||||
threads[i] = (HANDLE)_beginthread(&routine, 0, &arr[i]);
|
||||
|
||||
// Process these threads
|
||||
WaitForMultipleObjects(n, threads, TRUE, INFINITE);
|
||||
return;
|
||||
}
|
||||
|
||||
// Driver program to test above functions
|
||||
int main()
|
||||
{
|
||||
// Doesn't work for negative numbers
|
||||
int arr[] = {34, 23, 122, 9};
|
||||
int n = sizeof(arr) / sizeof(arr[0]);
|
||||
|
||||
sleepSort (arr, n);
|
||||
|
||||
return(0);
|
||||
}
|
||||
68
spiral_print.cpp
Normal file
68
spiral_print.cpp
Normal file
@@ -0,0 +1,68 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
void genArray(int a[][10],int r,int c){
|
||||
|
||||
int value=1;
|
||||
for(int i=0;i<r;i++){
|
||||
for(int j=0;j<c;j++){
|
||||
a[i][j] = value;
|
||||
cout<<a[i][j]<<" ";
|
||||
value++;
|
||||
}
|
||||
cout<<endl;
|
||||
}
|
||||
}
|
||||
void spiralPrint(int a[][10],int r,int c){
|
||||
|
||||
int startRow=0,endRow=r-1;
|
||||
int startCol =0, endCol = c-1;
|
||||
int cnt=0;
|
||||
|
||||
while(startRow<=endRow && startCol<=endCol){
|
||||
|
||||
///Print start row
|
||||
for(int i=startCol;i<=endCol;i++,cnt++){
|
||||
cout<<a[startRow][i]<<" ";
|
||||
}
|
||||
startRow++;
|
||||
|
||||
///Print the end col
|
||||
for(int i=startRow;i<=endRow;i++,cnt++){
|
||||
cout<<a[i][endCol]<<" ";
|
||||
}
|
||||
endCol--;
|
||||
|
||||
///Print the end row
|
||||
if(cnt==r*c){
|
||||
break;
|
||||
}
|
||||
|
||||
for(int i=endCol;i>=startCol;i--,cnt++){
|
||||
cout<<a[endRow][i]<<" ";
|
||||
}
|
||||
endRow--;
|
||||
|
||||
///Print the start Col
|
||||
if(cnt==r*c){
|
||||
break;
|
||||
}
|
||||
for(int i=endRow;i>=startRow;i--,cnt++){
|
||||
cout<<a[i][startCol]<<" ";
|
||||
}
|
||||
startCol++;
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int a[10][10];
|
||||
|
||||
int r,c;
|
||||
cin>>r>>c;
|
||||
genArray(a,r,c);
|
||||
spiralPrint(a,r,c);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user