PROBLEM LINK:Practice Setter: Evgeniy Artemov DIFFICULTY:EasyMedium PREREQUISITES:Pigeonhole principle, Constructive algorithms and Casebased Analysis. PROBLEM:Given two integers $N$ and $M$, find out any matrix with $N$ rows and $M$ columns using as less distinct numbers as possible, such that for all cell, all values of neighbors of that cell are pairwise distinct. SUPER QUICK EXPLANATION
EXPLANATIONConsidering a $N*M$ table, we can see that if the maximum number of neighbors any cell has in a table is $K$, there cannot exist any valid table using less than $K$ distinct values. The reason is, that if we take $K$ values out of distinct $ < K$ values, It is guaranteed to have at least one value occurring more than once, which means that the values of neighbors of that particular cell are not pairwise distinct. Hence, the table is invalid. Refer Pigeonhole principle for proof. Thus, we need to find a way to fill the table using $K$ values. General case In general case where we have $K = 4$, occurs when both $N, M \geq 3$. Here, in every row or column, we need the first element to differ from the third element in the row, the second element should differ from the fourth element, the third element should differ from the fifth element and so on. If we choose the fifth element to be the same as the first element, it shall make no difference. It is recommended to try a few ways to fill a $4*4$ table and then trying to extend it either way. Here, it is possible to have different constructions than the one mentioned here, so feel free to share yours. One possible way to fill a $4*4$ table is
Now, it can be easily seen, that we can just keep appending rows and columns without causing any conflict for any cell, thus efficiently generating our table. Special cases Assuming $N \leq M$. Values $N$ and $M$ can be swapped if otherwise.
This covers all cases where $K < 4$. All other case have $N, M \geq 3$ which implies $K == 4$ as seen above. Time ComplexityTime complexity is $O(N*M)$ per test case. AUTHOR'S AND TESTER'S SOLUTIONS:Setter's solution Feel free to Share your approach, If it differs. Suggestions are always welcomed. :)
This question is marked "community wiki".
asked 14 Jan, 16:05

If we think of a matrix as a chessboard, then all white and all black cells can be filled independently. I picked the following pattern to fill, let's say white cell (dots represent black cells):
The same pattern can be used for black cells (leading to the same solution as in editorial), or it can reflected along vertical axis and then used in black cells (leading to the same solution as @l_returns), or it can be shifted, rotated, etc. and then used in black cells. answered 14 Jan, 23:58
This was exactly my thought process to come to the pattern. Nice!!
(15 Jan, 00:08)
yeah same here...
(15 Jan, 00:17)

Answer is hidden as author is suspended. Click here to view.
answered 14 Jan, 23:17
4
I think it is...
(14 Jan, 23:30)
oh yea, I forgot : D
(14 Jan, 23:41)
1
Yes, this pattern rocks :)
(15 Jan, 00:06)

What I did was I followed a greedy approach. Just check the neighbors and find the mex. However I found it didn't work for some cases like (5,5). So I preprocessed for 50,50 and kept it. Now I just took a submatrix from it and filled for any n,m (except min(n,m) <=2 where greedy is used). answered 14 Jan, 21:05

I did
answered 14 Jan, 21:55

I used a backtracking search for this. The trick to making it work is the direction to search in. The obvious approach is something like this:
This is too slow because if the grid is wide, you have to search an extremely large number of possibilities before you discover that a given value of $k$ is impossible. I searched like this.
This way, if a given value of $k$ is impossible, the search is confined to a corner and takes very little time. And if $k=4$ and the grid is large, it tends to settle into a tileable pattern and repeats it without having to backtrack. answered 14 Jan, 21:08

I just simply generated a 50*50 matrix using the condition i.e., pigeonhole principle and for special cases I just written code manually. Refer to my code which I simply generated the matrix. answered 14 Jan, 23:13

@jaggu1999 thanks for code ,but i don't understand how my solution is wrong here is the link i am fraustrated now, link; https://www.codechef.com/viewsolution/22481898 answered 14 Jan, 23:31
@agentproton try
(15 Jan, 21:43)

We all know that a number can have at most 4 neighbors, and no two neighbor can be the same so k=4 at max. Now for the pattern I applied the following sequence: 1 2 3 4 1 2 3 4.... 1 2 3 4 1 2 3 4.... 3 4 1 2 3 4 1 2.... 3 4 1 2 3 4 1 2.... 1 2 3 4 1 2 3 4.... and so on. Link to my solution: https://www.codechef.com/viewsolution/22373862 answered 15 Jan, 09:28

all you have to do is check n<m if(n<m) you van print the sub matrix as it is other wise you have to print it swapping m and n and print it correctly. eg
if n<m and say n=2 and m=3 you can print the above submatrix but if n=3 and m=2 you have to print
hope you unerstand now. answered 15 Jan, 16:20

@sdssudh @jaggu1999 Can anyone find a case for which my code is giving wrong output? Thanks. I also have used an approach like @sdssudh, Link: https://www.codechef.com/viewsolution/22482748 I have found the mex for each position, and filled that at a[i][j], also for some cases like for 5*5 matrix, it was giving k=5, then I computed the solution of 50 cross 50 matrix, and printed out the submatrix. answered 15 Jan, 19:28

https://www.codechef.com/viewsolution/22505362 can anyone tell me what's wrong in this code? answered 16 Jan, 02:02

@petch Sorry I put the wrong code here, my submission is : https://www.codechef.com/viewsolution/22482748 Please help me to find the wrong test case. answered 16 Jan, 20:09

Please can someone suggest me what's wrong in my code? I am not getting where am i going wrong. https://www.codechef.com/viewsolution/22517943 this is my sample input and output: input:
output:
answered 17 Jan, 17:15
