在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;}}


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 原油停盘了以后怎么办 2岁宝宝不吃蔬菜怎么办 事情想多了头疼怎么办 生气引起的肝疼怎么办 饿又懒得买吃的怎么办 四岁宝宝缺铁怎么办 宝宝吃母乳缺铁怎么办 九个月宝宝缺铁怎么办 2岁宝宝走路不稳怎么办 两岁宝宝爱打人怎么办 2岁宝宝脾气暴躁怎么办 二岁宝宝有口气怎么办 bl良品数码欺骗怎么办 喝酒喝的胃疼怎么办 喝酒喝多了胃疼怎么办 喝白酒喝的难受怎么办 吃饭后恶心想吐怎么办 喝奶茶恶心想吐怎么办 怀孕了一直想吐怎么办 肝不好一直想吐怎么办 胃老是恶心想吐怎么办 胃疼拉肚子拉水怎么办 半夜胃疼呕吐怎么办啊, 孕妇胃疼怎么办5个月 孕早期饿的胃疼怎么办 怀孕7个月胃痛怎么办 怀孕了胃疼的厉害怎么办 孕晚期老是胃疼怎么办 孕晚期半夜胃疼怎么办 吃多了反胃想吐怎么办 抽烟胃恶心想吐怎么办 孕妇7个月头疼怎么办 我的世界狗死了怎么办 户户通搜不到台怎么办 两车相撞我全责怎么办 我全责自己的车怎么办 没有我你怎么办 酷我 仿古砖上的水泥怎么办 异界气息的ss怎么办 前夫威胁我我该怎么办 怀孕了不确定孩子是谁的怎么办