标签:
1 #include <iostream> 2 #include <stdio.h> 3 #include <cmath> 4 #include <string> 5 #include <vector> 6 #include <algorithm> 7 #include <memory.h> 8 #include <stack> 9 10 using namespace std; 11 12 const double PI = acos(-1.0); 13 const double eps = 1e-6; 14 const double INF = 1e9 + 7; 15 16 17 18 int Min(int a, int b, int c) 19 { 20 int m = a; 21 if (m > b) 22 m = b; 23 if (m > c) 24 m = c; 25 return m; 26 } 27 28 29 int main() 30 { 31 freopen("data.in", "r", stdin); 32 33 int n, m; 34 35 cin >> n >> m; 36 37 if (n == 0 && m == 0) 38 { 39 cout << 0 << endl; 40 return 0; 41 } 42 vector<int> row(m, 0); 43 vector<vector<int> > matrix(n, row); 44 45 46 for (int i = 0; i < n; i++) 47 { 48 for (int j = 0; j < m; j++) 49 { 50 cin >> matrix[i][j]; 51 } 52 } 53 54 int** dp = new int*[n]; 55 for (int i = 0; i < n; ++i){ 56 dp[i] = new int[m]; 57 memset(dp[i], 0, sizeof(int)*(m)); 58 } 59 60 61 for (int j = 0; j < m; ++j) 62 if (matrix[0][j] == 1) 63 dp[0][j] = 1; 64 65 for (int i = 0; i < n; ++i) 66 if (matrix[i][0] == 1) 67 dp[i][0] = 1; 68 69 70 for (int i = 1; i < n; i++) 71 { 72 for (int j = 1; j < m; j++) 73 { 74 if (matrix[i][j] == 1) 75 { 76 dp[i][j] = Min(dp[i-1][j],dp[i][j-1], dp[i-1][j-1])+1; 77 } 78 } 79 } 80 81 int maxArea = dp[0][0]; 82 for (int i = 0; i < n; i++) 83 { 84 for (int j = 0; j < m; j++) 85 { 86 if (maxArea<dp[i][j]) 87 { 88 maxArea = dp[i][j]; 89 } 90 91 } 92 93 } 94 95 cout <<maxArea << endl; 96 97 for (int i = 0; i < m; ++i) 98 delete[] dp[i]; 99 delete[] dp; 100 101 102 103 104 system("pause"); 105 return 0; 106 }
最大矩形:
1 #include <iostream> 2 #include <stdio.h> 3 #include <cmath> 4 #include <string> 5 #include <vector> 6 #include <algorithm> 7 #include <memory.h> 8 #include <stack> 9 10 using namespace std; 11 12 const double PI = acos(-1.0); 13 const double eps = 1e-6; 14 const double INF = 1e9 + 7; 15 16 17 18 int largestRectangleArea(int* height, int length) { 19 stack<int> stk; 20 int i = 0; 21 int maxArea = 0; 22 while (i < length){ 23 if (stk.empty() || height[stk.top()] <= height[i]){ 24 stk.push(i++); 25 } 26 else { 27 int t = stk.top(); 28 stk.pop(); 29 int area = height[t] * (stk.empty() ? i : i - stk.top() - 1); 30 maxArea = maxArea > area ? maxArea : area; 31 } 32 } 33 return maxArea; 34 } 35 36 37 int main() 38 { 39 freopen("data.in", "r", stdin); 40 41 int n, m; 42 43 cin >> n >> m; 44 45 if (n == 0 && m == 0) 46 { 47 cout << 0<< endl; 48 return 0; 49 } 50 vector<int> row(m,0); 51 vector<vector<int> > matrix(n, row); 52 53 54 for (int i = 0; i < n; i++) 55 { 56 for (int j = 0; j < m; j++) 57 { 58 cin >> matrix[i][j]; 59 } 60 } 61 62 int** dp = new int*[n]; 63 for (int i = 0; i < n; ++i){ 64 dp[i] = new int[m + 1]; 65 memset(dp[i], 0, sizeof(int)*(m + 1)); 66 } 67 68 69 for (int j = 0; j < m; ++j) 70 if (matrix[0][j] == 1) 71 dp[0][j] = 1; 72 73 for (int j = 0; j < m; ++j) 74 for (int i = 1; i < n; ++i) 75 if (matrix[i][j] == 1) 76 dp[i][j] = dp[i - 1][j] + 1; 77 78 int maxarea = 0; 79 for (int i = 0; i < n; ++i){ 80 int tmp = largestRectangleArea(dp[i], m + 1); 81 if (tmp > maxarea) 82 maxarea = tmp; 83 } 84 85 cout << maxarea<< endl; 86 87 for (int i = 0; i < m; ++i) 88 delete[] dp[i]; 89 delete[] dp; 90 91 92 93 94 system("pause"); 95 return 0; 96 }
参考:
http://blog.csdn.net/doc_sgl/article/details/11832965;
标签:
原文地址:http://www.cnblogs.com/lishichao/p/4848665.html