-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path2darraysearch.cpp
More file actions
71 lines (55 loc) · 1.51 KB
/
2darraysearch.cpp
File metadata and controls
71 lines (55 loc) · 1.51 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
#include<bits/stdc++.h>
using namespace std;
#define lli long long int
#define MOD 1000000007
#define mat vector<vector<int>>
// uses divide and conquer on a 2d array where
// every row and every column is sorted ascendingly
void dsearch(mat a,int item,int fR , int tR, int fC ,int tC){
int i = fR + (tR-fR)/2;
int j = fC + (tC-fC)/2;
if (a[i][j] == item)
cout<<"Found "<< item << " at "<< i << " " << j<<endl;
else{
if (i != tR || j != fC) dsearch(a,item, fR, i, j, tC);
if (fR == tR && fC + 1 == tC)
if (a[fR][tC] == item)
cout<<"Found "<< item << " at "<< fR << " " << tC <<endl;
if (a[i][j] < item) {
if (i + 1 <= tR)
dsearch(a, item, i + 1, tR, fC, tC);
}
else
{
if (j - 1 >= fC)
dsearch(a,item, fR, tR, fC, j - 1);
}
}
}
void CowboyBebop(){
int m , n ;
cin >> m >> n ;
mat v( m , vector<int>(n));
for (int i = 0; i < m; i++){
for (int j = 0; j < n; j++){
cin >> v[i][j] ;
}
}
int item ;
cin >> item ;
dsearch(v,item,0,m-1,0,n-1) ;
}
using namespace std;
int main(void){
std::ios_base::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int t = 1 ;
// cin >> t;
while(t--){
CowboyBebop();
}
return 0;
}