Quantcast
Channel: CodeChef Discuss - latest questions
Viewing all articles
Browse latest Browse all 39796

TABGAME - Editorial

$
0
0

PROBLEM LINK:

Div1
Div2
Practice

Setter-Oleksandr Kulkov
Tester-Teja Vardhan Reddy
Editorialist-Abhishek Pandey

DIFFICULTY:

EASY

PRE-REQUISITES:

Observations, Strings, 2-D arrays, Game Theory.

PROBLEM:

Given $2$ strings which tell if a player bringing coin to that cell wins or loses, we need to tell $Q$ queries asking "Who wins the game if game starts at $(x,y)$ and alice goes first?"

QUICK-EXPLANATION:

Key to AC- Observing winning and losing positions diagonally!!

We have data to tell who wins if stone comes at $(0,i)$ or $(i,0)$. Use that to derive data of first $2$ rows. Use the observation that, winning positions dont change diagonally after row 2. (There are corner cases where they can change from row 1 to row 2 going diagonally, but they are guaranteed to remain constant thereafter). Now, store the states (whether player starting at that cell wins or loses) for first $2$ rows and first $2$ columns. After this, all thats left is to do this to make cases. If $(x,y)$ does not lie in first $2$ rows or first $2$ columns, find the corresponding cell in row/column $2$ diagonal to $(x,y)$ (if cell is not in row 1 or row 2 or column 1 or column 2) to find the answer, as states remain constant diagonally. If cell is in first $2$ rows or columns whose state we calculated above, we simply refer to it to answer the query.

EXPLANATION:

This editorial will have $2$ sections. We will be referring to first the brute force, and then deal with what we observed and how we used it to get full solution.

First Subtask-

There was a lot of confusion in this question related to what does $0$ represent and what does $1$ represent, mostly because the question followed a different convention. Hence, to avoid any such confusion, let me denote $W$ as winning position, i.e. player starting from this cell wins, and $L$ by losing position, i.e. player starting from this cell loses.

Lets first get how the games are played by the players. The only thing you need to remember is, for impartial games, usually the starting position itself determines the winner if players play optimally. If "playing optimally" is something that confused you in this question, read the paragraphs below in tab-

View Content

What does above mean (especially if you read the paragraphs in hidden tabs)? The starting position will determine the winner, and if we know states of previous cells then we can find states ($W$ or $L$) of current cell. All thats left is, finding a base case to start with so we can start finding if the position is $W$ or $L$.

How do we do that....HMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM. The setter gave us $2$ strings, can we use them for this? Turns out we can :D

I will denote the string also by $WLWLWWWL...$ to avoid any confusion regarding $0$ and $1's$. $W$ means player bringing stone at that position wins, and $L$ means he loses.

We can move vertically up, or horizontally left. Hence, the states ($W$ or $L$) of only these 2 cells matter. For cell $(1,1)$ , we know the states of $(0,1)$ and $(1,0)$ in input. Once we get $(1,1)$, then along the row we can derive state of $(1,2)$ , and hence $(1,3)$ and hence $(1,4)$...and so on. Doing this for every row gives us states of all the cells. Now for every query, we see the state of given cell and accordingly append to the string.

You know the base case, and also the recurrence (when and how to assign a state $W$ or $L$). Can you come up with a pseudo code to assign current state $W$ or $L$?

alt text

Please let me make it clear. $W/L$ in matrix means player who starts his turn at that cell wins/loses, while $W/L$ in string means player who brings stone at that cell wins/loses.

Answer is in tab below-

View Content

Full Solution-

Surprisingly, this section wont be long at all!

The only difference between brute force and full solution is that, full solution observes (or proves) that diagonal elements after row 2 remain constant. Hence, while brute force tries to find the entire matrix, full solution derives the state of cell $(x,y)$ using cells in first $2-3$ rows and columns. (We only need cell which is lying "Diagonally backward" from $(x,y)$ in these rows.)

alt text

Images and all those are fine, but how can one actually get the intuition?

The most basic and commonly used method, of course, is observation. But proofs are needed to be done, so that we can verify and also better ourselves at such questions. I will first state the lemma's or rules, and then move on to prove them one by one. Proofs are in tabs, so that you can first attempt to derive them.

  1. If $A_{ij}=L \implies A_{i+1,j+1}=L$ as well. Meaning, if cell $(i,j)$ is losing state, then cell $(i+1,j+1)$ is losing state as well.
  2. Can we sketch a similar proof for $A_{ij}=W$? Why/Why not? What significance does it have?
  3. Prove that there cannot be more than $3$ consecutive $W's$ in matrix after row 1.

Proof of Lemma 1-

View Content

Answer for 2.-

View Content

Proof for 3.

View Content

Now, with this idea clear, how do we implement it? One of the neat implementations used by @um_nik ought to be discussed here :D.

  • Create a matrix. Find Row 1 and Row 2, along with Column 1 and Column 2 as we discussed above.
  • If Query is in row 1 or row 2, we already have the answer
  • Else, diagonally move backward until you come at row or column 2. The answer is stored in your table.

Code for reference is in tab below-

View Content

SOLUTION

Author's solution can be found here.

Tester's solution can be found here.

View Content

Editorialist's solution can be found here.

$Time$ $Complexity=O(N)$
$Space$ $Complexity=O(N)$

CHEF VIJJU'S CORNER :D

1. Is your idea same as editorial? Still got TLE? Make sure you done use $s=s+'1'$ for making the output string, as this method creates a completely new string and then adds '1' to end, making it $O(N^2)$! Use $s+=1;$ for better performance. Just like creating a new vector to add an element at last v/s adding an element at back of vector.

2. At the end of the day, I'd want you to remember the part of $W$ and $L$ which we discussed. You can apply that to any such game. If starting position determines winner, and you know the state of positions which you can visit from current cell, you can derive for this cell as well. A player wins only if he can force his opponent to lose by making sure opponent plays only at "losing" or $L$ cells. Hopefully this will clear the numerous doubts among div2 guys on what "playing optimally" is :)

3. Often I am asked, what does a coder truly do to ace all the contest's problems?

View Content

4. Setter's Notes (his solution isnt based on observations)-

View Content

5. Related Problems:
- Hackerrank Section for basic problems on game theory.
- Codeforces Section for trickier problems.


Viewing all articles
Browse latest Browse all 39796

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>