Skip to content

Commit 2c62330

Browse files
committed
Added all graph problems to graph folder
1 parent 265b947 commit 2c62330

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+2648
-2717
lines changed

Diff for: Backtracking

+88-88
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,88 @@
1-
#include <iostream.h>
2-
3-
//Number of queens
4-
int N;
5-
6-
//chessboard
7-
int board[100][100];
8-
9-
//function to check if the cell is attacked or not
10-
int is_attack(int i,int j)
11-
{
12-
int k,l;
13-
//checking if there is a queen in row or column
14-
for(k=0;k<N;k++)
15-
{
16-
if((board[i][k] == 1) || (board[k][j] == 1))
17-
return 1;
18-
}
19-
//checking for diagonals
20-
for(k=0;k<N;k++)
21-
{
22-
for(l=0;l<N;l++)
23-
{
24-
if(((k+l) == (i+j)) || ((k-l) == (i-j)))
25-
{
26-
if(board[k][l] == 1)
27-
return 1;
28-
}
29-
}
30-
}
31-
return 0;
32-
}
33-
34-
int N_queen(int n)
35-
{
36-
int i,j;
37-
//if n is 0, solution found
38-
if(n==0)
39-
return 1;
40-
for(i=0;i<N;i++)
41-
{
42-
for(j=0;j<N;j++)
43-
{
44-
//checking if we can place a queen here or not
45-
//queen will not be placed if the place is being attacked
46-
//or already occupied
47-
if((!is_attack(i,j)) && (board[i][j]!=1))
48-
{
49-
board[i][j] = 1;
50-
//recursion
51-
//wether we can put the next queen with this arrangment or not
52-
if(N_queen(n-1)==1)
53-
{
54-
return 1;
55-
}
56-
board[i][j] = 0;
57-
}
58-
}
59-
}
60-
return 0;
61-
}
62-
63-
int main()
64-
{
65-
//taking the value of N
66-
cout<<"Enter the value of N for NxN chessboard\n";
67-
cin>>N;
68-
69-
int i,j;
70-
//setting all elements to 0
71-
for(i=0;i<N;i++)
72-
{
73-
for(j=0;j<N;j++)
74-
{
75-
board[i][j]=0;
76-
}
77-
}
78-
//calling the function
79-
N_queen(N);
80-
//printing the matix
81-
for(i=0;i<N;i++)
82-
{
83-
for(j=0;j<N;j++)
84-
cout<<board[i][j]<<" ";
85-
cout<<"\n";
86-
}
87-
88-
}
1+
#include <iostream.h>
2+
3+
//Number of queens
4+
int N;
5+
6+
//chessboard
7+
int board[100][100];
8+
9+
//function to check if the cell is attacked or not
10+
int is_attack(int i,int j)
11+
{
12+
int k,l;
13+
//checking if there is a queen in row or column
14+
for(k=0;k<N;k++)
15+
{
16+
if((board[i][k] == 1) || (board[k][j] == 1))
17+
return 1;
18+
}
19+
//checking for diagonals
20+
for(k=0;k<N;k++)
21+
{
22+
for(l=0;l<N;l++)
23+
{
24+
if(((k+l) == (i+j)) || ((k-l) == (i-j)))
25+
{
26+
if(board[k][l] == 1)
27+
return 1;
28+
}
29+
}
30+
}
31+
return 0;
32+
}
33+
34+
int N_queen(int n)
35+
{
36+
int i,j;
37+
//if n is 0, solution found
38+
if(n==0)
39+
return 1;
40+
for(i=0;i<N;i++)
41+
{
42+
for(j=0;j<N;j++)
43+
{
44+
//checking if we can place a queen here or not
45+
//queen will not be placed if the place is being attacked
46+
//or already occupied
47+
if((!is_attack(i,j)) && (board[i][j]!=1))
48+
{
49+
board[i][j] = 1;
50+
//recursion
51+
//wether we can put the next queen with this arrangment or not
52+
if(N_queen(n-1)==1)
53+
{
54+
return 1;
55+
}
56+
board[i][j] = 0;
57+
}
58+
}
59+
}
60+
return 0;
61+
}
62+
63+
int main()
64+
{
65+
//taking the value of N
66+
cout<<"Enter the value of N for NxN chessboard\n";
67+
cin>>N;
68+
69+
int i,j;
70+
//setting all elements to 0
71+
for(i=0;i<N;i++)
72+
{
73+
for(j=0;j<N;j++)
74+
{
75+
board[i][j]=0;
76+
}
77+
}
78+
//calling the function
79+
N_queen(N);
80+
//printing the matix
81+
for(i=0;i<N;i++)
82+
{
83+
for(j=0;j<N;j++)
84+
cout<<board[i][j]<<" ";
85+
cout<<"\n";
86+
}
87+
88+
}

Diff for: Backtracking : N Queens Problem

-69
This file was deleted.

Diff for: CalculateTheGivenSeries.cpp

+48-48
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,49 @@
1-
//C++ program to find the sum of the series (1)+(1+2)+(1+2+3)+(1+2+3+...+n)
2-
3-
#include <iostream>
4-
5-
using namespace std;
6-
7-
int main()
8-
9-
{
10-
11-
int i, j, n, sum, total = 0;
12-
13-
cout << "Enter the value for nth term: ";
14-
15-
cin >> n;
16-
17-
for (i = 1; i <= n; i++)
18-
19-
{
20-
21-
sum = 0;
22-
23-
for (j = 1; j <= i; j++)
24-
25-
{
26-
27-
total += j;
28-
29-
sum += j;
30-
31-
cout << j;
32-
33-
if (j < i)
34-
35-
{
36-
37-
cout << "+";
38-
39-
}
40-
41-
}
42-
43-
cout << " = " << sum << endl;
44-
45-
}
46-
47-
cout << "\nThe sum of the above series is: " << total << endl;
48-
1+
//C++ program to find the sum of the series (1)+(1+2)+(1+2+3)+(1+2+3+...+n)
2+
3+
#include <iostream>
4+
5+
using namespace std;
6+
7+
int main()
8+
9+
{
10+
11+
int i, j, n, sum, total = 0;
12+
13+
cout << "Enter the value for nth term: ";
14+
15+
cin >> n;
16+
17+
for (i = 1; i <= n; i++)
18+
19+
{
20+
21+
sum = 0;
22+
23+
for (j = 1; j <= i; j++)
24+
25+
{
26+
27+
total += j;
28+
29+
sum += j;
30+
31+
cout << j;
32+
33+
if (j < i)
34+
35+
{
36+
37+
cout << "+";
38+
39+
}
40+
41+
}
42+
43+
cout << " = " << sum << endl;
44+
45+
}
46+
47+
cout << "\nThe sum of the above series is: " << total << endl;
48+
4949
}

0 commit comments

Comments
 (0)