scheme - Get Diagonals in a Matrix -


Using the racket, and avoiding any state actions (which ends with an exclamation mark), I am trying to get all the "diagonals",

strips" in a list of lists, or a vector if it is easy.

This "n-queens" is a part of solving the problem of chess , But I do not want to see it disappointed about how to solve it's problem completely The relevance is that I am trying to determine whether a queen can attack diagonally any other queen. I thought I could do it for such horizontal and vertical attacks - Try changing each row or column to your list and see that there is more than one queen in a list.

However, the diagonal strips are becoming very ugly and complex code!

Example:

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 To solve this problem should be a good, concise method.   

I have the following lists Will be created by:

  '(1 6 11 16)' (2 5) '(2 712)' (3 6 9) '(3 8)' (4 7 10 13) I have noticed that there is a form in these lists where the additional element is either <5 10 15) (914) '(81114)' (12-15)   

code> (+ n 1) are greater than the previous element, or they are more than the previous element, but checking the limits of other issues, my code multi (In this case, n is the length of one dimension.)

Am I wrong about this leaving from? Is there an easy way to check whether the diagonals are threatened by another queen? Or is it the right idea, but I think I'm missing out on a few simple tasks to simplify all this one tonne?

Some thoughts:

  1. You do not need to list all the diagonals , What you need is a diagonal for each queen on the board
  2. If you really want to actually all the diagonals, then the data structure is better to change. Although I'm sure this problem can be solved using lists, but I did not want to switch to vectors, so I can use the index efficiently

    given Anyway, you can write a reference reference index list through list-rif but it will not be efficient / elegant or you can list lists with list-> vector Vectors can convert, vec Write an index-based solution with tor-ref and then go back to the lists with vector - & gt; List

    For example, you can do the following solution (customized with this). It receives the vector vector in the form of an input (if you want to use the list of lists, replace the input with vector-ref with list-ref Although it would be less efficient), and gives a list of lists with diagonals. Elements add in different order, but the value is correct (exercise for the reader: format the output so that it matches the sample in question) Notice how I am using for looping, this idiomatic racket and this kind of problem Appropriate depends on the index:

     ; Returns a list of lists with all the diagonals in a square matrix; There is a single element except the diagonals; M: represented as a vector of matrix vectors; N: Number of rows (or columns, because this is a square) (define diagonal MN) include two results (append; first: leading diagonal (for list (/ (piece (range (boundary (range 1 (- (* (* 2 N () () () () () () () (((and (lieutenant; piece n) 0 (add1 (piece n)))) () (for / (list ([j (in-range z (add1 (- piece z)) () () Vector-riff (vector-rif M (sub 1 (- nj)) (- piece j))) Second: antigrogale (for list / ([piece (range) 1 (- (0 for (list ([j (in-range z ( D1 (- (Z (If Lt; Piece N) 0 (AD1 (- Piece N)))) () You can test it as follows, it works as expected.  
      (define matrix # (# (1 2 3 4) # (5 6 7 8) # (910 11 12) # (13 14 15 16))) (diagonal matrix 4) = & gt; '(14 9) (15 10 5) (16116 1) (12 7 2) (8 3) (2-5) (399) (4 7-10 13) (81114) ( 12 15))    

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -