Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. public int countShips(Point p1, Point p2) {
  2.  
  3. if (p1.equals(p2))
  4. return hasShips(p1, p1) ? 1 : 0;
  5.  
  6.  
  7. int nRows = Math.abs(p1.x - p2.x);
  8. int nCols = Math.abs(p1.y - p2.y);
  9. int cnt = 0;
  10.  
  11. if(nRows >= nCols) {
  12. int x = (p1.x + p2.x) / 2;
  13. if (hasShips(p1, new Point(x, p2.y)))
  14. cnt += countShips(p1, new Point(x, p2.y));
  15.  
  16. if (hasShips(new Point(x + 1, p1.y), p2))
  17. cnt += countShips(new Point(x + 1, p1.y), p2);
  18.  
  19. } else {
  20. int y = (p1.y + p2.y) / 2;
  21. if (hasShips(p1, new Point(p2.x, y)))
  22. cnt += countShips(p1, new Point(p2.x, y));
  23.  
  24. if (hasShips(new Point(p1.x, y + 1), p2))
  25. cnt += countShips(new Point(p1.x, y + 1), p2);
  26. }
  27.  
  28. return cnt;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement