Skip to content

Commit acb2493

Browse files
committed
Added few more problems
1 parent 07fac76 commit acb2493

File tree

5 files changed

+285
-0
lines changed

5 files changed

+285
-0
lines changed

Backtracking/Fillthematrix.cpp

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
/*
2+
Fill The Matrix
3+
A matrix B (consisting of integers) of dimension N × N is said to be good if there exists an array A (consisting of integers) such that B[i][j] = |A[i] - A[j]|, where |x| denotes absolute value of integer x.
4+
5+
You are given a partially filled matrix B of dimension N × N. Q of the entries of this matrix are filled by either 0 or 1. You have to identify whether it is possible to fill the remaining entries of matrix B (the entries can be filled by any integer, not necessarily by 0 or 1) such that the resulting fully filled matrix B is good.
6+
Input
7+
The first line of the input contains an integer T denoting the number of test cases.
8+
9+
The first line of each test case contains two space separated integers N, Q.
10+
11+
Each of the next Q lines contain three space separated integers i, j, val, which means that B[i][j] is filled with value val.
12+
Output
13+
For each test case, output "yes" or "no" (without quotes) in a single line corresponding to the answer of the problem.
14+
Constraints
15+
1 ≤ T ≤ 10^6
16+
2 ≤ N ≤ 10^5
17+
1 ≤ Q ≤ 10^6
18+
1 ≤ i, j ≤ N
19+
0 ≤ val ≤ 1
20+
Sum of each of N, Q over all test cases doesn't exceed 106
21+
Input
22+
4
23+
2 2
24+
1 1 0
25+
1 2 1
26+
2 3
27+
1 1 0
28+
1 2 1
29+
2 1 0
30+
3 2
31+
2 2 0
32+
2 3 1
33+
3 3
34+
1 2 1
35+
2 3 1
36+
1 3 1
37+
Output
38+
yes
39+
no
40+
yes
41+
no
42+
*/
43+
44+
45+
46+
47+
48+
49+
50+
51+
#include <bits/stdc++.h>
52+
using namespace std;
53+
54+
typedef long long ll;
55+
typedef vector<int> vi;
56+
typedef pair<int,int> ii;
57+
58+
#define fill(a,x) memset(a,x,sizeof(a))
59+
#define pb push_back
60+
#define sz(x) (int)x.size()
61+
#define F first
62+
#define S second
63+
#define FOR(i,a,b) for(int i = a; i<=b; ++i)
64+
#define NFOR(i,a,b) for(int i = a; i>=b; --i)
65+
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
66+
const ll INF = 1e18;
67+
const ll mod = 1e9+7;
68+
const int N = 1e5+10;
69+
70+
bool done[N];
71+
int arr[N];
72+
int grp[N];
73+
vi edges[N];
74+
vi zeros[N];
75+
vector<ii> ones;
76+
77+
void dfsz(int s,int p,int g)
78+
{
79+
done[s]=true;
80+
grp[s]=g;
81+
for(int i=0;i<sz(zeros[s]);i++)
82+
{
83+
if(zeros[s][i]==p or done[zeros[s][i]])
84+
continue;
85+
86+
dfsz(zeros[s][i],s,g);
87+
}
88+
}
89+
90+
bool dfs(int s,int p)
91+
{
92+
done[s]=true;
93+
for(int i=0;i<sz(edges[s]);i++)
94+
{
95+
if(edges[s][i]==p)
96+
continue;
97+
98+
if(arr[edges[s][i]]==arr[s])
99+
return false;
100+
101+
if(done[edges[s][i]])
102+
continue;
103+
104+
arr[edges[s][i]]=1-arr[s];
105+
bool check=dfs(edges[s][i],s);
106+
if(check==false)
107+
return false;
108+
}
109+
110+
return true;
111+
}
112+
int main(){
113+
fast;
114+
int t;
115+
cin>>t;
116+
while(t--)
117+
{
118+
ones.clear();
119+
int n,q;
120+
cin>>n>>q;
121+
FOR(i,1,n)
122+
{
123+
edges[i].clear();
124+
zeros[i].clear();
125+
arr[i]=-1;
126+
grp[i]=0;
127+
done[i]=false;
128+
}
129+
130+
bool ans=true;
131+
FOR(k,1,q)
132+
{
133+
int i,j,val;
134+
cin>>i>>j>>val;
135+
if(i==j and val==1)
136+
ans=false;
137+
if(i==j and val==0)
138+
continue;
139+
if(val==0)
140+
{
141+
zeros[i].pb(j);
142+
zeros[j].pb(i);
143+
}
144+
if(val==1)
145+
ones.pb(make_pair(i,j));
146+
}
147+
148+
if(ans==false)
149+
{
150+
cout<<"no"<<endl;
151+
continue;
152+
}
153+
154+
int g=1;
155+
for(int i=1;i<=n;i++)
156+
{
157+
if(!done[i])
158+
{
159+
dfsz(i,0,g);
160+
g++;
161+
}
162+
}
163+
164+
g--;
165+
FOR(i,0,sz(ones)-1)
166+
{
167+
if(grp[ones[i].F]==grp[ones[i].S])
168+
ans=false;
169+
else
170+
{
171+
edges[grp[ones[i].F]].pb(grp[ones[i].S]);
172+
edges[grp[ones[i].S]].pb(grp[ones[i].F]);
173+
}
174+
}
175+
176+
if(ans==false)
177+
{
178+
cout<<"no"<<endl;
179+
continue;
180+
}
181+
182+
for(int i=1;i<=g;i++)
183+
done[i]=false;
184+
185+
for(int i=1;i<=g;i++)
186+
{
187+
if(!done[i])
188+
{
189+
arr[i]=0;
190+
ans=dfs(i,0);
191+
if(ans==false)
192+
{
193+
cout<<"no"<<endl;
194+
break;
195+
}
196+
}
197+
}
198+
199+
if(ans)
200+
cout<<"yes"<<endl;
201+
}
202+
return 0;
203+
}

