/** * // 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(Sea sea, vector<int> topRight, vector<int> bottomLeft){ return helper(sea, topRight[0], topRight[1], bottomLeft[0], bottomLeft[1]); } inthelper(Sea& sea, int top, int right, int bottom, int left){ int h = top-bottom; int w = right-left; if(!sea.hasShips({top, right},{bottom, left})) return0; if(h == 0 && w == 0) return1; if(h > w){ int a = helper(sea, top, right, bottom+h/2+1, left); int b = helper(sea, bottom+h/2, right, bottom, left); return a + b; } else{ int a = helper(sea, top, right, bottom, left+w/2+1); int b = helper(sea, top, left+w/2, bottom, left); return a + b; } } };