tag:blogger.com,1999:blog-4150232295397368657.post4918909572252145283..comments2012-10-18T16:48:23.835-04:30Comments on /random/thoughts: Spiral matrixnicolaslarahttp://www.blogger.com/profile/15167966852380794888noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-4150232295397368657.post-23718135239055661192009-09-17T09:09:46.987-04:302009-09-17T09:09:46.987-04:30May somebody help me to adjust the above algorithm...May somebody help me to adjust the above algorithm(about spiral matrix) for C<br />thanks in advancef1bg2001https://www.blogger.com/profile/09688208468222660742noreply@blogger.comtag:blogger.com,1999:blog-4150232295397368657.post-35421366326014103002009-09-17T08:33:44.176-04:302009-09-17T08:33:44.176-04:30I am newbie and I want to write this(spiral matrix...I am newbie and I want to write this(spiral matrix) in C <br />can somebody help meAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-4150232295397368657.post-13179856256901173072009-05-18T00:39:00.000-04:302009-05-18T00:39:00.000-04:30Sorry I made a mistake... When i said
"Here we fi...Sorry I made a mistake... When i said<br /><br />"Here we first got [[5,0,1],[4,3,2]] and the we double-reverse this list."<br /><br />I mixed the order of the sublists. I wanted to say<br />"Here we first got [[4,3,2],[5,0,1]] and the we double-reverse this list."JFDRhttps://www.blogger.com/profile/01323031118444594872noreply@blogger.comtag:blogger.com,1999:blog-4150232295397368657.post-90154891958237849552009-05-18T00:32:00.000-04:302009-05-18T00:32:00.000-04:30Hey!! I liked a lot your post!. This is the kind o...Hey!! I liked a lot your post!. This is the kind of stuff that makes me follow a blog: unexpected, challenging and interesting posts :P<br /><br />I writed some code to solve this problem, also in haskell and with a similar approach. I tried to make it shorter and clearer than the one you proposed, but it wasn't possible :P, however it runs faster for big instances (100 onwards)<br /><br />Here goes the code and bit of explanation.<br />------------------<br />This is and also recursive implementation. The idea is to find the spiral matrix of n, using the spiral matrix of n-1 and so on.<br /><br />Unlike your implementation, this one returns a list of lists, where each sub list corresponds to a row.<br /><br />The basic procedure is the following:<br />For example to find the spiralM of order 3,already having spiral of order 2 --> [[3,2], [0,1]] <br /><br />We want to add at the end of each row of the submatrix<br />the elements that don't correspond to the first row of the next matrix (as Nicolas explained ).This is '5' and '4'. Let's call this two elements 'last of row' elements.<br />To do this we may add each 'last of row'element at the start of each sublist that corresponds to it in ascending order. Then reverse the list resulting of reversing every Sublist.<br />And we'll obtain: -> [[1,0,5], [2,3,4]]<br /><br />Here we first got [[5,0,1],[4,3,2]] and the we double-reverse this list.<br /><br />At last we must add at the beginning of the current matrix the list the sublist [8..6]. This will result in the Spiral Matrix we are looking for.<br /><br />Why adding elements add start? Because it's requires less time in processing, avoiding all the time using functions as append (++) that requires to walk over the entire list.<br /><br />Here goes the code... Thank you again for this great post<br /><br />-- returns the spiral matrix of order n <br />spiral:: Int -> [[Int]]<br />spiral 1 = [[0]]<br />spiral n = nextSpiral n (spiral (n-1))<br /><br />-- returns the next spiral matrix given the <br />-- previous'oldSpiral'<br />nextSpiral:: Int -> [[Int]] -> [[Int]]<br />nextSpiral n oldSpiral= ((firstRow (n*n) (n*n - n )) : doubleReverse (addBegin oldSpiral ((n-1)*(n-1)))) <br /><br />-- returns the list resulting from adding the next <br />-- element of the descendent count starting whith 'n' -- and finishing with 'stop' at the start of each <br />-- element<br />addBegin:: [[Int]] -> Int -> [[Int]]<br />addBegin [] _ = []<br />addBegin (xs:ys) n = ((n:xs) : addBegin ys (n+1)) <br /><br />-- reverse a list after reversing every sublist<br />doubleReverse :: [[Int]] -> [[Int]]<br />doubleReverse xs = reverse (map reverse xs)<br /><br />-- returns the list from last to n-1<br />firstRow:: Int -> Int -> [Int]<br />firstRow n last = reverse [last..n-1]JFDRhttps://www.blogger.com/profile/01323031118444594872noreply@blogger.comtag:blogger.com,1999:blog-4150232295397368657.post-38401036159349792572009-04-21T05:56:00.000-04:302009-04-21T05:56:00.000-04:30range = enumFromTo 0range = enumFromTo 0Anonymousnoreply@blogger.com