-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2D_Array.cpp
More file actions
1260 lines (1006 loc) Β· 26.4 KB
/
2D_Array.cpp
File metadata and controls
1260 lines (1006 loc) Β· 26.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// π· 1. Mathematical Foundation of 2D Arrays
// A 2D array is mathematically a matrix:
// A = [a11 a12 a13
// a21 a22 a23
// a31 a32 a33]
// i β row index
// j β column index
// π Memory Representation (C++)
// C++ uses Row-Major Order:
//Address of A[i][j] = base + (i Γ cols + j) Γ size_of_element
// Address(A[i][j])=base+(iΓcols+j)
// π base is the starting address of the array in memory
// π Meaning: entire row is stored continuously in memory.
// π· 2. Traversing a 2D Array
// To traverse a 2D array, we use nested loops:
// π· 3. Core Traversal Logic
// πΉ Mathematical Idea
// Traversal = visiting all elements:
// For i from 0 to rows-1:
// For j from 0 to cols-1:
// Process A[i][j]
// πΉ Code Implementation
// π· 2. Traversal Techniques
// πΉ Row-wise Traversal
// for(int i = 0; i < rows; i++) {
// for(int j = 0; j < cols; j++) {
// cout << arr[i][j] << " ";
// }
// }
// πΉ Column-wise Traversal
// for(int j = 0; j < cols; j++) {
// for(int i = 0; i < rows; i++) {
// cout << arr[i][j] << " ";
// }
// }
#include<bits/stdc++.h>
using namespace std;
int main(){
int rows,cols;
cin>>rows>>cols;
vector<vector<int>>arr(rows,vector<int>(cols));
//input
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
cin>>arr[i][j];
}
}
//output
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
cout<<arr[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
// π· 1. Row-wise Sum (C++)
// π Concept
// Fix a row i, and add all elements across columns:
// RowSum (i) = β j=0 to cols-1 A[i][j]
// πΉ Code Implementation
#include<bits/stdc++.h>
using namespace std;
int main(){
int rows ,cols;
cin>>rows>>cols;
vector<vector<int>>arr(rows,vector<int>(cols));
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
cin>>arr[i][j];
}
}
for(int i=0;i<rows;i++){
int sum=0;
for(int j=0;j<cols;j++){
sum+=arr[i][j];
}
cout<<sum<<endl;
}
}
// π· 2. Column-wise Sum (C++)
// π Concept
// Fix a column j, and add all elements across rows:
// ColSum
// (j) = β i=0 to rows-1 A[i][j]
// πΉ Code Implementation
#include<bits/stdc++.h>
using namespace std;
int main(){
int rows,cols;
cin>>rows>>cols;
vector<vector<int>>arr(rows,vector<int>(cols));
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
cin>>arr[i][j];
}
}
for(int j=0;j<cols;j++){
int sum=0;
for(int i=0;i<rows;i++){
sum+=arr[i][j];
}
cout<<sum<<endl;
}
return 0;
}
// π· 4. Important Patterns in 2D Arrays
// πΉ Matrix Transpose
// Transpose of a matrix A is obtained by swapping rows with columns:
// A^T = [a11 a21 a31
// a12 a22 a32
// a13 a23 a33]
#include<bits/stdc++.h>
using namespace std;
int main(){
int rows,cols;
cin>>rows>>cols;
vector<vector<int>>arr(rows,vector<int>(cols));
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
cin>>arr[i][j];
}
}
vector<vector<int>>transpose(cols,vector<int>(rows));
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
transpose[j][i]=arr[i][j];
}
}
for(int i=0;i<cols;i++){
for(int j=0;j<rows;j++){
cout<<transpose[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
// πΆ (B) Diagonals
// π Primary diagonal: i=j
//π Secondary diagonal: i+j=nβ1
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
vector<int>primary_diagonal;
vector<int>secondary_diagonal;
vector<vector<int>>arr(n,vector<int>(n));
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>arr[i][j];
if(i==j){
primary_diagonal.push_back(arr[i][j]);
}
if(i+j==n-1){
secondary_diagonal.push_back(arr[i][j]);
}
}
}
cout<<"Primary Diagonal: ";
for(int i=0;i<primary_diagonal.size();i++){
cout<<primary_diagonal[i]<<" ";
}
cout<<endl;
cout<<"Secondary Diagonal: ";
for(int i=0;i<secondary_diagonal.size();i++){
cout<<secondary_diagonal[i]<<" ";
}
cout<<endl;
return 0;
}
// 5πΉ Spiral Traversal
// Algorithm:
// Top row
// Right column
// Bottom row
// Left column
// int top = 0, bottom = rows - 1;
// int left = 0, right = cols - 1;
// while(top <= bottom && left <= right) {
// for(int i = left; i <= right; i++)
// cout << arr[top][i] << " ";
// top++;
// for(int i = top; i <= bottom; i++)
// cout << arr[i][right] << " ";
// right--;
// if(top <= bottom) {
// for(int i = right; i >= left; i--)
// cout << arr[bottom][i] << " ";
// bottom--;
// }
// if(left <= right) {
// for(int i = bottom; i >= top; i--)
// cout << arr[i][left] << " ";
// left++;
// }
// }
//π· 6. Spiral Traversal
#include<bits/stdc++.h>
using namespace std;
int main(){
int rows,cols;
cin>>rows>>cols;
vector<vector<int>>arr(rows,vector<int>(cols));
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
cin>>arr[i][j];
}
}
// Spiral traversal logic would go here
int top=0,bottom=rows-1;
int left=0,right=cols-1;
while(top<=bottom && left<=right){
for(int i=left;i<=right;i++){
cout<<arr[top][i]<<" ";
}
top++;
for(int i=top;i<=bottom;i++){
cout<<arr[i][right]<<" ";
}
right--;
if(top<=bottom){
for(int i=right;i>=left;i--){
cout<<arr[bottom][i]<<" ";
}
bottom--;
}
if(left<=right){
for(int i=bottom;i>=top;i--){
cout<<arr[i][left]<<" ";
}
left++;
}
}
return 0;
}
// π· 7. Matrix Operations (Mathematical)
// πΉ Matrix Addition
// C = A + B
// C[i][j] = A[i][j] + B[i][j]
// πΉ Matrix Multiplication
// C = A Γ B
// C[i][j] = β k=0 to n-1 A[i][k] Γ B[k][j]
// πΉ Matrix Transpose
// C = A^T
// C[i][j] = A[j][i]
// πΉ Matrix Determinant (for 2x2)
// det(A) = a11*a22 - a12*a21
// πΉ Matrix Inverse (for 2x2)
// A^-1 = (1/det(A)) * adj(A)
// where adj(A) is the adjugate of A
//subtract two matrices
// C = A - B
// C[i][j] = A[i][j] - B[i][j]
// πΉ Scalar Multiplication
// C = k * A
// C[i][j] = k * A[i][j]
// πΉ Identity Matrix
// C = I
// C[i][j] = 1 if i == j, else 0
// πΉ Zero Matrix
// C = 0
// C[i][j] = 0 for all i, j
// πΉ Matrix Power
// C = A^k
// C = A Γ A Γ ... Γ A (k times)
// πΉ Matrix Rank
// rank(A) = number of linearly independent rows (or columns)
// πΉ Matrix Trace
// trace(A) = sum of elements on the main diagonal
// trace(A) = β i=0 to n-1 A[i][i]
// πΉ Matrix Symmetry
// A is symmetric if A = A^T
// A[i][j] = A[j][i] for all i, j
// πΉ Matrix Skew-Symmetry
// A is skew-symmetric if A = -A^T
// A[i][j] = -A[j][i] for all i, j
// πΉ Matrix Orthogonality
// A is orthogonal if A^T = A^-1
// A^T Γ A = I
// πΉ Matrix Eigenvalues and Eigenvectors
// A * v = Ξ» * v
// where Ξ» is an eigenvalue and v is the corresponding eigenvector
// πΉ Matrix Diagonalization
// A = PDP^-1
// where D is a diagonal matrix of eigenvalues and P is a matrix of corresponding eigenvectors
// πΉ Matrix Rank-Nullity Theorem
// rank(A) + nullity(A) = number of columns in A
// πΉ Matrix Row Reduction (Gaussian Elimination)
// πΉ Matrix LU Decomposition
// A = LU
// where L is a lower triangular matrix and U is an upper triangular matrix
// πΉ Matrix Addition implementation
#include<bits/stdc++.h>
using namespace std;
int main(){
int rows,cols;
cin>>rows>>cols;
vector<vector<int>>A(rows,vector<int>(cols));
vector<vector<int>>B(rows,vector<int>(cols));
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
cin>>A[i][j];
}
}
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
cin>>B[i][j];
}
}
vector<vector<int>>C(rows,vector<int>(cols));
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
C[i][j]=A[i][j]+B[i][j];
}
}
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
cout<<C[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
// πΉ Matrix Multiplication implementation
#include<bits/stdc++.h>
using namespace std;
int main(){
int rowsA,colsA,rowsB,colsB;
cin>>rowsA>>colsA;
cin>>rowsB>>colsB;
if(colsA!=rowsB){
cout<<"matrix multiplication are not posible"<<endl;
return 0;
}
vector<vector<int>>A(rowsA,vector<int>(colsA));
vector<vector<int>>B(rowsB,vector<int>(colsB));
for(int i=0;i<rowsA;i++){
for(int j=0;j<colsA;j++){
cin>>A[i][j];
}
}
for(int i=0;i<rowsB;i++){
for(int j=0;j<colsB;j++){
cin>>B[i][j];
}
}
vector<vector<int>>C(rowsA,vector<int>(colsB));
for(int i=0;i<rowsA;i++){
for(int j=0;j<colsB;j++){
C[i][j]=0;
for(int k=0;k<colsA;k++){
C[i][j]+=A[i][k]*B[k][j];
}
}
}
for(int i=0;i<rowsA;i++){
for(int j=0;j<colsB;j++){
cout<<C[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
// πΉ Matrix Determinant (for 2x2)
// det(A) = a11*a22 - a12*a21
#include<bits/stdc++.h>
using namespace std;
int main(){
int rows,cols;
cin>>rows>>cols;
if(rows!=2|| cols!=2){
cout<<"Determinant is only for 2*2 matrices"<<endl;
return 0;
}
vector<vector<int>>A(rows,vector<int>(cols));
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
cin>>A[i][j];
}
}
int determinant=A[0][0]*A[1][1]-A[0][1]*A[1][0];
cout<<"Determinant: "<<determinant<<endl;
return 0;
}
// π· 6. Prefix Sum (Very Important)
// π Mathematical Formula
// prefix[i][j] = A[i][j] + prefix[i-1][j] + prefix[i][j-1] - prefix[i-1][j-1]
// Explanation:
// prefix[i][j] = A[i][j] + top + left - overlap
// where:
// top = prefix[i-1][j]
// left = prefix[i][j-1]
// overlap = prefix[i-1][j-1]
// π· 1. What is Prefix Sum?
// π Prefix sum means precomputing cumulative sums so that future queries become very fast.
// πΉ 1D Idea First (Easy Understanding)
// Given array:
// [2, 4, 1, 3, 5]
// Prefix sum:
// [2, 6, 7, 10, 15]
// π Formula
// prefix[i]=prefix[iβ1]+arr[i]
// πΉ Why?
// π To quickly find sum of any subarray:
// sum(L,R)=prefix[R]βprefix[Lβ1]
// π· 2. 2D Prefix Sum (Main Concept)
// Now extend to matrix.
// πΉ Given Matrix
// 1 2 3
// 4 5 6
// 7 8 9
// πΉ Prefix Matrix Meaning
// π prefix[i][j] = sum of all elements from (0,0) to (i,j)
// π· 3. Mathematical Formula (VERY IMPORTANT)
// prefix[i][j] = A[i][j] + prefix[i-1][j] + prefix[i][j-1] - prefix[i-1][j-1]
// π₯ Why subtract?
// π Because overlap is counted twice:
// top area
// left area
// π overlap counted twice β subtract once
// π· 4. Step-by-Step Example
// Matrix:
// 1 2 3
// 4 5 6
// 7 8 9
// πΉ Calculate prefix[1][1] (value = 5)
//=5+(top=2)+(left=4)β(overlap=1)
// =5+2+4β1=10
// Final Prefix Matrix
// 1 3 6
// 5 12 21
// 12 27 45
//π· 5. How to Use Prefix Sum (Main Purpose)
// π To find sum of any submatrix quickly
// π Formula
// For rectangle:
// Top-left = (r1, c1)
// Bottom-right = (r2, c2)
//sum=prefix[r2][c2]βprefix[r1β1][c2]βprefix[r2][c1β1]+prefix[r1β1][c1β1]
// π₯ Why?
// Same idea:
// Take full rectangle
// Remove extra parts
// Add overlap back
#include<bits/stdc++.h>
using namespace std;
int main(){
int rows,cols;
cin>>rows>>cols;
vector<vector<int>>A(rows,vector<int>(cols));
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
cin>>A[i][j];
}
}
vector<vector<int>>prefix(rows,vector<int>(cols));
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
int top=(i>0)?prefix[i-1][j]:0;
int left=(j>0)?prefix[i][j-1]:0;
int overlap=(i>0 && j>0)?prefix[i-1][j-1]:0;
prefix[i][j]=A[i][j]+top+left-overlap;
}
}
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
cout<<prefix[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
// π· 1. Linear Search in 2D Array
// π Algorithm (Step-by-step)
// Traverse each row
// Traverse each column
// Compare element with key
// If match β print position
// π§ Mathematical Idea
// βiβ[0,n),βjβ[0,m)
// π Check every element β brute force
// β± Time Complexity :O(nΓm)
#include<bits/stdc++.h>
using namespace std;
int main(){
int rows,cols,key;
cin>>rows>>cols>>key;
vector<vector<int>>arr(rows,vector<int>(cols));
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
cin>>arr[i][j];
}
}
bool found=false;
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
if(arr[i][j]==key){
cout<<"element found at position:"<<i<<" "<<j<<endl;
found=true;
break;
}
}
if(found){
break;
}
}
if(!found){
cout<<"element not found"<<endl;
}
return 0;
}
// π· 1. Core Idea: Grid = Graph
// π Think of the grid as a graph:
// Each cell β Node
// Movement (up/down/left/right) β Edges
// Example grid:
// 1 1 0
// 0 1 0
// 1 0 1
// π Each (i, j) is a node.
// π· 2. What is BFS?
// π BFS (Breadth First Search) explores nodes level by level
// First β starting node
// Then β all neighbors
// Then β neighbors of neighbors
// π Uses Queue (FIFO)
// π· 3. Movement in Grid
// You defined:
// int dx[] = {1, -1, 0, 0};
// int dy[] = {0, 0, 1, -1};
// π These represent directions:
// Direction dx dy
// Down +1 0
// Up -1 0
// Right 0 +1
// Left 0 -1
// π· 4. BFS Algorithm in Grid
// 1. Initialize queue and visited array
// 2. Enqueue starting node (x, y) and mark visited
// 3. While queue not empty:
// a. Dequeue node (cx, cy)
// b. For each direction (dx[i], dy[i]):
// i. Calculate new position (nx, ny) = (cx + dx[i], cy + dy[i])
// ii. Check if (nx, ny) is within bounds and not visited
// iii. If valid β Enqueue (nx, ny) and mark visited
// 4. Continue until queue is empty or target found
#include<bits/stdc++.h>
using namespace std;
int main(){
int rows,cols;
cin>>rows>>cols;
vector<vector<int>>grid(rows,vector<int>(cols));
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
cin>>grid[i][j];
}
}
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
vector<vector<bool>>visited(rows,vector<bool>(cols,false));
queue<pair<int,int>>q;
// Starting point (0, 0)
q.push({0,0});
visited[0][0]=true;
while(!q.empty()){
auto current=q.front();
q.pop();
int cx=current.first;
int cy=current.second;
cout<<"Visited: "<<cx<<" "<<cy<<endl;
for(int i=0;i<4;i++){
int nx=cx+dx[i];
int ny=cy+dy[i];
// Check bounds and visited
if(nx>=0 && nx<rows && ny>=0 && ny<cols && !visited[nx][ny]){
q.push({nx,ny});
visited[nx][ny]=true;
}
}
}
return 0;
}
// π· 1. What DFS Really Means
// π DFS (Depth First Search) =
// Go as deep as possible in one direction, then backtrack and try other directions.
// πΆ Simple Idea
// From one cell:
// Go down as far as possible
// Then try up
// Then right
// Then left
// π One path fully β then next path
// π· 2. Grid = Graph (Important Understanding)
// For a matrix:
// 1 1 0
// 0 1 0
// 1 0 1
// π Treat as graph:
// Each cell = node
// Moves (up, down, left, right) = edges
// π· 3. Your Code (Understanding First)
// void dfs(int i, int j, vector<vector<int>>& grid) {
// if(i < 0 || j < 0 || i >= grid.size() || j >= grid[0].size())
// return;
// dfs(i+1, j, grid);
// dfs(i-1, j, grid);
// dfs(i, j+1, grid);
// dfs(i, j-1, grid);
// }
// π΄ Problem in Your Code
// π This code is incomplete β
// Why?
// No visited check
// Will revisit same cell again and again
// π Leads to infinite recursion / crash
// π· 4. Correct DFS (Full Logic)
// π You must:
// Check boundary
// Check if already visited
// Mark visited
// Explore neighbors
#include<bits/stdc++.h>
using namespace std;
void dfs(int i,int j,vector<vector<int>>&grid,vector<vector<bool>>&visited){
// Boundary check
int rows=grid.size();
int cols=grid[0].size();
if(i<0||j<0||i>=rows||j>=cols||grid[i][j]==0||visited[i][j]){
return;
}
// Mark visited
visited[i][j]=true;
// PRINT HERE
cout << "Visited: " << i << "," << j << endl;
// Explore neighbors
dfs(i+1,j,grid,visited); // down
dfs(i-1,j,grid,visited); // up
dfs(i,j+1,grid,visited);//right
dfs(i,j-1,grid,visited);//left
}
int main(){
int rows,cols;
cin>>rows>>cols;
vector<vector<int>>grid(rows,vector<int>(cols));
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
cin>>grid[i][j];
}
}
vector<vector<bool>>visited(rows,vector<bool>(cols,false));
dfs(0, 0, grid, visited);
return 0;
}
// πΆ 4. Dynamic Programming (Grid)
// π Unique Paths Formula
// dp[i][j]=dp[iβ1][j]+dp[i][jβ1]
// Explanation:
// To reach (i, j):
// From top (i-1, j)
// From left (i, j-1)
// Base Cases:
// dp[0][0] = 1 (starting point)
// dp[i][0] = 1 (only one way down)
// dp[0][j] = 1 (only one way right)
// π· 5. Unique Paths in Grid (DP Implementation)
// π· 7. Time & Space Complexity
// Type Value
// Time O(rows Γ cols)
// Space O(rows Γ cols)
// π· 8. Mathematical Insight (Advanced π₯)
// π This is also a combinatorics problem
// Total moves:
// Right = (cols - 1)
// Down = (rows - 1)
// Total steps:(rows+colsβ2)
// Number of unique paths = C(steps, right) = C(steps, down)β
// C(steps, right) = (steps)! / (right! * down!)
// π· 9. Intuition (Very Important)
// π Each cell = sum of ways to reach it
// π Like building paths step-by-step
// π· π§ Final Understanding
// β Move only right or down
// β Each cell depends on top + left
// β Build solution using DP
// π₯ One-Line Summary
// π Number of ways to reach a cell = ways from top + ways from left
#include<bits/stdc++.h>
using namespace std;
int main(){
int rows,cols;
cin>>rows>>cols;
vector<vector<int>>dp(rows,vector<int>(cols));
for(int i=0;i<rows;i++){
dp[i][0]=1;
}
for(int j=0;j<cols;j++){
dp[0][j]=1;
}
for(int i=1;i<rows;i++){
for(int j=1;j<cols;j++){
dp[i][j]=dp[i-1][j]+dp[i][j-1];
}
}
cout<<"Number of unique paths from (0,0) to ("<<rows-1<<","<<cols-1<<") is: "<<dp[rows-1][cols-1]<<endl;
return 0;
}
// π· 1. What is Backtracking?
// π Backtracking =
// Try a path β if it fails β go back β try another path
// πΆ Simple Idea
// Like solving a maze:
// Try moving
// If stuck β go back
// Try another direction
// π· 2. Your Code Concept
// bool solve(int x, int y, vector<vector<int>>& maze)
// π This function checks:
// Can we reach destination from (x,y)?
// π· 3. Problem Understanding
// π Maze:
// 1 β allowed path
// 0 β blocked
// π Start: (0,0)
// π End: (n-1,n-1)
// π Moves allowed:
// Right β‘οΈ
// Down β¬οΈ
// π· 4. Step-by-Step Logic
// π Step 1: Base Case (Reached Destination)
// if(x == n-1 && y == n-1) return true;
// π If reached end β success β
// π Step 2: Invalid Case
// if(x >= n || y >= n || maze[x][y] == 0) return false;
// π Stop if:
// Out of bounds
// Blocked cell
// π Step 3: Mark Visited
// maze[x][y] = 0;
// π Prevent revisiting same cell
// π Step 4: Explore Paths
// if(solve(x+1, y, maze) || solve(x, y+1, maze))
// π Try:
// Down
// Right
// π Step 5: Return Result
// return true / false;
// πΆ Maze:
// 1 1 0
// 1 1 1
// 0 1 1
// π· Path Found
// π One valid path:
// (0,0) β (1,0) β (1,1) β (1,2) β (2,2)
// π· 7. How Backtracking Works Here
// πΆ Step Flow
// Start at (0,0)
// Try going down
// If blocked β go right
// If dead end β backtrack
// π₯ Important
// π If path fails:
// Function returns false
// Automatically goes back (backtracking)
// π· 8. Why It's Called Backtracking
// π Because:
// Try path
// If fail β return β try next
// π· 9. Time Complexity
// π Worst case: O(2^n^2)
//π Because many paths possible
#include<bits/stdc++.h>
using namespace std;
int n;
bool solve(int x,int y,vector<vector<int>>&maze){
// base case
if(x==n-1 && y==n-1){
return true;
}
//invalid case
if(x>=n || y>=n || maze[x][y]==0){
return false;
}
//mark visited
maze[x][y]=0;
//explore paths
if(solve(x+1,y,maze) || solve(x,y+1,maze)){
return true;
}
return false;
}
int main(){
cin>>n;
vector<vector<int>>maze(n,vector<int>(n));
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>maze[i][j];