Skip to content

Commit 12f31b2

Browse files
author
Harshil bhardwaj
authored
Merge pull request #58 from Pranit5895/master
Solutions
2 parents ea17a3e + a4e6498 commit 12f31b2

File tree

3 files changed

+350
-0
lines changed

3 files changed

+350
-0
lines changed

22Day22/SOL/N-Queens.java

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/* Java program to solve N Queen Problem using
2+
backtracking */
3+
public class NQueenProblem {
4+
final int N = 4;
5+
/* A utility function to print solution */
6+
void printSolution(int board[][])
7+
{
8+
for (int i = 0; i < N; i++) {
9+
for (int j = 0; j < N; j++)
10+
System.out.print(" " + board[i][j]
11+
+ " ");
12+
System.out.println();
13+
}
14+
}
15+
/* A utility function to check if a queen can
16+
be placed on board[row][col]. Note that this
17+
function is called when "col" queens are already
18+
placeed in columns from 0 to col -1. So we need
19+
to check only left side for attacking queens */
20+
boolean isSafe(int board[][], int row, int col)
21+
{
22+
int i, j;
23+
24+
/* Check this row on left side */
25+
for (i = 0; i < col; i++)
26+
if (board[row][i] == 1)
27+
return false;
28+
29+
/* Check upper diagonal on left side */
30+
for (i = row, j = col; i >= 0 && j >= 0; i--, j--)
31+
if (board[i][j] == 1)
32+
return false;
33+
34+
/* Check lower diagonal on left side */
35+
for (i = row, j = col; j >= 0 && i < N; i++, j--)
36+
if (board[i][j] == 1)
37+
return false;
38+
39+
return true;
40+
}
41+
/* A recursive utility function to solve N
42+
Queen problem */
43+
boolean solveNQUtil(int board[][], int col)
44+
{
45+
/* base case: If all queens are placed
46+
then return true */
47+
if (col >= N)
48+
return true;
49+
50+
/* Consider this column and try placing
51+
this queen in all rows one by one */
52+
for (int i = 0; i < N; i++) {
53+
/* Check if the queen can be placed on
54+
board[i][col] */
55+
if (isSafe(board, i, col)) {
56+
/* Place this queen in board[i][col] */
57+
board[i][col] = 1;
58+
59+
/* recur to place rest of the queens */
60+
if (solveNQUtil(board, col + 1) == true)
61+
return true;
62+
63+
/* If placing queen in board[i][col]
64+
doesn't lead to a solution then
65+
remove queen from board[i][col] */
66+
board[i][col] = 0; // BACKTRACK
67+
}
68+
}
69+
70+
/* If the queen can not be placed in any row in
71+
this colum col, then return false */
72+
return false;
73+
}
74+
/* This function solves the N Queen problem using
75+
Backtracking. It mainly uses solveNQUtil () to
76+
solve the problem. It returns false if queens
77+
cannot be placed, otherwise, return true and
78+
prints placement of queens in the form of 1s.
79+
Please note that there may be more than one
80+
solutions, this function prints one of the
81+
feasible solutions.*/
82+
boolean solveNQ()
83+
{
84+
int board[][] = { { 0, 0, 0, 0 },
85+
{ 0, 0, 0, 0 },
86+
{ 0, 0, 0, 0 },
87+
{ 0, 0, 0, 0 } };
88+
89+
if (solveNQUtil(board, 0) == false) {
90+
System.out.print("Solution does not exist");
91+
return false;
92+
}
93+
94+
printSolution(board);
95+
return true;
96+
}
97+
// driver program to test above function
98+
public static void main(String args[])
99+
{
100+
NQueenProblem Queen = new NQueenProblem();
101+
Queen.solveNQ();
102+
}
103+
}

24Day24/SOL/8-Queens.java

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// This program solves the classic "8 queens" problem using recursive
2+
// backtracking.
3+
4+
import java.util.*;
5+
public class Queens {
6+
public static void main(String[] args) {
7+
giveIntro();
8+
Scanner console = new Scanner(System.in);
9+
System.out.print("What size board do you want to use? ");
10+
int size = console.nextInt();
11+
System.out.println();
12+
Board b = new Board(size);
13+
solve(b);
14+
}
15+
// post: explains program to user
16+
public static void giveIntro() {
17+
System.out.println("This program solves the classic '8 queens'");
18+
System.out.println("problem, placing queens on a chessboard so");
19+
System.out.println("that no two queens threaten each other.");
20+
System.out.println();
21+
}
22+
// pre : queens have been safely placed in columns 1 through (col - 1)
23+
// post: recursively searchs the board for a solution starting at col,
24+
// returning true iff such a solution occurs and storing the
25+
// solution in board if it exists
26+
public static boolean explore(Board b, int col) {
27+
if (col > b.size())
28+
return true;
29+
else {
30+
for (int row = 1; row <= b.size(); row++)
31+
if (b.safe(row, col)) {
32+
b.place(row, col);
33+
for (int i = 1; i < col; i++)
34+
System.out.print(" ");
35+
System.out.println("Q in row " + row + " of col " + col);
36+
if (explore(b, col + 1))
37+
return true;
38+
b.remove(row, col);
39+
}
40+
return false;
41+
}
42+
}
43+
44+
// post: searches for a solution to the 8 queens problem with this
45+
// board, reporting result.
46+
public static void solve(Board solution) {
47+
if (!explore(solution, 1))
48+
System.out.println("No solution.");
49+
else {
50+
System.out.println("One solution is as follows:");
51+
solution.print();
52+
}
53+
}
54+
}

28Day28/SOL/Solution.java

