# """# This is Sea's API interface.# You should not implement it, or speculate about its implementation# """# class Sea:# def hasShips(self, topRight: 'Point', bottomLeft: 'Point') -> bool:## class Point:# def __init__(self, x: int, y: int):# self.x = x# self.y = yclassSolution:defcountShips(self,sea:"Sea",topRight:"Point",bottomLeft:"Point")->int:defdfs(topRight,bottomLeft):x1,y1=bottomLeft.x,bottomLeft.yx2,y2=topRight.x,topRight.yifx1>x2ory1>y2:return0ifnotsea.hasShips(topRight,bottomLeft):return0ifx1==x2andy1==y2:return1midx=(x1+x2)>>1midy=(y1+y2)>>1a=dfs(topRight,Point(midx+1,midy+1))b=dfs(Point(midx,y2),Point(x1,midy+1))c=dfs(Point(midx,midy),bottomLeft)d=dfs(Point(x2,midy),Point(midx+1,y1))returna+b+c+dreturndfs(topRight,bottomLeft)
/** * // This is Sea's API interface. * // You should not implement it, or speculate about its implementation * class Sea { * public boolean hasShips(int[] topRight, int[] bottomLeft); * } */classSolution{publicintcountShips(Seasea,int[]topRight,int[]bottomLeft){intx1=bottomLeft[0],y1=bottomLeft[1];intx2=topRight[0],y2=topRight[1];if(x1>x2||y1>y2){return0;}if(!sea.hasShips(topRight,bottomLeft)){return0;}if(x1==x2&&y1==y2){return1;}intmidx=(x1+x2)>>1;intmidy=(y1+y2)>>1;inta=countShips(sea,topRight,newint[]{midx+1,midy+1});intb=countShips(sea,newint[]{midx,y2},newint[]{x1,midy+1});intc=countShips(sea,newint[]{midx,midy},bottomLeft);intd=countShips(sea,newint[]{x2,midy},newint[]{midx+1,y1});returna+b+c+d;}}
/** * // This is Sea's API interface. * // You should not implement it, or speculate about its implementation * class Sea { * public: * bool hasShips(vector<int> topRight, vector<int> bottomLeft); * }; */classSolution{public:intcountShips(Seasea,vector<int>topRight,vector<int>bottomLeft){intx1=bottomLeft[0],y1=bottomLeft[1];intx2=topRight[0],y2=topRight[1];if(x1>x2||y1>y2){return0;}if(!sea.hasShips(topRight,bottomLeft)){return0;}if(x1==x2&&y1==y2){return1;}intmidx=(x1+x2)>>1;intmidy=(y1+y2)>>1;inta=countShips(sea,topRight,{midx+1,midy+1});intb=countShips(sea,{midx,y2},{x1,midy+1});intc=countShips(sea,{midx,midy},bottomLeft);intd=countShips(sea,{x2,midy},{midx+1,y1});returna+b+c+d;}};
/** * // This is Sea's API interface. * // You should not implement it, or speculate about its implementation * type Sea struct { * func hasShips(topRight, bottomLeft []int) bool {} * } */funccountShips(seaSea,topRight,bottomLeft[]int)int{x1,y1:=bottomLeft[0],bottomLeft[1]x2,y2:=topRight[0],topRight[1]ifx1>x2||y1>y2{return0}if!sea.hasShips(topRight,bottomLeft){return0}ifx1==x2&&y1==y2{return1}midx:=(x1+x2)>>1midy:=(y1+y2)>>1a:=countShips(sea,topRight,[]int{midx+1,midy+1})b:=countShips(sea,[]int{midx,y2},[]int{x1,midy+1})c:=countShips(sea,[]int{midx,midy},bottomLeft)d:=countShips(sea,[]int{x2,midy},[]int{midx+1,y1})returna+b+c+d}
1 2 3 4 5 6 7 8 910111213141516171819202122232425
/** * // This is the Sea's API interface. * // You should not implement it, or speculate about its implementation * class Sea { * hasShips(topRight: number[], bottomLeft: number[]): boolean {} * } */functioncountShips(sea:Sea,topRight:number[],bottomLeft:number[]):number{const[x1,y1]=bottomLeft;const[x2,y2]=topRight;if(x1>x2||y1>y2||!sea.hasShips(topRight,bottomLeft)){return0;}if(x1===x2&&y1===y2){return1;}constmidx=(x1+x2)>>1;constmidy=(y1+y2)>>1;consta=countShips(sea,topRight,[midx+1,midy+1]);constb=countShips(sea,[midx,y2],[x1,midy+1]);constc=countShips(sea,[midx,midy],bottomLeft);constd=countShips(sea,[x2,midy],[midx+1,y1]);returna+b+c+d;}