-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTheMatrix.cpp
278 lines (232 loc) · 10.8 KB
/
TheMatrix.cpp
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
#include <string>
#include <vector>
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<stack>
#include<queue>
#include<cmath>
#include<algorithm>
#include<functional>
#include<list>
#include<deque>
#include<bitset>
#include<set>
#include<map>
#include<cstdio>
#include<cstring>
#include<sstream>
#define X first
#define Y second
#define pb push_back
#define pqPair priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >
#define long_long long long
// BEGIN CUT HERE
#define long_long __int64
// END CUT HERE
using namespace std;
typedef long_long ll;
class TheMatrix {
public:
int MaxArea(vector <string> board) {
int dp[123][123],i,j,x,y,re=1;
for(i=0;i<board.size();i++){
for(j=0;j<board[0].size();j++){
memset(dp,0,sizeof(dp));
for(y=i;y<board.size();y++){
for(x=j;x<board[0].size();x++){
if(((x+y-i-j)%2 == (board[y][x]!=board[i][j])) &&(dp[y][x+1]>=0 && dp[y+1][x]>=0)){
dp[y+1][x+1]=(x-j+1)*(y-i+1);
//cout<<"!";
// cout<<x<<y<<i<<j<<endl;
}else{
dp[y+1][x+1]=-1;
}
re=max(re,dp[y+1][x+1]);
// cout<<dp[y+1][x+1]<<" ";
}//cout<<endl;
}//cout<<endl;
}
}
return re;
}
// BEGIN CUT HERE
public:
void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); if ((Case == -1) || (Case == 5)) test_case_5(); if ((Case == -1) || (Case == 6)) test_case_6(); if ((Case == -1) || (Case == 7)) test_case_7(); }
private:
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
void test_case_0() { string Arr0[] = {"1",
"0"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 2; verify_case(0, Arg1, MaxArea(Arg0)); }
void test_case_1() { string Arr0[] = {"0000"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 1; verify_case(1, Arg1, MaxArea(Arg0)); }
void test_case_2() { string Arr0[] = {"01"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 2; verify_case(2, Arg1, MaxArea(Arg0)); }
void test_case_3() { string Arr0[] = {"001",
"000",
"100"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 2; verify_case(3, Arg1, MaxArea(Arg0)); }
void test_case_4() { string Arr0[] = {"0"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 1; verify_case(4, Arg1, MaxArea(Arg0)); }
void test_case_5() { string Arr0[] = {"101",
"010"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 6; verify_case(5, Arg1, MaxArea(Arg0)); }
void test_case_6() { string Arr0[] = {"101",
"011",
"101",
"010"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 8; verify_case(6, Arg1, MaxArea(Arg0)); }
void test_case_7() { string Arr0[] = {"11001110011000110001111001001110110011010110001011",
"10100100010111111011111001011110101111010011100001",
"11101111001110100110010101101100011100101000010001",
"01000010001010101100010011111000100100110111111000",
"10110100000101100000111000100001011101111101010010",
"00111010000011100001110110010011010110010011100100",
"01100001111101001101001101100001111000111001101010",
"11010000000011011010100010000000111011001001100101",
"10100000000100010100100011010100110110110001000001",
"01101010101100001100000110100110100000010100100010",
"11010000001110111111011010011110001101100011100010",
"11101111000000011010011100100001100011111111110111",
"11000001101100100011000110111010011001010100000001",
"00100001111001010000101101100010000001100100001000",
"01001110110111101011010000111111101011000110010111",
"01001010000111111001100000100010101100100101010100",
"11111101001101110011011011011000111001101100011011",
"10000100110111000001110110010000000000111100101101",
"01010011101101101110000011000110011111001111011100",
"01101010011111010000011001111101011010011100001101",
"11011000011000110010101111100000101011011111101100",
"11100001001000110010100011001010101101001010001100",
"11011011001100111101001100111100000101011101101011",
"11110111100100101011100101111101000111001111110111",
"00011001100110111100111100001100101001111100001111",
"10001111100101110111001111100000000011110000100111",
"10101010110110100110010001001010000111100110100011",
"01100110100000001110101001101011001010001101110101",
"10110101110100110110101001100111110000101111100110",
"01011000001001101110100001101001110011001001110001",
"00100101010001100110110101001010010100001011000011",
"00011101100100001010100000000011000010100110011100",
"11001001011000000101111111000000110010001101101110",
"10101010110110010000010011001100110101110100111011",
"01101001010111010001101000100011101001110101000110",
"00110101101110110001110101110010100100110000101101",
"11010101000111010011110011000001101111010011110011",
"10010000010001110011011101001110110010001100011100",
"00111101110001001100101001110100110010100110110000",
"00010011011000101000100001101110111100100000010100",
"01101110001101000001001000001011101010011101011110",
"00000100110011001011101011110011011101100001110111",
"00110011110000011001011100001110101010100110010110",
"00111001010011011111010100000100100000101101110001",
"10101101101110111110000011111011001011100011110001",
"00101110010101111000001010110100001110111011100011",
"01111110010100111010110001111000111101110100111011"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 12; verify_case(7, Arg1, MaxArea(Arg0)); }
// END CUT HERE
};
// BEGIN CUT HERE
int main() {
TheMatrix ___test;
___test.run_test(-1);
}
/*
// PROBLEM STATEMENT
// Have you ever had a dream, that you were so sure was real? What if you were unable to wake from that dream? How would you know the difference between the dream world and the real world?
To answer this complex puzzle, one of the questions that must be answered is to find out whether the world that you live in can be represented by a chess matrix.
Cells of a matrix are called adjacent if they share an edge.
A matrix of zeroes and ones is called a chess matrix if there are no two adjacent cells that share the same value.
Hence, in a chess matrix the zeroes and ones have to alternate in the same way the colors alternate on a chessboard:
You are given a vector <string> board that represents a rectangular grid of cells, with a 0 or a 1 in each cell.
Each character of each element of board will be either '0' or '1'.
In this grid we selected some rectangular subgrid that is a chess matrix.
Return the largest possible area of the selected subgrid.
DEFINITION
Class:TheMatrix
Method:MaxArea
Parameters:vector <string>
Returns:int
Method signature:int MaxArea(vector <string> board)
CONSTRAINTS
-board will contain between 1 and 100 elements, inclusive.
-Each element of the board is a string containing between 1 and 100 characters, inclusive.
-All elements of board will have the same length.
-Each character of each element of board will be either '0' or '1'.
EXAMPLES
0)
{"1",
"0"}
Returns: 2
The entire board is a chess matrix.
1)
{"0000"}
Returns: 1
The largest possible chess matrix here is just a single cell.
2)
{"01"}
Returns: 2
Again, the entire board is a chess matrix.
3)
{"001",
"000",
"100"}
Returns: 2
Each rectangular subgrid is determined by a contiguous range of rows and a contiguous range of columns. The four corners of this grid do not form a valid rectangular subgrid.
4)
{"0"}
Returns: 1
5)
{"101",
"010"}
Returns: 6
6)
{"101",
"011",
"101",
"010"}
Returns: 8
7)
{"11001110011000110001111001001110110011010110001011",
"10100100010111111011111001011110101111010011100001",
"11101111001110100110010101101100011100101000010001",
"01000010001010101100010011111000100100110111111000",
"10110100000101100000111000100001011101111101010010",
"00111010000011100001110110010011010110010011100100",
"01100001111101001101001101100001111000111001101010",
"11010000000011011010100010000000111011001001100101",
"10100000000100010100100011010100110110110001000001",
"01101010101100001100000110100110100000010100100010",
"11010000001110111111011010011110001101100011100010",
"11101111000000011010011100100001100011111111110111",
"11000001101100100011000110111010011001010100000001",
"00100001111001010000101101100010000001100100001000",
"01001110110111101011010000111111101011000110010111",
"01001010000111111001100000100010101100100101010100",
"11111101001101110011011011011000111001101100011011",
"10000100110111000001110110010000000000111100101101",
"01010011101101101110000011000110011111001111011100",
"01101010011111010000011001111101011010011100001101",
"11011000011000110010101111100000101011011111101100",
"11100001001000110010100011001010101101001010001100",
"11011011001100111101001100111100000101011101101011",
"11110111100100101011100101111101000111001111110111",
"00011001100110111100111100001100101001111100001111",
"10001111100101110111001111100000000011110000100111",
"10101010110110100110010001001010000111100110100011",
"01100110100000001110101001101011001010001101110101",
"10110101110100110110101001100111110000101111100110",
"01011000001001101110100001101001110011001001110001",
"00100101010001100110110101001010010100001011000011",
"00011101100100001010100000000011000010100110011100",
"11001001011000000101111111000000110010001101101110",
"10101010110110010000010011001100110101110100111011",
"01101001010111010001101000100011101001110101000110",
"00110101101110110001110101110010100100110000101101",
"11010101000111010011110011000001101111010011110011",
"10010000010001110011011101001110110010001100011100",
"00111101110001001100101001110100110010100110110000",
"00010011011000101000100001101110111100100000010100",
"01101110001101000001001000001011101010011101011110",
"00000100110011001011101011110011011101100001110111",
"00110011110000011001011100001110101010100110010110",
"00111001010011011111010100000100100000101101110001",
"10101101101110111110000011111011001011100011110001",
"00101110010101111000001010110100001110111011100011",
"01111110010100111010110001111000111101110100111011"}
Returns: 12
*/
// END CUT HERE