+193
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
// Java program to create a simple calculator
2+
// with basic +, -, /, * using java swing elements
3+
4+
import java.awt.event.*;
5+
import javax.swing.*;
6+
import java.awt.*;
7+
class calculator extends JFrame implements ActionListener {
8+
// create a frame
9+
static JFrame f;
10+
11+
// create a textfield
12+
static JTextField l;
13+
14+
// store oprerator and operands
15+
String s0, s1, s2;
16+
17+
// default constrcutor
18+
calculator()
19+
{
20+
s0 = s1 = s2 = "";
21+
}
22+
// main function
23+
public static void main(String args[])
24+
{
25+
// create a frame
26+
f = new JFrame("calculator");
27+
try {
28+
// set look and feel
29+
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
30+
}
31+
catch (Exception e) {
32+
System.err.println(e.getMessage());
33+
}
34+
// create a object of class
35+
calculator c = new calculator();
36+
37+
// create a textfield
38+
l = new JTextField(16);
39+
40+
// set the textfield to non editable
41+
l.setEditable(false);
42+
43+
// create number buttons and some operators
44+
JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bs, bd, bm, be, beq, beq1;
45+
// create number buttons
46+
b0 = new JButton("0");
47+
b1 = new JButton("1");
48+
b2 = new JButton("2");
49+
b3 = new JButton("3");
50+
b4 = new JButton("4");
51+
b5 = new JButton("5");
52+
b6 = new JButton("6");
53+
b7 = new JButton("7");
54+
b8 = new JButton("8");
55+
b9 = new JButton("9");
56+
57+
// equals button
58+
beq1 = new JButton("=");
59+
60+
// create operator buttons
61+
ba = new JButton("+");
62+
bs = new JButton("-");
63+
bd = new JButton("/");
64+
bm = new JButton("*");
65+
beq = new JButton("C");
66+
67+
// create . button
68+
be = new JButton(".");
69+
// create a panel
70+
JPanel p = new JPanel();
71+
72+
// add action listeners
73+
bm.addActionListener(c);
74+
bd.addActionListener(c);
75+
bs.addActionListener(c);
76+
ba.addActionListener(c);
77+
b9.addActionListener(c);
78+
b8.addActionListener(c);
79+
b7.addActionListener(c);
80+
b6.addActionListener(c);
81+
b5.addActionListener(c);
82+
b4.addActionListener(c);
83+
b3.addActionListener(c);
84+
b2.addActionListener(c);
85+
b1.addActionListener(c);
86+
b0.addActionListener(c);
87+
be.addActionListener(c);
88+
beq.addActionListener(c);
89+
beq1.addActionListener(c);
90+
91+
// add elements to panel
92+
p.add(l);
93+
p.add(ba);
94+
p.add(b1);
95+
p.add(b2);
96+
p.add(b3);
97+
p.add(bs);
98+
p.add(b4);
99+
p.add(b5);
100+
p.add(b6);
101+
p.add(bm);
102+
p.add(b7);
103+
p.add(b8);
104+
p.add(b9);
105+
p.add(bd);
106+
p.add(be);
107+
p.add(b0);
108+
p.add(beq);
109+
p.add(beq1);
110+
111+
// set Background of panel
112+
p.setBackground(Color.blue);
113+
114+
// add panel to frame
115+
f.add(p);
116+
117+
f.setSize(200, 220);
118+
f.show();
119+
}
120+
public void actionPerformed(ActionEvent e)
121+
{
122+
String s = e.getActionCommand();
123+
124+
// if the value is a number
125+
if ((s.charAt(0) >= '0' && s.charAt(0) <= '9') || s.charAt(0) == '.') {
126+
// if operand is present then add to second no
127+
if (!s1.equals(""))
128+
s2 = s2 + s;
129+
else
130+
s0 = s0 + s;
131+
132+
// set the value of text
133+
l.setText(s0 + s1 + s2);
134+
}
135+
else if (s.charAt(0) == 'C') {
136+
// clear the one letter
137+
s0 = s1 = s2 = "";
138+
139+
// set the value of text
140+
l.setText(s0 + s1 + s2);
141+
}
142+
else if (s.charAt(0) == '=') {
143+
double te;
144+
145+
// store the value in 1st
146+
if (s1.equals("+"))
147+
te = (Double.parseDouble(s0) + Double.parseDouble(s2));
148+
else if (s1.equals("-"))
149+
te = (Double.parseDouble(s0) - Double.parseDouble(s2));
150+
else if (s1.equals("/"))
151+
te = (Double.parseDouble(s0) / Double.parseDouble(s2));
152+
else
153+
te = (Double.parseDouble(s0) * Double.parseDouble(s2));
154+
155+
// set the value of text
156+
l.setText(s0 + s1 + s2 + "=" + te);
157+
158+
// convert it to string
159+
s0 = Double.toString(te);
160+
161+
s1 = s2 = "";
162+
}
163+
else {
164+
// if there was no operand
165+
if (s1.equals("") || s2.equals(""))
166+
s1 = s;
167+
// else evaluate
168+
else {
169+
double te;
170+
171+
// store the value in 1st
172+
if (s1.equals("+"))
173+
te = (Double.parseDouble(s0) + Double.parseDouble(s2));
174+
else if (s1.equals("-"))
175+
te = (Double.parseDouble(s0) - Double.parseDouble(s2));
176+
else if (s1.equals("/"))
177+
te = (Double.parseDouble(s0) / Double.parseDouble(s2));
178+
else
179+
te = (Double.parseDouble(s0) * Double.parseDouble(s2));
180+
// convert it to string
181+
s0 = Double.toString(te);
182+
183+
// place the operator
184+
s1 = s;
185+
186+
// make the operand blank
187+
s2 = "";
188+
}
189+
// set the value of text
190+
l.setText(s0 + s1 + s2);
191+
}
192+
}
193+
}

0 commit comments

Comments
 (0)