作者:章华燕
编辑:张 欢
燕哥会在后期公布最佳答案,敬请期待!!!!
答案一
选手邮箱:[email protected]
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() 去掉无效点
* check() 检查是否可以组成矩形
* 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;
}
}
答案二
选手邮箱:[email protected]
答案三
选手邮箱:[email protected]
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
vector<vector<int> >shu;
struct Node{
int x,y;
Node(){
x=0;//x轴到本位置连续1的个数
y=0;//y轴到本位置连续1的个数
}
}ansPoit1,ansPoit2;
vector<vector<Node> >info1,info2;
int main(){
int a,b;
cin>>a>>b;
shu.resize(a);
info1.resize(a);
info2.resize(a);
for (auto &p: shu) {
p.resize(b);
}
for (auto &p: info1) {
p.resize(b);
}
for (auto &p: info2) {
p.resize(b);
}
int maxpoit=-1;
for (int i=0; i<a; i++) {
for (int j=0; j<b; j++) {
int d;
scanf("%d",&d);
shu[i][j]=d;
if (j==0) {
if (d==0) {
info1[i][j].x=0;
}else{
info1[i][j].x=1;
}
}else{
if (d==0) {
info1[i][j].x=0;
}else{
info1[i][j].x=info1[i][j-1].x+1;
}
}
//纵向
if (i==0) {
if (d==0) {
info1[i][j].y=0;
}else{
info1[i][j].y=1;
}
}else{
if (d==0) {
info1[i][j].y=0;
}else{
info1[i][j].y=info1[i-1][j].y+1;
}
}
if (info1[i][j].x+info1[i][j].y>maxpoit) {
maxpoit = info1[i][j].x+info1[i][j].y;
ansPoit1.x=i;
ansPoit1.y=j;
}
}
}
maxpoit=-1;
for (int i=a-1; i>=0; i--) {
for (int j=b-1; j>=0; j--) {
int d =shu[i][j];
if (j==b-1) {
if (d==0) {
info2[i][j].x=0;
}else{
info2[i][j].x=1;
}
}else{
if (d==0) {
info2[i][j].x=0;
}else{
info2[i][j].x=info2[i][j+1].x+1;
}
}
//纵向
if (i==a-1) {
if (d==0) {
info2[i][j].y=0;
}else{
info2[i][j].y=1;
}
}else{
if (d==0) {
info2[i][j].y=0;
}else{
info2[i][j].y=info2[i+1][j].y+1;
}
}
if (info2[i][j].x+info2[i][j].y>maxpoit) {
maxpoit = info2[i][j].x+info2[i][j].y;
ansPoit2.x=i;
ansPoit2.y=j;
}
}
}