#include <bits/stdc++.h> using namespace std; bool isPossible(int **input, int i, int j, int num) { for(int r = 0; r < 9; r++) { if(input[r][j] == num || input[i][r] == num) return false; } int row = (i / 3) * 3, col = (j / 3) * 3; for(int r = row; r < row + 3; r++) { for(int c = col; c < col + 3; c++) { if(input[r][c] == num) return false; } } return true; } bool sodukuSolver(int **input, int i, int j) { if(i == 9) { return true; } if(j == 9) { return sodukuSolver(input, i + 1, 0); } if(input[i][j] == 0) { for(int num = 1; num <= 9; num++) { if(isPossible(input, i, j, num)) { input[i][j] = num; if(sodukuSolver(input, i, j + 1)) { return true; } } } input[i][j] = 0; return false; } else { return sodukuSolver(input, i, j + 1); } } int main() { int **input = new int*[9]; for(int i = 0; i < 9; i++) { input[i] = new int[9]; char s[10]; cin>>s; for(int j = 0; s[j] != '\0'; j++) { input[i][j] = s[j] - '0'; } } bool canSolve = sodukuSolver(input, 0, 0); if(canSolve) { cout << "Soduku Can Be Solved In Following Way :" << endl; for(int i = 0; i < 9; i++) { for(int j = 0; j < 9; j++) { cout<<input[i][j]; } cout<<endl; } } else { cout << endl << "Soduku Cannot Be Solved" << endl; } return 0; }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}