HomeVolumesContestsSectionsForumsUsersPrintHelpAbout

Volumes > Data Structures > problem:


50711 - Snail Trails

Guest
• Discussion of problem (1)

Volume problems

• 50593 - Transporter
• 50598 - Minimum Sum
• 50704 - Connected?
• 50705 - Student Clubs
• 50779 - The shortest path in a maze
• 180. 50776 - The number of differen...
• 2007.A. 51071 - Phalanx-2
• 50. 50718 - Elevator
• 50711 - Snail Trails
• 50674 - Collecting Eggs
• 50306 - Beautiful Numbers
• 50411 - Sum of the Leaves
• 50675 - Kruja Boys
• 50421 - Repairing road segments
• 50736 - Top N Donors - 2
• 50775 - Balanced Parenthesis
• 50774 - Hot Potato

Feedback

If you notice incorrect translations in Contester, please let author know.

Time limit 2000/4000/4000/4000 ms. Memory limit 65000/65000/65000/65000 Kb.
Adapted from USACO. Difficulty Alpha

Snail Trails

From USACO - Brian Jacokes

Sally Snail likes to stroll on a N x N square grid (1 < n ≤ 120). She always starts in the upper left corner of the grid. The grid has empty squares (denoted below by '.') and a number (B) of barriers (denoted below by '#'). Here is a depiction of a grid including a demonstration of the grid labelling algorithm:

Sally travels vertically (up or down) or horizontally (left or right). Sally can travel either down or right from her starting location, which is always A1.

Sally travels as long as she can in her chosen direction. She stops and turns 90 degrees whenever she encounters the edge of the board or one of the barriers. She can not leave the grid or enter a space with a barrier. Additionally, Sally can not re-cross any square she has already traversed. She stops her traversal altogether any time she can no longer make a move.

Here is one sample traversal on the sample grid right:

Sally traversed right, down, right, down, left, up, and right. She could not continue since she encountered a square already visited. Things might have gone differently if she had chosen to turn back toward our left when she encountered the barrier at F5.

Your task is to determine and print the largest possible number of squares that Sally can visit if she chooses her turns wisely. Be sure to count square A1 as one of the visited squares.


INPUT FORMAT
The first line of the input has N, the dimension of the square, and B, the number of barriers (1 ≤ B ≤ 200). The subsequent B lines contain the locations of the barriers. The sample input file below describes the sample grid above. The sample output file below is supposed to describe the traversal shown above. Note that when N > 26 then the input file can not specify barriers to the right of column Z.

SAMPLE INPUT
  8 4
  E2
  A6
  G1
  F5

OUTPUT FORMAT
The output file should consist of exactly one line, the largest possible number of squares that Sally can visit.

SAMPLE OUTPUT:
  33

Using this traversal on the right:


Для отправки решений необходимо выполнить вход.

www.contester.ru