-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlargestSquare.cpp
78 lines (68 loc) · 1.66 KB
/
largestSquare.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
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
ifstream fin("largestSquare.in");
ofstream fout("largestSquare.out");
int main()
{
int n, m;
fin >> n >> m;
vector<vector<int>> a(n, vector<int>(m));
vector<vector<int>> dp(n, vector<int>(m, 10000));
for (int y = 0; y <= n - 1; ++y)
{
for (int x = 0; x <= m - 1; ++x)
{
fin >> a[y][x];
}
}
for (int i = 0; i <= n - 1; ++i)
{
dp[1][0] = 1;
}
for (int i = 0; i <= m - 1; ++i)
{
dp[0][1] = 1;
}
int largestSide = 1;
for (int y = 1; y <= n - 1; ++y)
{
for (int x = 1; x <= m - 1; ++x)
{
if (a[y][x] == 0)
{
dp[y][x] = 0;
}
else
{
if (a[y - 1][x] == 1)
{
dp[y][x] = min(dp[y][x], dp[y - 1][x] + 1);
}
if (dp[y][x - 1] == 1)
{
dp[y][x] = min(dp[y][x], dp[y][x - 1] + 1);
}
if (dp[y - 1][x - 1] == 1)
{
dp[y][x] = min(dp[y][x], dp[y - 1][x - 1] + 1);
}
if (dp[y][x] == 10000)
{
dp[y][x] = 1;
}
largestSide = max(largestSide, dp[y][x]);
}
}
}
fout << largestSide << '\n';
return 0;
}