Matrix formed with numbers but all the consecutive numbers are place on adjancent positions | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Matrix formed with numbers but all the consecutive numbers are place on adjancent positions

Hi Solo Learn. I have been struggling with a coding problem for a long time and I have decided to ask for help. Given a number n and a number k, create a matrix nxn filled with the first n^2 numbers (1,2,3,4,5,...,n*n), where all of the consecutive numbers are placed on adjacent positions. Also number 1 must be on k-th line. Adjacent means: up,down,left,right. e.g: n=4, k=1 1 2 3 4 12 13 14 5 11 16 15 6 10 9 8 7 I tried to use backtracking, but I feel like there is something simple that I can't see. https://code.sololearn.com/cZ043fpsEof4/#cpp Thank you for your time!

12th May 2020, 2:10 PM
Stefan Secrieru
Stefan Secrieru - avatar
1 Answer
0
Can one of the sort algorithms (I think on cmath) do that?
14th May 2020, 9:06 PM
Mark McGuire
Mark McGuire - avatar