在01矩阵中找最大1矩形
来源:互联网 发布:网络推广专员工作内容 编辑:程序博客网 时间:2024/05/16 19:50
题目:
给定一个01矩阵,求该矩阵内部的满足以下条件的最大的矩形的左上角所在的行和列,以及该矩形的长和宽。该矩形满足边上全为1,内部可0可1。(假定所有输入有且仅有一个矩形满足条件)
package Test;import java.util.ArrayList;import java.util.Iterator;import java.util.List;class Position {public int x = 0;public int y = 0;public int w = 0;public int h = 0;public int a = 0;public Position(int x, int y) {this.x = x;this.y = y;}public String toString() {return "(" + x + ", " + y + ")" + "(" + w + ", " + h + ") " + a + "\n";}}/** * 思路:先优化矩阵,去掉无效点;找左上点;求每个左上点可以形成的最大矩形面积;在结果集中找出最大面积的位置信息 * clean() 去掉无效点 * travel() 遍历矩阵,找出左上点 * checkAll() 求每个左上点可以形成的最大矩形 * findMax() 在可以组成矩形的结果中找最大面积 * @author w18666 * */public class Solution {public static void main(String[] args) {int[][] Mat = { {0, 0, 1, 1, 0, 0, 0, 0, 1},{0, 1, 1, 1, 1, 1, 1, 1, 1},{0, 1, 1, 0, 1, 0, 1, 0, 1},{1, 0, 1, 0, 0, 0, 0, 0, 1},{0, 0, 1, 1, 1, 1, 1, 1, 1},{1, 0, 0, 0, 1, 0, 1, 0, 0},};Mat = clean(Mat);//printArr(Mat);List<Position> list = checkAll(travel(Mat), Mat);//System.out.println(list);Position p = findMax(list);System.out.println((p.x + 1) + " " + (p.y + 1) + " " + (p.h + 1) + " " + (p.w + 1));}public static int[][] clean(int[][] Mat) {int m = Mat.length;//m行int n = Mat[0].length;//n列for (int i = 0; i < m; i++) {for (int j = 0; j < n; j++) {if (0 != Mat[i][j]) {int s = 0;if (i > 0 && Mat[i - 1][j] == 1) s += 1;//上if (i < m - 1 && Mat[i + 1][j] == 1) s += 1;//下if (j > 0 && Mat[i][j - 1] ==1 ) s += 1;//左if (j < n - 1 && Mat[i][j + 1] == 1) s += 1;//右if (s <= 1) Mat[i][j] = 0;}}}return Mat;}public static List<Position> travel(int[][] Mat) {int m = Mat.length;//m行int n = Mat[0].length;//n列List<Position> list = new ArrayList<Position>();for (int i = 0; i < m - 1; i++) {for (int j = 0; j < n - 1; j++) {if (0 != Mat[i][j] && 1 == Mat[i + 1][j] && 1 == Mat[i][j + 1]) {//Position p = new Position(i, j);int x = i, y = j, w = 0, h = 0;while (x < m - 1 && Mat[++x][j] == 1) ++h;while (y < n - 1 && Mat[i][++y] == 1) ++w;p.h = h;p.w = w;list.add(p);}}}return list;}public static List<Position> checkAll(List<Position> list, int[][] Mat) {Iterator<Position> it = list.iterator();while (it.hasNext()) {Position p = it.next();int hh = p.h, ww = p.w;for (int i = 0; i < p.h; i++) {p.h -= i; for (int j = 0; j < p.w ; j++) { p.w -= j; if (check(p, Mat) && p.h * p.w > p.a) {p.a = p.h * p.w;hh = p.h;ww = p.w; } }}p.h = hh;p.w = ww;}return list;}public static boolean check(Position p, int[][] Mat) {int m = Mat.length;//m行int n = Mat[0].length;//n列int i = p.x, j = p.y;while (i < m && Mat[i][p.y + p.w] == 1) {//右边i++;}while (j < n && Mat[p.x + p.h][j] == 1) {//下边j++;}if (--j == p.h || --i == p.w) {return false;}return true;}public static void printArr(int[][] Mat) {int m = Mat.length;//m行int n = Mat[0].length;//n列for (int i = 0; i < m; i++) {for (int j = 0; j < n; j++) {System.out.print(Mat[i][j] + " ");}System.out.println("\n");}}public static Position findMax(List<Position> list) {if (list.isEmpty()) return null;Position max = list.get(0);if (list.size() == 1) return max;Iterator<Position> it = list.iterator();while (it.hasNext()) {Position p = it.next();if (p.a > max.a) {max = p;}}return max;}}
阅读全文
0 0
- 在01矩阵中找最大1矩形
- HRBUST 1786 最大停车场 (01矩阵找最大的矩形)
- 柱状图中找最大矩形 & 矩阵中找最大的仅含相同值的矩形区域
- Google面试题:在柱状图中找最大的矩形
- Arithmetic problem | 找二维矩阵权值为1的最大矩形面积
- [LeetCode] 01矩阵中最大矩形 Maximal Rectangle
- 算法题:直方图和0-1矩阵中最大矩形
- 在柱状图中找最大矩形——O(n)时间复杂度java实现
- 经典面试题--寻找01矩阵中最大的1矩形(POJ 3494)
- 【难】【DP】计算bool矩阵中,仅包含1的最大矩形和最大正方形
- hdu1510(矩阵找矩形)
- DUT1033: MATRIX(2013市赛,找矩阵中最大子矩阵)
- 数据结构作业5-1 在矩阵中找特定元素
- 找出矩阵中的最大矩形
- opencv 在图片中找对应矩形(形状分析)
- Matlab找矩阵中最大最小值的位置
- hdu1505 City Game二维01矩阵求最大矩形周长
- 【最大矩阵和】最大加权矩形 rqnoj106
- Ubuntu更改默认python版本的两种方法
- 第五节 hive的安装
- 特质的继承层级
- 判断GPS省市/区县非API调用方法
- 2017 ACM-ICPC 亚洲区(西安赛区)网络赛
- 在01矩阵中找最大1矩形
- css基础(一些标签的默认值)
- Android彻底组件化方案实践
- 5、C#的变量的命名规范和使用
- Java中如何正确的终止一个线程
- myBatis基础配置入门
- 战舰世界酷炫涂装
- 51nod 1279 扔盘子(单调栈)
- 第六节 hiveshell的常用命令