Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

Programming Challenge | Expert | Task - Magic Forest

Programming Challenge | Expert | Task - Magic Forest


I've found a collection of numerous problems from my previous algorithmic competitions. Let's if members on Hack Forums will be able to solve some of them.
Please provide your solutions in C or C++.When you provide me with your solution I will check if your output will match with the original samples from the author of the task or if your solution exceeds the time and memory limit.
These tasks are not for beginners.

Magic Forest
You're the magician currently in the middle of your journey to become a wizzard. Your current obstacle is go through the infamous Magic Forest.

The Magic Forest is a two-dimensional grid consisting of r rows and c columns. Each cell in Magic Forest may contain a tree, or may be vacant. A vacant cell may be occupied by bad magician. Magicians (including you) cannot enter cells with trees. One of the cells is designated as the exit cell.

The initial grid, including your initial position (S), the exit cell (E), the positions of the trees (T) and the initial positions of all other magicians (denoted with numbers from 1 to 9), will be given to you. Here's an example of such grid (from the first example):
[img]http://mendo.mk/task_files/forest.png[/img]
Magicians (including you) may move in the forest. In a single move, magicians may perform one of the following actions:

- Do nothing.
- Move from the current cell to one of the four adjacent cells (two cells are adjacent if they share a side). Note that magicians cannot enter cells with trees.
- If you are located on the exit cell, you may leave the forest. Only you can perform this move — all other bad magicians will never leave the forest by using this type of movement.

After each time you make a single move, each of the other bad magicians simultaneously make a single move (the choice of which move to make may be different for each of the magicians).

Your health is measured in HU (Health Units). At the beginning of the journey your health is 100 HU. If a bad magician is located in the same cell with you at some moment, he can cast a spell and reduce your health for some HUs. Each bad magician can cast his spell on you only once. If at some moment your health becomes 0 HU, then you are dead. Note that the moment you leave the forest, no more bad magicians can cast a spell on you, even if another bad magician moves to the exit cell immediately after that.

You would like to leave the forest. In order to do so, you have to make a sequence of moves, ending with a move of the final type. Before you make any move, however, you post this sequence on your personal virtual idol Blog. Then, you will follow this sequence of moves faithfully.

Print the maximum number of HUs that you could have when you enter the exit cell or print 0 if you cannot enter the exit cell with positive number of HUs assuming that you pick the sequence of moves that minimizes this number. Note that you are not required to minimize the number of moves you make.

Input
The first line of input contains one positive integer T (1 <= T <= 20), the number of test cases.

Each test case starts with a line that contains exactly two integers: r and c (1 ≤ r, c ≤ 1000), denoting the number of rows and the number of columns in Magic Forest. The next r rows will each depict a row of the map, where each character represents the content of a single cell:

- 'T': A cell occupied by a tree.
- 'S': An empty cell, and your starting position. There will be exactly one occurrence of this in the map.
- 'E': An empty cell, and where the exit is located. There will be exactly one occurrence of this in the map.
- A digit (1-9): A cell represented by a digit X means that the cell is empty and is occupied by a bad magician whose spell strength is X.
- '.': An empty cell that is not occupied by any magician

It is guaranteed that it will be possible for you to go from your starting position to the exit cell through a sequence of moves.

Output
For each test case, output the maximum number of HUs that you could have when you enter the exit cell or print 0 if you cannot enter the exit cell with positive number of HUs.

Constraints
Time limit: 3 seconds
Memory limit: 64 megabytes

Examples

Input
2
6 8
4...E.T.
...TTT.1
..3.T...
.TT.T9T.
.8..S.T.
...T..T.
3 5
.....
S.E.1
.....

Output
93
99

For the first example: You cannot reach the exit without meeting with the bad magicians with spell strength of 3 and 4. Note that in this example all bad magicians have different spell strengths, but it is possible that more than one bad magician have same spell strength.
[Image: dead.gif]
This is one hell of a challenge. Did you come up with this yourself?
[Image: chain.png]
(Mar 12, 2015 12:58 PM)Sajuuk Wrote:  This is one hell of a challenge. Did you come up with this yourself?

I modified an old task that I've found in my folders. The funny thing is that I simplified it. Those folders that I have contain hundreds of tasks from algorithmic competitions.
[Image: dead.gif]
There are layers of recursion here in addition to the main recursive branch which must problem solve for the least quantity of health loss out of the evaluation of all other partial problem subsets.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
This reminds me of one of this year's Hackercup problems. Instead of magicians, there were towers that rotate clockwise and shoot a laser beam in whatever direction they're facing, everytime you make a move. But this is even more complicated, especially due to the randomness of the evil magicians' moves.
Could you elaborate on the behavior of the other magicians? I don't see how it's possible for there to be a single unique path if the magicians can either move to any empty square or do nothing at random, there doesn't seem to be any determinism.

You say that in the example above, it's impossible to complete without being hit by magicians 3 and 4. Why? Isn't a possible subset of the magician's moveset that they all do nothing? Or that they move in such a way that you never encounter one?

The problem Eight mentioned makes more sense since the towers make the same specific move every turn.....the wizards have the choice to move to any of the adjacent squares or do nothing and I don't see any rules dictating which move they make.

Maybe I'm missing something. Are the other magicians also trying to go for the exit?
[img]http://66.media.tumblr.com/acfffd664b499bccaea0a5bcfd0debf0/tumblr_of816spRTW1twdas2o1_400.jpg[/img]

