文章目录
- 题意
- 思路
- 代码
题意
题目链接
找到最长的边都是0的正方形,如果长度想等,尽可能起点小
思路
- 暴力+剪枝
- dp ,预处理每个点的上下左右最长长度,再去枚举长度去转移
代码
// 力大飞砖
class Solution {
public:vector<int> findSquare(vector<vector<int>>& matrix) {int x_ans = 0;int y_ans = 0;int length_ans = -1;for (int x = 0; x < matrix.size(); x++){for (int y = 0; y < matrix[x].size(); y++){if (matrix[x][y] == 1)continue;int max_tmp = min(matrix.size() - x, matrix[y].size() - y);if (max_tmp <= length_ans)continue;for (int z = max_tmp; z > 0; z--){if (z <= length_ans)break;if (judge_invalid(matrix, x, y, z) && z > length_ans){length_ans = z;x_ans = x;y_ans = y;}}}}return ~length_ans ? vector<int> {x_ans, y_ans, length_ans} : vector<int> {};}bool judge_invalid(vector<vector<int>>& matrix, int x, int y, int length){for (int i = 0; i < length; i++)if (matrix[x][y + i] || matrix[x + i][y] || matrix[x + i][y + length - 1] || matrix[x + length -1][y + i])return false;return true;}
};