DIFNEIGH - EDITORIAL

@jaggu1999 thanks for code ,but i don’t understand how my solution is wrong here is the link
i am fraustrated now,
link; CodeChef: Practical coding for everyone

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):

. 1 . 2 . 1 . 2
3 . 4 . 3 . 4 . 
. 2 . 1 . 2 . 1 
4 . 3 . 4 . 3 .

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.

3 Likes

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: CodeChef: Practical coding for everyone

@sdssudhu, I used similar greedy appraoch to fill the table.

So I pre-processed for 50,50 and kept it. Now I just took a submatrix from it and filled for any n,m

Can you elaborate on this part? like how’re are you using the submatrix?

Thanks.

@killerx

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

  1. 1 1 2 …
  2. 2 3 3…

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

  1. 1 2
  2. 1 3
  3. 2 3

hope you unerstand now.

@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: CodeChef: Practical coding for everyone

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.

@lakshitf Try

1
2 2

https://www.codechef.com/viewsolution/22505362
can anyone tell me what’s wrong in this code?

@shristi_raj Try\

2
3 2
6 7

@petch
Sorry I put the wrong code here, my submission is : CodeChef: Practical coding for everyone

Please help me to find the wrong test case.

@lakshitf

1
2 5

Your output is

4
1 1 2 2 1
2 3 3 4 1

But you can use actually use at most 3 numbers:

3
1 2 3 1 2 
1 2 3 1 2

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:

8
1 1
1 2
2 1
2 2
2 3
3 2
3 3
3 4

output:

1
1
1
1 1
1
1
1
2
1 1
2 2
3
1 2 3
1 2 3
3
1 1
2 2
3 3
4
1 1 2
3 3 4
2 2 1
4
1 1 2 2
3 3 4 4
2 2 1 1

I think it is…
1 2 3 4 1 2…
1 2 3 4 1 2…
3 4 1 2 3 4…
3 4 1 2 3 4…

4 Likes

oh yea, I forgot : D

Yes, this pattern rocks :slight_smile:

1 Like

Please give ONLY submission link and remove this code.

This was exactly my thought process to come to the pattern. Nice!!

yeah same here…

For testcase n = 6, m = 1 you are using three different numbers, although two numbers are sufficient:

1
1
2
2
1
1

@agentproton try
1
5 1