-
Notifications
You must be signed in to change notification settings - Fork 0
/
18_11.cpp
118 lines (105 loc) · 2.9 KB
/
18_11.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
//
// 18_11.cpp
//
//
// Created by Pengyan Qin on 7/28/15.
//
//
#include <iostream>
#include <fstream>
using namespace std;
// 1 represents black, 0 represents white
// time complexity: O(n)
bool is_valid(int *A, int n, int k){
for(int i = 0; i <= k; i += k){ // only 2 times
for(int j = 0; j <= k; ++j){
if(*(A + i*n + j) == 0 || *(A + j*n + i) == 0 )
return false;
}
}
return true;
}
// brute force
// time complexity: O(n^4)
int maximum_subsquare(int *A, int n){
for(int k = n-1; k > 0; --k){ // the side length of subsquare
for(int i = 0; i < n; ++i){
for(int j = 0; j < n; ++j){ // the start point of subsquare
if((i + k < n) && (j + k < n)){
if(is_valid(A + i*n + j, n, k))
return k+1;
}
else
break;
}
}
}
return -1;
}
// time complexity: O(n^3)
// check square reduced to O(1) by preprocessing
struct ones{
int onesright;
int onesdown;
ones():onesright(0), onesdown(0){}
};
ones* preprocessing(int *A, int n){
ones* M = new ones[(n+1)*(n+1)];
for(int i = n; i >= 0; --i){
for(int j = n ; j >= 0; --j){
if(i == n || j == n || *(A + i*n + j) == 0 ){
*(M + i*n + j) = ones();
}
else{
(M + i*n + j)->onesright = (M + i*n + j + 1)->onesright + 1;
(M + i*n + j)->onesdown = (M + (i + 1)*n + j)->onesdown + 1;
}
}
}
for(int i = 0; i < n; ++i){
for(int j = 0; j < n; ++j){
cout << "(" << (M + i*n + j)->onesright << ", " << (M + i*n + j)->onesdown << ")" << " ";
}
cout << endl;
}
return M;
}
int maximum_subsquare_helper(ones *A, int n){
for(int k = n-1; k >= 0; --k){
for(int i = 0; i < n; ++i){
for(int j = 0; j < n; ++j){ // the start point of subsquare
if((i + k < n) && (j + k < n)){
if((A + i*n + j)->onesdown == k+1 && (A + i*n + j)->onesright == k+1 &&
(A + i*n + j + k)->onesdown == k+1 && (A + (i + k)*n + j)->onesright == k+1)
return k+1;
}
else
break;
}
}
}
return -1;
}
int maximum_subsquare1(int *A, int n){
ones * Ah = preprocessing(A, n);
return maximum_subsquare_helper(Ah, n);
}
int main(){
fstream fin;
string filename = "18_11.in";
fin.open(filename.c_str());
if(!fin.is_open()){
cout << "fail to open " << endl;
exit(1);
}
int n;
fin >> n;
int *A = new int[n*n];
for(int i = 0; i < n; ++i){
for(int j = 0; j < n; ++j){
fin >> *(A + i*n + j);
}
}
cout << maximum_subsquare(A, n) << endl;
cout << maximum_subsquare1(A, n) << endl;
}