/**
600 · Smallest Rectangle Enclosing Black Pixels
Algorithms
Hard
Accepted Rate
38%
DescriptionSolutionNotesDiscussLeaderboard
Description
An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black pixels are connected, i.e., there is only one black region. Pixels are connected horizontally and vertically. Given the location (x, y) of one of the black pixels, return the area of the smallest (axis-aligned) rectangle that encloses all black pixels.
Example
Example 1:
Input:[“0010”,“0110”,“0100”],x=0,y=2
Output:6
Explanation:
The upper left coordinate of the matrix is (0,1), and the lower right coordinate is (2,2).
Example 2:
Input:[“1110”,“1100”,“0000”,“0000”], x = 0, y = 1
Output:6
Explanation:
The upper left coordinate of the matrix is (0, 0), and the lower right coordinate is (1,2).
Tags
Company
Google
https://blog.csdn.net/roufoo/article/details/103387086
https://www.lintcode.com/problem/600/
*/
//WA, 没找到错误的地方,希望懂得的大佬指点下
import "fmt"
/**
* @param image: a binary matrix with '0' and '1'
* @param x: the location of one of the black pixels
* @param y: the location of one of the black pixels
* @return: an integer
*/
func minArea(image [][]byte, x int, y int) int {
// write your code here
var m int = len(image)
if m == 0 {
return 0
}
var n int = len(image[0])
var image2 [][]byte = image
var image3 [][]byte = image
for i := 1; i < m; i++ {
for j := 0; j < n; j++ {
if image2[i-1][j] == '1' {
image2[i][j] = '1'
}
}
}
for i := 0; i < m; i++ {
for j := 1; j < n; j++ {
if image3[i][j-1] == '1' {
image3[i][j] = '1'
}
}
}
var widthX, widthY int = 0, 0
for i := 0; i < n; i++ {
if image2[m-1][i] == '1' {
widthX += 1
}
}
for i := 0; i < m; i++ {
if image3[i][n-1] == '1' {
widthY += 1
}
}
return widthX * widthY
}
func main() {
// ["0010","0110","0100"] 0 2
var b1 [][]byte = [][]byte{[]byte("0010"), []byte("0110"), []byte("0100")}
var r1 int = minArea(b1, 0, 2)
fmt.Println(r1)
}