# Untitled

a guest
Jan 8th, 2021
26
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. # Battleships Board Validation
2.
3. Do you know the game Battleship? Well, I want to play with my little brother, but before we can begin we need to set up our ships on the board. This is your input.
4.
5. Now, we need to check that the ships that we set up are valid. This is where you come in to help. Your task is to write a program or function which checks whether the given 2d array (your board/input) is a valid board or not.
6.
7. The input will be a 2d array, where `1` represents part of a ship and `0` represents ocean.
8.
9. **The rules:**
10.
11. - There must be:
12. - One battleship (size 4)
13. - Two cruisers (size 3)
14. - Three destroyers (size 2)
15. - Four submarines (size 1)
16. - Any additional ships are not allowed, and neither are missing ships
17. - Each ship must be either vertical or horizontal (aside from submarines, which are a single grid space)
18. - The ships cannot overlap, but may be adjacent
19.
20. Here are some more test cases that your code must check and return the correct answer for ([truthy/falsey](https://codegolf.meta.stackexchange.com/a/2194/52210) whether the input-board is valid or not):
21.
22. Truthy test cases:
23.
24. {1,1,1,1,0,0,0,0,0,0},
25. {1,1,1,1,0,0,0,0,0,0},
26. {1,1,1,1,0,0,0,0,0,0},
27. {1,1,1,1,0,0,0,0,0,0},
28. {1,1,1,1,0,0,0,0,0,0},
29. {0,0,0,0,0,0,0,0,0,0},
30. {0,0,0,0,0,0,0,0,0,0},
31. {0,0,0,0,0,0,0,0,0,0},
32. {0,0,0,0,0,0,0,0,0,0},
33. {0,0,0,0,0,0,0,0,0,0}
34.
35. {1,1,1,0,0,0,0,0,0,0},
36. {1,1,0,0,0,0,0,0,1,0},
37. {1,1,0,0,1,1,1,0,1,0},
38. {1,0,0,0,0,0,0,0,0,0},
39. {1,0,0,0,0,0,0,0,1,0},
40. {0,0,0,0,1,1,1,0,0,0},
41. {0,0,0,0,0,0,0,0,1,0},
42. {0,0,0,0,0,0,0,0,0,0},
43. {0,0,0,0,0,0,0,1,0,0},
44. {0,0,0,0,0,0,0,0,0,0}
45.
46. {1,1,1,0,0,0,0,0,0,0},
47. {1,1,0,0,0,0,0,0,1,0},
48. {1,1,0,0,1,1,1,0,1,0},
49. {1,0,0,0,0,0,0,0,0,0},
50. {1,0,0,0,0,0,0,0,1,0},
51. {0,0,0,0,1,1,1,0,0,0},
52. {0,0,0,0,0,0,0,0,1,0},
53. {0,0,0,0,0,0,0,0,0,0},
54. {0,0,0,0,0,0,0,1,0,0},
55. {0,0,0,0,0,0,0,0,0,0}
56.
57. {0,1,0,0,0,0,0,0,0,0},
58. {1,1,1,1,0,0,0,0,0,0},
59. {1,0,1,1,1,0,0,0,0,0},
60. {1,1,0,0,0,0,0,0,0,0},
61. {1,1,0,0,0,0,0,0,0,0},
62. {0,1,0,0,0,0,0,0,1,0},
63. {0,0,1,0,0,0,0,0,1,0},
64. {0,0,0,0,0,0,0,0,0,0},
65. {0,0,0,0,1,0,0,0,0,0},
66. {0,0,0,0,1,0,0,0,0,0}
67.
68. Falsey test cases:
69.
70. // contains five instead of four submarines
71. {1,0,0,0,0,1,1,0,0,0},
72. {1,0,1,0,0,0,0,0,1,0},
73. {1,0,1,0,1,1,1,0,1,0},
74. {1,0,0,0,0,0,0,0,0,0},
75. {0,0,0,0,0,0,0,0,1,0},
76. {0,0,0,0,1,1,1,0,0,0},
77. {0,1,0,0,0,0,0,0,1,0},
78. {0,0,0,1,0,0,0,0,0,0},
79. {0,0,0,0,0,0,0,1,0,0},
80. {0,0,0,0,0,0,0,0,0,0}
81.
82. // battleship is missing
83. {0,0,0,0,0,1,1,0,0,0},
84. {0,0,1,0,0,0,0,0,1,0},
85. {0,0,1,0,1,1,1,0,1,0},
86. {0,0,0,0,0,0,0,0,0,0},
87. {0,0,0,0,0,0,0,0,1,0},
88. {0,0,0,0,1,1,1,0,0,0},
89. {0,0,0,0,0,0,0,0,1,0},
90. {0,0,0,1,0,0,0,0,0,0},
91. {0,0,0,0,0,0,0,1,0,0},
92. {0,0,0,0,0,0,0,0,0,0}
93.
94. // multiple ships are wrong
95. {0,1,1,1,0,0,0,0,0,0},
96. {0,0,0,1,1,1,0,0,0,0},
97. {0,1,1,1,0,0,0,0,0,0},
98. {0,0,0,1,1,1,0,0,0,0},
99. {0,1,1,1,0,1,1,0,0,0},
100. {0,1,0,0,0,0,1,1,0,0},
101. {0,0,0,0,0,0,0,0,0,0},
102. {0,0,0,0,0,0,0,0,0,0},
103. {0,0,0,0,0,0,0,0,0,0},
104. {0,0,0,0,0,0,0,0,0,0}
105.
106. Who's code will be the shortest byte solution that passes all these examples successfully?! Good luck. :)
107.
108. Any language is allowed, but I'd like to see an answer in Java especially.
109.
110. Tags: [tag:code-golf][tag:decision-problem][tag:matrix][tag:game]
RAW Paste Data