Skip to content

Commit 1346c4e

Browse files
Merge pull request #197 from JitinSaxenaa/sudoku
Sudoku
2 parents 985eef6 + 7055ce2 commit 1346c4e

3 files changed

Lines changed: 128 additions & 43 deletions

File tree

CPP/data_structures/graphs/graph/number of provinces.cpp

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
class Solution {
5+
private:
6+
void dfs(int node, vector<int> adjLs[], int vis[]) {
7+
vis[node] = 1;
8+
for (auto it : adjLs[node]) {
9+
if (!vis[it]) {
10+
dfs(it, adjLs, vis);
11+
}
12+
}
13+
}
14+
public:
15+
int numProvinces(vector<vector<int>> adj, int V) {
16+
vector<int> adjLs[V];
17+
for (int i = 0; i < V; i++) {
18+
for (int j = 0; j < V; j++) {
19+
if (adj[i][j] == 1 && i != j) {
20+
adjLs[i].push_back(j);
21+
adjLs[j].push_back(i);
22+
}
23+
}
24+
}
25+
int vis[V] = {0};
26+
int cnt = 0;
27+
for (int i = 0; i < V; i++) {
28+
if (!vis[i]) {
29+
cnt++;
30+
dfs(i, adjLs, vis);
31+
}
32+
}
33+
return cnt;
34+
}
35+
};
36+
37+
// Example usage
38+
int main() {
39+
vector<vector<int>> adj = {
40+
{1,1,0},
41+
{1,1,0},
42+
{0,0,1}
43+
};
44+
int V = adj.size();
45+
46+
Solution sol;
47+
cout << "Number of provinces: " << sol.numProvinces(adj, V) << endl;
48+
return 0;
49+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package Java.algorithms.arrays;
2+
3+
public class SudokuSolver {
4+
5+
private static final int SIZE = 9;
6+
7+
public void solveSudoku(char[][] board) {
8+
solve(board);
9+
}
10+
11+
private boolean solve(char[][] board) {
12+
for (int row = 0; row < SIZE; row++) {
13+
for (int col = 0; col < SIZE; col++) {
14+
if (board[row][col] == '.') {
15+
for (char num = '1'; num <= '9'; num++) {
16+
if (isValid(board, row, col, num)) {
17+
board[row][col] = num;
18+
if (solve(board)) return true;
19+
board[row][col] = '.';
20+
}
21+
}
22+
return false;
23+
}
24+
}
25+
}
26+
return true;
27+
}
28+
29+
private boolean isValid(char[][] board, int row, int col, char num) {
30+
for (int i = 0; i < SIZE; i++) {
31+
if (board[row][i] == num || board[i][col] == num) return false;
32+
}
33+
int startRow = (row / 3) * 3;
34+
int startCol = (col / 3) * 3;
35+
for (int i = startRow; i < startRow + 3; i++) {
36+
for (int j = startCol; j < startCol + 3; j++) {
37+
if (board[i][j] == num) return false;
38+
}
39+
}
40+
return true;
41+
}
42+
43+
private void printBoard(char[][] board) {
44+
for (int i = 0; i < SIZE; i++) {
45+
for (int j = 0; j < SIZE; j++) {
46+
System.out.print(board[i][j] + " ");
47+
}
48+
System.out.println();
49+
}
50+
}
51+
52+
/**
53+
* Simulates a C/C++ style int main() function
54+
*/
55+
public static int run() {
56+
char[][] board = {
57+
{'5','3','.','.','7','.','.','.','.'},
58+
{'6','.','.','1','9','5','.','.','.'},
59+
{'.','9','8','.','.','.','.','6','.'},
60+
{'8','.','.','.','6','.','.','.','3'},
61+
{'4','.','.','8','.','3','.','.','1'},
62+
{'7','.','.','.','2','.','.','.','6'},
63+
{'.','6','.','.','.','.','2','8','.'},
64+
{'.','.','.','4','1','9','.','.','5'},
65+
{'.','.','.','.','8','.','.','7','9'}
66+
};
67+
68+
SudokuSolver solver = new SudokuSolver();
69+
solver.solveSudoku(board);
70+
solver.printBoard(board);
71+
72+
return 0; // success
73+
}
74+
75+
public static void main(String[] args) {
76+
int status = run();
77+
System.exit(status);
78+
}
79+
}

0 commit comments

Comments
 (0)