(Mar 13, 2015 07:40 AM)Kantos Wrote:  Could you elaborate on the behavior of the other magicians? I don't see how it's possible for there to be a single unique path if the magicians can either move to any empty square or do nothing at random, there doesn't seem to be any determinism.

You say that in the example above, it's impossible to complete without being hit by magicians 3 and 4. Why? Isn't a possible subset of the magician's moveset that they all do nothing? Or that they move in such a way that you never encounter one?

The problem Eight mentioned makes more sense since the towers make the same specific move every turn.....the wizards have the choice to move to any of the adjacent squares or do nothing and I don't see any rules dictating which move they make.

Maybe I'm missing something. Are the other magicians also trying to go for the exit?

The task was given on the Balkan Olympiads in 2008 so it can't be impossible.
[Image: dead.gif]
(Mar 13, 2015 08:01 AM)Deadbyte Wrote:  The task was given on the Balkan Olympiads in 2008 so it can't be impossible.

Well if it's not impossible, then there should be answers to my questions. I'm asking for clarification here. I can't find a reference to this problem anywhere else.

Why can't you reach the exit without getting hit by 3 and 4?
[img]http://66.media.tumblr.com/acfffd664b499bccaea0a5bcfd0debf0/tumblr_of816spRTW1twdas2o1_400.jpg[/img]

(Mar 13, 2015 08:43 AM)Kantos Wrote:  Well if it's not impossible, then there should be answers to my questions. I'm asking for clarification here. I can't find a reference to this problem anywhere else.

Why can't you reach the exit without getting hit by 3 and 4?

Hints:
http://en.wikipedia.org/wiki/Graph_(abstract_data_type)
http://en.wikipedia.org/wiki/Breadth-first_search
http://en.wikipedia.org/wiki/Depth-first_search

Your provided code will be graded on 10 test examples.
Here is the first test example a rar file with 2 text documents the input and the output.
http://www.filedropper.com/test4441
(Mar 13, 2015 08:51 AM)Deadbyte Wrote:  Hints:
http://en.wikipedia.org/wiki/Graph_(abstract_data_type)
http://en.wikipedia.org/wiki/Breadth-first_search
http://en.wikipedia.org/wiki/Depth-first_search

I understand how to solve the problem, I'm simply asking for clarification on how the magician's move because it is unclear how the magicians choose where they move to. Those links don't answer that.

I'll ask you again : Why can't you reach the exit without getting hit by 3 and 4? If your goal is to find a path resulting in the maximum hitpoints possible, then it should be 100 for the first example since the other magicians can choose to simply do nothing every turn. I've read the prompt multiple times and there's nothing saying that the magicians have to move anywhere. Are the magicians also trying to reach the exit?
[img]http://66.media.tumblr.com/acfffd664b499bccaea0a5bcfd0debf0/tumblr_of816spRTW1twdas2o1_400.jpg[/img]

(Mar 13, 2015 09:03 AM)Kantos Wrote:  I understand how to solve the problem, I'm specifically asking for clarification on how the magician's move. Those links don't answer that.

I'll ask you again : Why can't you reach the exit without getting hit by 3 and 4? If your goal is to find a path resulting in the maximum hitpoints possible, then it should be 100 for the first example since the other magicians can choose to simply do nothing every turn. I've read the prompt multiple times and there's nothing saying that the magicians have to move anywhere. Are the magicians also trying to reach the exit?

First provide a code, I will test it on the 10 test examples.
I won't give more info on the task. If you think that the task has a flaw or it is invalid.
Contact here: http://www.ioinformatics.org/index.shtml
[Image: dead.gif]
(Mar 13, 2015 09:07 AM)Deadbyte Wrote:  First provide a code, I will test it on the 10 test examples.
I won't give more info on the task. If you think that the task has a flaw or it is invalid.
Contact here: http://www.ioinformatics.org/index.shtml

I can't provide a code because the problem is not clearly defined, which is what I've been saying since my first post. The problem Eight posted is more clearly defined and it's only one sentence long.

You're the one who posted the challenge, you should be able to answer the simple questions I am asking you.

It's not even listed on the website you posted http://wiki.ioinformatics.org/wiki/Category:Tasks
[img]http://66.media.tumblr.com/acfffd664b499bccaea0a5bcfd0debf0/tumblr_of816spRTW1twdas2o1_400.jpg[/img]

(Mar 13, 2015 09:15 AM)Kantos Wrote:  I can't provide a code because the problem is not clearly defined, which is what I've been saying since my first post. The problem Eight posted is more clearly defined and it's only one sentence long.

You're the one who posted the challenge, you should be able to answer the simple questions I am asking you. It's not even listed on the website you posted http://wiki.ioinformatics.org/wiki/Category:Tasks

Of course it is not listed it is IOI not BOI.
[Image: dead.gif]
(Mar 13, 2015 09:17 AM)Deadbyte Wrote:  Of course it is not listed it is IOI not BOI.

That's the page you linked me.

Whatever man, I'm not doing the challenge since you refuse to answer any questions, I doubt you've even solved it yourself.
[img]http://66.media.tumblr.com/acfffd664b499bccaea0a5bcfd0debf0/tumblr_of816spRTW1twdas2o1_400.jpg[/img]



Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by myselfandiAug 11, 2019 03:34 AM 12 263 Aug 11, 2019 03:34 AM
Last Post: YouthInAsia
[.NET] by zerocool1245Feb 6, 2017 10:18 AM 3 247 Feb 6, 2017 10:18 AM
Last Post: Ninja
[C] by JakeDarkbladeOct 2, 2016 10:39 AM 22 1,769 Oct 2, 2016 10:39 AM
Last Post: Captain Steve Rogers