Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public int countShips(Point p1, Point p2) {
- if (p1.equals(p2))
- return hasShips(p1, p1) ? 1 : 0;
- int nRows = Math.abs(p1.x - p2.x);
- int nCols = Math.abs(p1.y - p2.y);
- int cnt = 0;
- if(nRows >= nCols) {
- int x = (p1.x + p2.x) / 2;
- if (hasShips(p1, new Point(x, p2.y)))
- cnt += countShips(p1, new Point(x, p2.y));
- if (hasShips(new Point(x + 1, p1.y), p2))
- cnt += countShips(new Point(x + 1, p1.y), p2);
- } else {
- int y = (p1.y + p2.y) / 2;
- if (hasShips(p1, new Point(p2.x, y)))
- cnt += countShips(p1, new Point(p2.x, y));
- if (hasShips(new Point(p1.x, y + 1), p2))
- cnt += countShips(new Point(p1.x, y + 1), p2);
- }
- return cnt;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement