Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all the position of the letter in the grid is easy to compute:</p> <pre><code>function get_pos( c : char) begin int col ← (c-'A') modulo 4 int row ← (c-'A') div 4 return (row,col) end </code></pre> <h3>Assuming positions (6,2) and (6,3) can be used</h3> <p>We can simply define a subtraction between cell coordinates as:</p> <pre><code>function subtract( pos1, pos2 : position ) begin return (pos2.row-pos1.row, pos2.col-pos1.col) end </code></pre> <p>If the subtraction yields a pair (x,y) then the path is simply x times the chararacter 'R' if x is positive or 'L' if x is negative possibliy nothing if x is zero, and similarly y times the character 'D' if y is positive or 'U' if y is negative possibly nothing if y is zero, then we end with the character 'E'.</p> <pre><code>function print_row_path( pos1, pos2 : position ) begin path ← subtract(pos1,pos2) if path.row &gt; 0 then print_times_char(path.row,'R') else if path.row &lt; 0 print_times_char(-path.row,'L') end if end function print_col_path( pos1, pos2 : position ) begin path ← subtract(pos1,pos2) if path.col &gt; 0 then print_times_char(path.col,'D') else if path.col &lt; 0 print_times_char(-path.col,'U') end if end function print_path_direction( pos1, pos2 : position ; first_direction : direction ) begin if (first_direction = FIRST_MOVE_ROW) then print_row_path(pos1,pos2) print_col_path(pos1,pos2) else print_col_path(pos1,pos2) print_row_path(pos1,pos2) end if print 'E' end function print_path(start, end : char) begin position pos1 ← get_pos(start) position pos2 ← get_pos(end) print_path_direction(pos1,pos2, FIRST_MOVE_ROW) end </code></pre> <p>Where print_times_char(t,c) is a function which prints t times the character c. I defined two "flavours" of path printing, one printing the row movements first and another printing column movements first.</p> <h3>Assuming positions (6,2) and (6,3) are forbiden</h3> <p>If we're not allowed to use positions (6,2) and (6,3) then:</p> <ul> <li>if 'A' ≤ start,end ≤ 'X' or 'Y' ≤ start,end ≤ 'Z' : (6,2) or (6.3) will never be used in the path</li> <li>if 'A' ≤ start ≤ 'X' and 'Y' ≤ end ≤ 'Z' : to ensure to not use forbidden cells print columns movements first</li> <li>if 'A' ≤ end ≤ 'X' and 'Y' ≤ start ≤ 'Z' : this time we have to print row movements first</li> </ul> <p>In pseudo code:</p> <pre><code>function print_path_wo_forbidden(start, end : char) begin position pos1 ← get_pos(start) position pos2 ← get_pos(end) if if 'A' ≤ start ≤ 'X' and 'Y' ≤ end ≤ 'Z' then print_path_direction(pos1,pos2, FIRST_MOVE_COLUMN) else print_path_direction(pos1,pos2, FIRST_MOVE_COLUMN) end if end </code></pre> <h3>Complexity</h3> <p>Printing the path between two positions is clearly in O(1), so for a string of length n we can build an O(n) algorithm.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload