果然想要学跟需要学的差距不是一星半点……
人工智能看得头大……
还得是整道题清醒清醒
先整一波改进记录……
522ms(太菜了这个……
class Solution {
int[] arr = {-1,1,0,0};
public int maxAreaOfIsland(int[][] grid) {
boolean[][] mark = new boolean[grid.length][grid[0].length];
for(int i = 0;i < grid.length;i++){
Arrays.fill(mark[i],false);
}
int max = 0;
for(int x = 0; x < grid.length;x++){
for(int y = 0;y < grid[0].length;y++){
if(grid[x][y] == 1){
int m = area(mark,grid,x,y);
max = max>m?max:m;
for(int i = 0;i < grid.length;i++){
Arrays.fill(mark[i],false);
}
}
}
}
return max;
}
public int area(boolean[][] mark,int[][] grid,int x,int y){
if(grid[x][y] == 0){
return 0;
}
mark[x][y] = true;
int max = 0;
for(int i = 0;i < 4;i++){
if(x+arr[i]>=0 && x+arr[i]<grid.length && y+arr[3-i]>=0 && y+arr[3-i]<grid[0].length){
if(!mark[x+arr[i]][y+arr[3-i]]){
max += area(mark,grid,x+arr[i],y+arr[3-i]);
}
}
}
return max+1;
}
}
剪枝之后27ms
剪枝之后快了不少,但是还是很慢
class Solution {
int[] arr = {-1,1,0,0};
public int maxAreaOfIsland(int[][] grid) {
boolean[][] mark = new boolean[grid.length][grid[0].length];
boolean[][] mark1 = new boolean[grid.length][grid[0].length];
for(int i = 0;i < grid.length;i++){
Arrays.fill(mark[i],false);
Arrays.fill(mark1[i],false);
}
int max = 0;
for(int x = 0; x < grid.length;x++){
for(int y = 0;y < grid[0].length;y++){
if(grid[x][y] == 1){
mark1[x][y] = true;
if(judge(grid,mark1,x,y)){
continue;
}
int m = area(mark,grid,x,y);
max = max>m?max:m;
for(int i = 0;i < grid.length;i++){
Arrays.fill(mark[i],false);
}
}
}
}
return max;
}
public int area(boolean[][] mark,int[][] grid,int x,int y){
if(grid[x][y] == 0){
return 0;
}
mark[x][y] = true;
int max = 0;
for(int i = 0;i < 4;i++){
if(x+arr[i]>=0 && x+arr[i]<grid.length && y+arr[3-i]>=0 && y+arr[3-i]<grid[0].length){
if(!mark[x+arr[i]][y+arr[3-i]]){
max += area(mark,grid,x+arr[i],y+arr[3-i]);
}
}
}
return max+1;
}
public boolean judge(int[][] grid,boolean[][] mark1,int x,int y){
for(int i = 0;i < 4;i++){
if(x+arr[i]>=0 && x+arr[i]<grid.length && y+arr[3-i]>=0 && y+arr[3-i]<grid[0].length){
if(mark1[x+arr[i]][y+arr[3-i]]){
return true;//存在被查询过的
}
}
}
return false;//不存在
}
}
然后查看题解hiahiahia~~
沉岛思想
进题解区第一眼看到了这个词,真的是茅塞顿开的感觉,突然就有了思路
class Solution {
int[] arr = {-1,1,0,0};
public int maxAreaOfIsland(int[][] grid) {
int max = 0;
for(int x = 0; x < grid.length;x++){
for(int y = 0;y < grid[0].length;y++){
if(grid[x][y] == 1){
max = Math.max(max,area(grid,x,y));
}
}
}
return max;
}
public int area(int[][] grid,int x,int y){
if(grid[x][y] == 0){
return 0;
}
int max = 0;
grid[x][y] = 0;
for(int i = 0;i < 4;i++){
if(x+arr[i]>=0 && x+arr[i]<grid.length && y+arr[3-i]>=0 && y+arr[3-i]<grid[0].length){
max += area(grid,x+arr[i],y+arr[3-i]);
}
}
return max+1;
}
}
后来又想把全局变量去掉
public class Solution {
public int maxAreaOfIsland(int[][] grid) {
int max = 0;
for(int x = 0; x < grid.length;x++){
for(int y = 0;y < grid[0].length;y++){
if(grid[x][y] == 1){
max = Math.max(max,area(grid,x,y));
}
}
}
return max;
}
public int area(int[][] grid,int x,int y){
if(x<0 || x>=grid.length || y<0 || y>=grid[0].length || grid[x][y] == 0){
return 0;
}
int max = 1;
grid[x][y] = 0;
max += area(grid,x-1,y);
max += area(grid,x+1,y);
max += area(grid,x,y-1);
max += area(grid,x,y+1);
return max;
}
}