Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6607537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:30:50+00:00 2026-05-25T19:30:50+00:00

I am trying to validate battleship field with these rules: Ships don’t touch with

  • 0

I am trying to validate battleship field with these rules:

  • Ships don’t touch with sides or corners;
  • Ships are straight;
  • There are 1×4-deck ship, 2×3-deck, 3×2-deck, 4×1-deck ships.

The field is represented as byte[10][10] array.
What algorithm could I use to accomplish this?
The language I use is Java, but any language is good.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:30:51+00:00Added an answer on May 25, 2026 at 7:30 pm

    A quick check for validity: 1×4-deck ship, 2×3-deck, 3×2-deck, 4×1-deck ships must occupy exactly 1*4 + 2*3 + 3*2 + 4*1 = 20 cells. So if your field does not contain 20 cells, it is invalid (either ships overlap, or there aren’t enough ships)

    Now, you need to verify that you have exactly the correct number of each type of ship, and that ships do not touch. You can do this through connected component analysis. The simple two-pass algorithm will do here (there’s pseudo-code and examples for you in the link). This will give you the size, location and shape of each “blob” in your field.

    From there, you just need to iterate over each blob and check that it’s either a vertical or horizontal line. That’s simple — just count the blob’s width (difference between maximum and minimum column values) and height (difference between maximum and minimum row values). One of these must be equal to 1. If not, then two ships were touching, and the field is invalid.

    Finally, check that you have the correct number of each ship type. If you don’t, then the field is invalid. If you do, the field is valid, and you’re done.

    EDIT

    Ships can also touch end-to-end, but this will reduce the overall number of ships (increase the number of ships of a certain type) and thus fail the last test.

    EDIT 2

    Corrected to use the correct ship specifications.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to validate a input field with the JQuery validation plugin. The numeric
I am trying to validate a text field for valid date (MM/DD/YYYY) I have
I am trying to validate a contact number field, with that I am not
I'm trying to validate an input field and store it using AJAX My JavaScript
im trying to validate my form, but for some reason the SSN field wont
Trying to validate the email field in a rails devise app with a word
Im trying to validate a form at the point of clicking submit. There are
I am trying to validate some groups of checkboxes using Jq but I don't
I am trying to validate username field in my form using jquery validation plugin.
When trying to validate my site, I get the following error: Line 188, column

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.