-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaximal_square.cpp
More file actions
132 lines (111 loc) · 4.33 KB
/
maximal_square.cpp
File metadata and controls
132 lines (111 loc) · 4.33 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
#include<bits/stdc++.h>
using namespace std;
class Solution {
public:
bool isValid(int i, int j, int m, int n){
if(i>=0 && i<n && j>=0 && j<m){
return true;
}
return false;
}
int bfs(queue<pair<int,int>> q, vector<vector<char>>&matrix){
int size = 1;
int elements = 3;
vector<pair<int,int>> directions = {{0,1}, {1,1},{1,0}};
while(!q.empty()){
vector<pair<int,int>> remove;
if(size == 1){
pair<int,int> curr = q.front();
q.pop();
for(int i=0; i<directions.size(); i++){
int tempx = curr.first+directions[i].first;
int tempy = curr.second+directions[i].second;
if(isValid(tempx,tempy, matrix[0].size(), matrix.size()) && matrix[tempx][tempy] == '1'){
q.push({tempx, tempy});
remove.push_back({tempx,tempy});
}
else{
return size*size;
}
}
}
else{
int n = 1;
while(n <= elements){
if(q.empty()){
return size*size;
}
if(n < elements/2+1){
pair<int,int> curr = q.front();
q.pop();
int tempx = curr.first+directions[0].first;
int tempy = curr.second+directions[0].second;
if(isValid(tempx,tempy, matrix[0].size(), matrix.size()) && matrix[tempx][tempy] == '1'){
q.push({tempx, tempy});
remove.push_back({tempx,tempy});
}
else{
return size*size;
}
}
else if(n == elements/2+1){
pair<int,int> curr = q.front();
q.pop();
for(int i=0; i<directions.size(); i++){
int tempx = curr.first+directions[i].first;
int tempy = curr.second+directions[i].second;
if(isValid(tempx,tempy, matrix[0].size(), matrix.size()) && matrix[tempx][tempy] == '1'){
q.push({tempx, tempy});
remove.push_back({tempx,tempy});
}
else{
return size*size;
}
}
}
else{
pair<int,int> curr = q.front();
q.pop();
int tempx = curr.first+directions[2].first;
int tempy = curr.second+directions[2].second;
if(isValid(tempx,tempy, matrix[0].size(), matrix.size()) && matrix[tempx][tempy] == '1'){
q.push({tempx, tempy});
remove.push_back({tempx,tempy});
}
else{
return size*size;
}
}
n++;
}
}
for(auto i:remove){
matrix[i.first][i.second] = '0';
}
size++;
elements+=2;
}
cout<<size<<endl;
return size*size;
}
int maximalSquare(vector<vector<char>>& matrix) {
int ans = 0;
for(int i=0; i<matrix.size(); i++){
for(int j=0; j<matrix[i].size();j++){
if(matrix[i][j] == '1'){
queue<pair<int,int>> q;
q.push({i,j});
ans = max(ans, bfs(q,matrix));
}
}
}
cout<<ans<<endl;
return ans;
}
};
int main(){
Solution s;
vector<vector<char>> matrix = {{'1','0','1','0','0'},{'1','0','1','1','1'},{'1','1','1','1','1'},{'1','0','0','1','0'}};
cout<<s.maximalSquare(matrix)<<endl;
return 0;
}