https://leetcode.com/problems/largest-plus-sign/description/
题目大意:问你一个01组成的矩阵里,十字形组成的1最大长度是多少。
解题思路:n^3的方法,暴力求解。
class Solution {
private int[] dx={1,-1,0,0};
private int[] dy={0,0,1,-1};
public int orderOfLargestPlusSign(int n, int[][] mines) {
int[][] a = new int[n][n];
for(int i=0;i<n;i++) Arrays.fill(a[i],1);
for(int i=0;i<mines.length;i++)
{
int x = mines[i][0],y=mines[i][1];
a[x][y] = 0;
}
int res =0;
System.out.println(n);
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
int cnt =0;
if(a[i][j]!=0)
{
cnt=1;
int x = i,y= j;
boolean flag=true;
// System.out.println(x+" "+y);
int step=1;
while(flag)
{
for(int k=0;k<4;k++)
{
int tx = x+dx[k]*step;
int ty = y+dy[k]*step;
if(tx<0 || ty<0 || tx>=n || ty>=n
|| a[tx][ty]==0) { flag=false; break; }
}
if(flag==true) { cnt++; step++; }
}
}
res =Math.max(res,cnt);
}
}
return res;
}
}