Please write a program to simulate a game called Battleship. First you need to place ships on an N by N board (1 <= N <= 1000). Every ship will occupy a 1 by k area, where k is an integer between 1 and N. The ships cannot overlap and they can only be placed vertically or horizontally. After placing the ship you can hit them with bombs. Each bomb is specified by an (x, y) coordinate, where both x, and y are between 0 and N - 1. You must report whether each bomb hits or misses the ships.

The first line of the input is the board size N. The next line is the number of ships m, where m is between 1 and 100. The next m line indicates the position of the ships. The first two numbers indicate the position of the first cell of the ship, where (N - 1, 0) is at the right lower corner. The third number is the orientation of the ship, where 1 indicates east bound, 2 for south bound, 3 for west bound, and 4 for north bound. Then the fourth number is the size of the ship k. For example, a line 3 4 4 3 indicates that a ship occupies cell (3, 4), (3, 5), and (3, 6). Note that the ship cannot overlap, so if a ship overlaps with previous ship, it will be ignored. Also all ship must not extend to outside of the board, have incorrect size, incorrect orientation, etc. Any ship that cannot be placed will be ignored. Finally the next line is the number of bombs b. In the next b lines we have the coordinates of the places we will bomb.

The output has b lines, and each line is for a bomb. If the bomb hit a ship. you must print the id of the ship. The id of the ship is from 1 to m, where the fist ship in the input has id 1, etc. If the the bomb does not hit a ship, you must print a 0.

Input Format

There is an positive integer N in the first line, m in the second line, the number of ships.
Following m lines are the descriptions of ship 1..m. Following are a number b and b bomb positions.(See sample input)

Output Format

For every bomb, please output the id of the hit ship. If the the bomb does not hit a ship, you must print a 0.

Sample Input

5
4
1 2 1 3
260 676 8 82
1 1 1 3
0 2 4 3
5
1 1
0 3
0 2
2 2
2 2

Sample Output

3
4
0
1
0

Note:

1. The positions of bombs are always valid.
2. When any position of a ship is invalid, the id is skipped.
3. If a ship is hit, the ship is destroyed and will not be hit again.

You can write this program using almost any current language, VB2008, C++, C, Java, or even Fortran.

I suggest you start with a modest grid like 10x10 to make sure the logic works. But also test first to see if the memory will allow a 1000x1000 grid, which is not big, but considerable.
The program logic is straigth-forward enough. Each program requirement translates to some explicit code, no hidden details. Also, the data structure could simply be a 2-d array, again no complications.

You would do well doing a little design and jump onto coding your problem. Although the logic is relatively simple, it does require quite a bit of detail, for example, it take a few lines of code or a few loops to check for overlaps of the ships. So get your hands (or even knees) wet and get going. You'll be happy you did.

To simulate the Battleship game, we can follow these steps:

1. Read the input values for N, the board size, m, the number of ships, and the ship positions.
2. Create an N by N board as a 2-dimensional array, initialized with 0.
3. Place the ships on the board based on their positions and orientations.
4. Read the number of bombs, b, and the coordinates of each bomb.
5. For each bomb, check if it hits a ship and update the board accordingly.
6. Print the id of the ship that is hit by each bomb, or 0 if no ship is hit.

Here's the step-by-step implementation of the program in Python:

```python
# Step 1: Read input values
N = int(input()) # Board size
m = int(input()) # Number of ships

# Create an empty board
board = [[0] * N for _ in range(N)]

# Initialize ship coordinates dictionary
ship_coordinates = {}

# Step 2: Place ships on the board
for i in range(m):
x, y, orientation, k = map(int, input().split())

# Check ship orientation and update coordinates accordingly
if orientation == 1: # East bound
ship_coordinates[i + 1] = [(x, y + j) for j in range(k)]
elif orientation == 2: # South bound
ship_coordinates[i + 1] = [(x + j, y) for j in range(k)]
elif orientation == 3: # West bound
ship_coordinates[i + 1] = [(x, y - j) for j in range(k)]
elif orientation == 4: # North bound
ship_coordinates[i + 1] = [(x - j, y) for j in range(k)]

# Check if ship coordinates are valid and update the board
valid_coordinates = all(0 <= x < N and 0 <= y < N for x, y in ship_coordinates[i + 1])
if valid_coordinates:
for x, y in ship_coordinates[i + 1]:
board[x][y] = i + 1 # Set ship id on the board

# Step 4: Read bomb positions
b = int(input()) # Number of bombs
bomb_positions = [tuple(map(int, input().split())) for _ in range(b)]

# Step 5: Check bomb positions and update the board
for x, y in bomb_positions:
ship_id = board[x][y] # Get ship id at the bomb position
if ship_id > 0: # If ship is hit
print(ship_id) # Print ship id
# Update the board to indicate that the ship has been destroyed
for coord in ship_coordinates[ship_id]:
board[coord[0]][coord[1]] = 0
else: # If no ship is hit
print(0)
```

This program reads the input and implements the game logic. It places the ships on the board, checks if bombs hit any ship, and prints the corresponding ship id or 0 if no ship is hit.

To simulate the game of Battleship and determine whether each bomb hits or misses the ships, you can write a program using the following steps:

1. Read the input values:
- Read the board size N.
- Read the number of ships m.
- Read the positions and descriptions of the ships.
- Read the number of bombs b.
- Read the coordinates of the bombs.

2. Create a board of size N by N and initialize it with zeros. This represents an empty board.

3. Place the ships on the board:
- Iterate over each ship description.
- Check if the ship can be placed on the board, considering its position, orientation, and size.
- If the ship can be placed, update the board with the ship's id for each occupied cell.

4. Check the hits and misses:
- Iterate over each bomb coordinate.
- Check if the bomb hits a ship by looking at the corresponding cell on the board.
- If the cell has a ship's id, print the id.
- If the cell is empty, print 0.

Here is a Python implementation of the program:

```python
def simulate_battleship():
# Step 1: Read the input values
board_size = int(input())
num_ships = int(input())
ships = []
for _ in range(num_ships):
start_x, start_y, orientation, size = map(int, input().split())
ships.append((start_x, start_y, orientation, size))
num_bombs = int(input())
bombs = []
for _ in range(num_bombs):
bomb_x, bomb_y = map(int, input().split())
bombs.append((bomb_x, bomb_y))

# Step 2: Create an empty board
board = [[0] * board_size for _ in range(board_size)]

# Step 3: Place the ships on the board
for ship_id, (start_x, start_y, orientation, size) in enumerate(ships, start=1):
if orientation == 1: # East bound
if start_x + size <= board_size:
for i in range(size):
board[start_x + i][start_y] = ship_id
elif orientation == 2: # South bound
if start_y + size <= board_size:
for i in range(size):
board[start_x][start_y + i] = ship_id
elif orientation == 3: # West bound
if start_x - size + 1 >= 0:
for i in range(size):
board[start_x - i][start_y] = ship_id
elif orientation == 4: # North bound
if start_y - size + 1 >= 0:
for i in range(size):
board[start_x][start_y - i] = ship_id

# Step 4: Check the hits and misses
for bomb_x, bomb_y in bombs:
hit_ship_id = board[bomb_x][bomb_y]
if hit_ship_id != 0:
print(hit_ship_id)
board[bomb_x][bomb_y] = 0
else:
print(0)

simulate_battleship()
```

You can run the program and provide the sample input to get the expected output.