# CHEFIHG - Editorial

Author: Misha Chorniy
Tester: Karan Aggarwal
Editorialist: Pushkar Mishra

Easy-Medium

### PROBLEM:

Given is a matrix of dimensions N\times M in which some cells are marked with “.”, some with “", and one with “C”. The “.” indicates that the cell can be visited, "” denotes restricted cell and “C” denotes the capital city. We have to give a sequence of moves of length at max 10^5 in terms of “L” (left), “R” (right), “U” (up) and “D” (down) such that a robot starting from any “.” cell in the in the grid and following the sequence of moves visits the “C” cell at least once.

### EXPLANATION:

This problem doesn’t require much thinking beyond the brute force way to do it. However, it is implementation heavy.

It is given that our matrix can at maximum be of 20 \times 20 cells. This clearly hints towards the algorithm being close to brute force. Let us think about all the “.” cells one by one. To begin with, let us look at the farthest “.” cell from the capital city. How far can it be from the capital city? It is given that all the border cells are “*”. This means that there can be a maximum of 400 - (20 - 20 -18 - 18) = 324 cells at maximum which are “.” marked.

This tells us that the shortest path from any “.” cell to capital city is at max 324 instructions (instructions being “L”, “R”, “U”, “D”) long. We first make a string of moves for the cell “.” that is farthest from the capital city. For all other “.” cells, we will have to execute the the same moves as well. After all the commands are executed, the farthest one has reached the capital city, and the others are in some position in the grid (most likely, not in the position they were).

Then we pick another one, and make it reach the capital city. The sequence of moves it takes for this is appended to the string we had from before, and all the remaining “.” cells are again moved accordingly. We continue doing this for each of the “.” cells till we have made each of them reach the capital city.

What is the length of the string at the end of all these operations? We have to move at most 324 cells. As we calculated before, the maximum length for one movement is 324 again. So, in total, 324*324 = 104976. This is slightly more than 10^5. How do reduce this to below 10^5? Actually, if we reason a bit more, we will see that the shortest path from any place in the grid to the capital city is 324 in theory only; it is actually much less. This is because 324 will only be if during a path from a cell to the capital, one has to go over all other cells. But why would one do that? We can move in all directions, so we can simply go from one row to its neighbouring ones or one column to its neighbouring ones. This tells us that the path will definitely be much shorter, definitely shorter than something like 305. And 305 * 324 = 98820. This is less than 10^5. So this works.

But what is the time complexity? For each of the “.” cells, we need to first do a BFS to find the shortest move sequence to the capital city. Then, for all the remaining “.” cells, we need to make them follow the same move sequence. There can be \mathcal{O}(NM) cells of type “.”. BFS takes \mathcal{O}(NM). But moving all other “.” cells on the same move sequence takes \mathcal{O}(N^2M^2); hence, this is the costliest operation per “.” cell. So the total complexity is \mathcal{O}(N^3M^3). This is sufficient given that N = M = 20 in the maximum case.

Please see setter’s/tester’s program for implementation details.

### COMPLEXITY:

\mathcal{O}(N^3M^3)

### SAMPLE SOLUTIONS:

2 Likes

Look at this solution. Just random Output gets AC

14 Likes

We had discovered it during the testing, we tried some test cases which can break some of the randomized solution. It was really hard to generate test cases for it. I would be interested if someone has a good test case to break such solutions.

2 Likes

What was that :3 :3 :3

Well The borders are always blocked with “*” so it can solve most of the cases as dpraveen said…though someone can still find an awesome test case to challenge the solution…

Well, even the tester’s solution is random output :3 then why add such a question?

6 Likes

Could someone please find the error ??

Solution

I implemented the same approach,but was getting WA…whats the problem ?

https://www.codechef.com/viewsolution/10899912

My solution is somewhat simpler:

• find some decent paths to the capital from every free cell, e.g. by NM times looking at all cells which don’t have a path yet and which have a neighbour with a path
• while true: add the path to the capital from one of them to the instructions, simulate it from each cell in S, discard those which reach or pass through the capital

I didn’t really think about how fast this is other than using at most N^2M^2=160000 instructions.

I wouldn’t mind this passing. But random…

4 Likes

“That guy would have never guessed this thing .”
That guy is one of the top competitors worldwide, so I’m pretty sure he would have.

2 Likes

Could any one provide any test case I implemented the same algo getting WA
https://www.codechef.com/viewsolution/10900398

I <3 Tanya Romanova

It seems the test cases are modified after the contest. It no longer accepts random generated output.
The tester’s solution needs to be updated to pass the new test cases.
My submission(Copied from the one who tried random approach):
[https://www.codechef.com/viewsolution/10900542][1]

Note 1: The PROBLEM section needs editing. Typo in “’” instead of “*”
[1]: https://www.codechef.com/viewsolution/10900542

1 Like

Sir I have one doubt what if by applying the 324 step on the further most,what if any random block becomes the further most(robot moves to further most block),then it is not necessary it will work by this approach.

This isn’t I_love_Tanya_Romanova’s solution, this is solution of Tanya Romanova ( https://www.codechef.com/users/romanova )

4 Likes

@ aayush_10zn Yes the test cases are now modified , now they are not accepting the solutions, which have randomly generated output…

Exactly https://www.codechef.com/users/lebron
And this is his solution : https://www.codechef.com/viewsolution/10893264

A case like this would not have allowed this:
20 20
C…
.

.

.

.

.

.

.

.

*********.

.
.
.
.
.
.
.
.
.
.

to enforce boundary restriction u can make the case for 1919 and then put '’ along the boundaries to get a 20*20.

I know he isn’t I_love_Tanya_Romanova, I meant to say that, I love this(not orig) Tanya Romanova

Who told that ?
Non-determinism is something which can never be beaten-

https://www.codechef.com/viewsolution/10900841

3 Likes
//