Backtracking/Nqueens.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
3+
Name: Mehul Chaturvedi
4+
IIT-Guwahati
5+
6+
*/
7+
8+
/*
9+
The very famous Nqueen problem. Quite straightforward.
10+
*/
11+
12+
#include <bits/stdc++.h>
13+
14+
using namespace std;
15+
16+
int board[11][11];
17+
18+
bool isPossible(int n,int row,int col){
19+
20+
// Same Column
21+
for(int i=row-1;i>=0;i--){
22+
if(board[i][col] == 1){
23+
return false;
24+
}
25+
}
26+
//Upper Left Diagonal
27+
for(int i=row-1,j=col-1;i>=0 && j>=0 ; i--,j--){
28+
if(board[i][j] ==1){
29+
return false;
30+
}
31+
}
32+
33+
// Upper Right Diagonal
34+
35+
for(int i=row-1,j=col+1;i>=0 && j<n ; i--,j++){
36+
if(board[i][j] == 1){
37+
return false;
38+
}
39+
}
40+
41+
return true;
42+
}
43+
void nQueenHelper(int n,int row){
44+
if(row==n){
45+
// We have reached some solution.
46+
// Print the board matrix
47+
// return
48+
49+
for(int i=0;i<n;i++){
50+
for(int j=0;j<n;j++){
51+
cout << board[i][j] << " ";
52+
}
53+
}
54+
cout<<endl;
55+
return;
56+
57+
}
58+
59+
// Place at all possible positions and move to smaller problem
60+
for(int j=0;j<n;j++){
61+
62+
if(isPossible(n,row,j)){
63+
board[row][j] = 1;
64+
nQueenHelper(n,row+1);
65+
board[row][j] = 0;
66+
}
67+
}
68+
return;
69+
70+
}
71+
void placeNQueens(int n){
72+
73+
memset(board,0,11*11*sizeof(int));
74+
75+
nQueenHelper(n,0);
76+
77+
}
78+
int main(){
79+
80+
placeNQueens(4);
81+
return 0;
82+
}
4.84 MB
Binary file not shown.
Binary file not shown.
655 KB
Binary file not shown.

0 commit comments

Comments
 (0)