[CC150v4] 20.11 Find Subsquare with Black Border - Shuatiblog.com
Imagine you have a square matrix, where each cell is filled with either black or white.
Design an algorithm to find the maximum subsquare such that all four borders are filled with black pixels.
有一个正方形矩阵,里面的每一个小格子要么被涂上黑色要么被涂上白色。 设计算法,找到四条边都是黑色格子的最大子正方形。
暴力法,从左到右,从上到下遍历格子,将它作为子正方形左上角的点。 固定了子正方形左上角的点,我们只需要知道边长,就能把子正方形确定下来。 我们按边长从大到小开始,去检查每一个子正方形的四条边是否都为黑色格子。 如果是,则记下当前最大的边长值。将子正方形左上角的点移动到下一行(即向下移动一格), 进入下一轮循环。https://github.com/wzhishen/cracking-the-coding-interview/blob/master/src/chap18/Q11.java
http://career-preparation.googlecode.com/svn/trunk/programs/src/workspace/CareerCupJava/src/class18_10.java
Similar Question:
HackerRank: Mr K marsh
Mr K has a rectangular land (m x n). There are marshes in the land where the fence cannot hold. Mr K wants you to find the perimeter of the largest rectangular fence that can be built on this land.
Read full article from [CC150v4] 20.11 Find Subsquare with Black Border - Shuatiblog.com
Imagine you have a square matrix, where each cell is filled with either black or white.
Design an algorithm to find the maximum subsquare such that all four borders are filled with black pixels.
有一个正方形矩阵,里面的每一个小格子要么被涂上黑色要么被涂上白色。 设计算法,找到四条边都是黑色格子的最大子正方形。
暴力法,从左到右,从上到下遍历格子,将它作为子正方形左上角的点。 固定了子正方形左上角的点,我们只需要知道边长,就能把子正方形确定下来。 我们按边长从大到小开始,去检查每一个子正方形的四条边是否都为黑色格子。 如果是,则记下当前最大的边长值。将子正方形左上角的点移动到下一行(即向下移动一格), 进入下一轮循环。https://github.com/wzhishen/cracking-the-coding-interview/blob/master/src/chap18/Q11.java
| ||
//brute force: O(n^4) time. | ||
//Preprocessing matrix can make isValid take constant time, | ||
//reducing total runtime to O(n^3). | ||
static Result findLargestSubsquare(int[][] matrix) { | ||
for (int len = matrix.length; len >= 2; --len) { | ||
Result res = findSubsquare(matrix, len); | ||
if (res != null) return res; | ||
} | ||
return null; | ||
} | ||
static Result findSubsquare(int[][] matrix, int length) { | ||
int cnt = matrix.length - length + 1; | ||
for (int i = 0; i < cnt; ++i) { | ||
for (int j = 0; j < cnt; ++j) { | ||
if (isValid(matrix, i, j, length)) { | ||
return new Result(i, j, length); | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
static boolean isValid(int[][] m, int r, int c, int l) { | ||
for (int n = 0; n < l/*XXX*/; ++n) { | ||
//check top and bottom rows | ||
if (m[r][c+n] == 0 || m[r+l-1][c+n] == 0) | ||
return false; | ||
//check left and right columns | ||
if (m[r+n][c] == 0 || m[r+n][c+l-1] == 0) | ||
return false; | ||
} | ||
return true; | ||
}
|
int col; int row; int length; | |
public Result(int r, int c, int l) {row=r;col=c;length=l;} | |
} |
http://career-preparation.googlecode.com/svn/trunk/programs/src/workspace/CareerCupJava/src/class18_10.java
class 18_10_2{ class Square{ int x1; int y1; int x2; int y2; Square(int x1, int y1, int x2, int y2){ this.x1=x1; this.y1=y1; this.x2=x2; this.y2=y2; } } class SquareCell{ int rightZeros; int downZeros; SquareCell(int right, int down){ rightZeros=right; downZeros=down; } } public SquareCell[][] preprocess(int[][] matrix){ SquareCell[][] processed=new SquareCell[matrix.length][matrix.length]; for(int i=0; i<matrix.length; i++) processed[i]=new SquareCell[matrix.length]; for(int i=matrix.length-1; i>=0; i--){ for(int j=matrix.length-1; j>=0; j--){ if(matrix[i][j]==1){ processed[i][j].rightZeros=0; processed[i][j].downZeros=0; }else{ if(j==matrix.length-1) processed[i][j].rightZeros=1; else processed[i][j].rightZeros=processed[i][j+1].rightZeros+1; if(i==matrix.length-1) processed[i][j].downZeros=1; else processed[i+1][j].downZeros=processed[i+1][j].downZeros+1; } } } return processed; } public Square findMaxSquare(int[][] matrix){ SquareCell[][] processed=preprocess(matrix); for(int i=matrix.length; i>=1; i--){ Square sq=findSquareWithSize(processed, i); if(sq!=null) return sq; } return null; } public Square findSquareWithSize(SquareCell[][] processed, int size){ for(int i=0; i<=processed.length-size+1; i++){ for(int j=0; j<=processed.length-size+1; j++){ Square sq=new Square(i, j, i+size-1, j+size-1); if(isSquare(processed, sq)) return sq; } } return null; } boolean isSquare(SquareCell[][] processed, Square sq){ int length=sq.x2-sq.x1+1; if(processed[sq.x1][sq.y1].rightZeros>=length&&processed[sq.x2][sq.y1].rightZeros>=length&&processed[sq.x1][sq.y2].downZeros>=length&&processed[sq.x2][sq.y2].downZeros>=length) return true; return false; } }
Similar Question:
HackerRank: Mr K marsh
Mr K has a rectangular land (m x n). There are marshes in the land where the fence cannot hold. Mr K wants you to find the perimeter of the largest rectangular fence that can be built on this land.
Read full article from [CC150v4] 20.11 Find Subsquare with Black Border - Shuatiblog.com