Mr and Mrs AntProblem code: MMANT |
All submissions for this problem are available.
Mr. and Mrs. Ant are very hungry. So, they want to collect food as much as they can. They can search for foods simultaneously. To do so, they start from their house and collect all foods together and meet in some place (not necessarily their house). Finally, they eat together.
The world of Mr. and Mrs. Ant is a two dimensional grid. Each cell is either the home, or free, or blocked, or contains a food. Two cells are adjacent if they share an edge. In each second, they can move from one cell to another cell simultaneously. One can decide to not to move in some step, while other may move. One cell can be visited many times. Both of them can move into the same cell also.
In this problem, the grid is given by an R x C matrix represented by following characters:
| H | Home of Mr. and Mrs. Ant | Occurs exactly once |
| F | A food item | Occurs at least once, at most 8 times |
| . (dot) | Free (passable) cell | - |
| # (hash) | Blocked Cell | - |
Given the grid information, give the minimum amount of time that must be needed for them to collect all the foods and then meet.
Input
The first line of input will contain T (T <= 30) denoting the number of cases. Each case starts with two integers R and C (2 <= R, C <= 12). Then, R lines follow giving the grid.
Output
For each case, print the case number, the minimum amount of time (in seconds) that must be needed for them to collect all the foods and meet. If it is impossible to collect all the food items, output -1 (negative one) instead.
Example
Input: 2 2 3 H#. .#F 2 6 F#F..# ..H#.F Output: Case 1: -1 Case 2: 8
| Author: | admin |
| Date Added: | 28-10-2011 |
| Time Limit: | 10 sec |
| Source Limit: | 50000 Bytes |
| Languages: | C, C99 strict, CPP 4.3.2, JAVA |
Comments
SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:
HELP
Program should read from standard input and write to standard output. After you submit a solution you can see your results by clicking on the [My Submissions] tab on the problem page. Below are the possible results:
- Accepted
Your program ran successfully and gave a correct answer. If there is a score for the problem, this will be displayed in parenthesis next to the checkmark. - Time Limit Exceeded
Your program was compiled successfully, but it didn't stop before time limit. Try optimizing your approach. - Wrong Answer
Your program compiled and ran succesfully but the output did not match the expected output. - Runtime Error
Your code compiled and ran but encountered an error. The most common reasons are using too much memory or dividing by zero. For the specific error codes see the help section. - Compilation Error
Your code was unable to compile. When you see this icon, click on it for more information.
If you are still having problems, see a sample solution here.

Fetching successful submissions
