题4:边长为1的最大子方阵优化 预处理 对每个位置上为1的元素找右边下边有多少个1 1 1 1 0 1 1 0 1 1 1 0 1 0 1 1 1 3,3 2,4 1,0 0,0 2,2 1,3 0,0 1,3 2,1 1,2 0,0 1,2 0,0 3,1 2,1 1,1 (右,下)//算上自身 [3],[3] 2,[4] [2], 2 1, 3 只需要看这四个数就可以确定一个正方形 //输出右下1个数的矩阵 private static void print(int[][][] rec,int N){ for(int i=0;i<N;i++){ for(int j=0;j<N;j++){ System.out.print(rec[i][j][0]+","+rec[i][j][1]+"\t"); } System.out.println(); } } //全局变量 int[][][]res;//保存1右下个数的数组 //生成右边和下边的1的数量的数组 private static void generateHelpRec(int[][] A){ int N=A.length; res=new int[N][N][2]; //最后一行 int row=N-1; //初始化最下面一层 for(int j=N-1;j>=0;j--){ int value=A[row][j]; if(value==1){ if(j==N-1) {rec[row][j][0]=1;}//右侧连接1的个数,最底层 else {rec[row][j][0]=rec[row][j+1][0]+1;} res[row][j][1]=1; } } row--; for(int i=row;i>=0;i--){ for(int j=N-1;j>=0;j--){ int value=A[i][j]; if(value==1){ if(j==N-1) rec[i][j][0]=-1; else rec[i][j][0]=1=rec[i][j+1][0]+1; res[i][j][1]=res[i][j][1]+1;//向下连接的个数 } } } return } //检查四条边是否正确 private static boolean check(int i,int j,int n){ //左上角那个点往右数的1的数目要≥n //左上角那个点往下数的1的数目要≥n //右上角那个点往下数的1的数目要≥n //左下角那个点往右数的1的数目要≥n if(rec[i][j][0]>=n&&rec[i][j][1]>=n&&rec[i][j+n-1][1]>=n&&rec[i+n-1][j][0]>=n) return true; return false; } private static int solve(int[][] A){ int N=A.length; int n=N; while(n>0){ for(int i=0;i<N;i++){ l3; if(i+n>N)break;//越界 for(int j=0;j<N;j++){ if(j+n>N)break;//越界 //检查四个边 if(check(i,j,n)) return n; } } } } //这种从底层往上求的方式类似于动态规划里的打表法
蓝桥杯算法基础(25)边长为1的最大子方阵优化
湖前一人对影成双2024-03-19 17:18
相关推荐
curemoon41 分钟前
理解都远正态分布中指数项的精度矩阵(协方差逆矩阵)柃歌1 小时前
【UCB CS 61B SP24】Lecture 7 - Lists 4: Arrays and Lists学习笔记柃歌1 小时前
【UCB CS 61B SP24】Lecture 4 - Lists 2: SLLists学习笔记A_one20101 小时前
前端开发常见问题与面试-02SKYDROID云卓小助手2 小时前
无人设备遥控器之如何分享数传篇Lqingyyyy2 小时前
P2865 [USACO06NOV] Roadblocks G 与最短路的路径可重复的严格次短路WHATEVER_LEO3 小时前
【每日论文】Text-guided Sparse Voxel Pruning for Efficient 3D Visual GroundingVacant Seat3 小时前
贪心算法-买卖股票的最佳时机郑州吴彦祖7724 小时前
数据结构——二叉树经典习题讲解lyx1426064 小时前
leetcode 8. 字符串转换整数 (atoi)