https://www.lintcode.com/problem/largest-plus-sign/description
import java.util.Arrays;
public class Solution {
private int result = 0;
/**
* @param N: size of 2D grid
* @param mines: in the given list
* @return: the order of the plus sign
*/
public int orderOfLargestPlusSign(int N, int[][] mines) {
// Write your code here
int[][] grid = new int[N][N];
for (int i = 0; i < grid.length; i++) {
int[] ints = grid[i];
Arrays.fill(ints, 1);
}
for (int i = 0; i < mines.length; i++) {
int[] mine = mines[i];
grid[mine[0]][mine[1]] = 0;
}
for (int i = 0; i < grid.length; i++) {
int[] mine = grid[i];
for (int j = 0; j < grid.length; j++) {
if (mine[j] == 1) {
treeWalk(grid, i, j);
}
}
}
return result;
}
private void treeWalk(int[][] mines, int i, int j) {
int level = 1;
while (true) {
{
int x = j - level;
int y = i;
if (x >= 0 && mines[y][x] == 1) {
} else {
break;
}
}
{
int x = j + level;
int y = i;
if (x < mines[0].length && mines[y][x] == 1) {
} else {
break;
}
}
{
int x = j;
int y = i - level;
if (y >= 0 && mines[y][x] == 1) {
} else {
break;
}
}
{
int x = j;
int y = i + level;
if (y < mines.length && mines[y][x] == 1) {
} else {
break;
}
}
level++;
}
result = Math.max(level, result);
}
}
网友评论