Queens


List all the ways to place iGridSize queens on the square grid that no queen attacks each other.
There is no trick, just brutal force to go thru all possibilities and remove the ones that doesn’t qualify.

leet51NQueensUtil(GRID_SIZE, aColOnRows, iRowCurrent, set< vector<int> >& setResult) 

setResult: output, have ways of placing
aColOnRows: how we picked for rows < iRowCurrent.
example when iRowCurrent == 3: element is like {1 3} means pos {0,1} {1,3}
this function will grow it to {1, 3, 0} and resurse with iRowCurrent == 4

This solution didn’t refer to the answers on internet, and it seems with fewer lines of code.
Download: http://riowing.net/p/wp/queen.cpp

Combination 2


One more way to list combinations, in addition to 240114. Thee ways:

  1. no recursion: each loop adding one more letter to output set
  2. with recursion:
    2a: each recursion add one more input letter: meCombinationEnumerationUtilRecursionInput
    2b: each recursion grow the output length by one: meCombinationEnumerationUtilRecursionOutput
meCombinationEnumerationUtilRecursionOutputsPrefix, & sInput, iPrefixLen, set& output, iOutputLen, ) is for case 2b.

sInput is string, but it stands for a set of chars. no repeats in sInput-s
adds sInput to output, and grows the sInput length by one, from 0 to full length.
triggers all calls with this new sInput length
e.g. for 1234 here is the sInput-s as input:
first level: ""
second level: "1" "2" "3" "4"
third level:
when called from "1": "12" "13" "14"
when called from "2": "23" "24"
when called from "3": "34"
when called from "4": none
as seen at third level, when length == 2,
sInput never repeat the earlier ones by only appending chars after, controlled by iStart.
Output order is different comparing to 2a:
2a: output by input length. output is also input; later output depending in previous output to be complete.
function is caled the same number as the number of inputs, which is much smaller.
2b: output by output length. output never used as input
function is caled the same number as the combinations, which is much bigger.

for pumutation, the frame is very similar, the difference is just the candicates. e.g 1234
With prefix 1 candidates 234, we make new sPrefix 13 by swapping with 2 and 3, resulting in candicates are 24
then new sPrefix 14 by swapping with 2 and 4, resulting in candicates are 32

Download: http://riowing.net/p/wp/Combination.cpp

Combination


Get the different combinations from letters like 1 2 3.

  1. all length combinations like 1 12 123
  2. combinations with certain length, e.g. 2, we would have: 12 23 13. it can further filter with iSum.

Two ways to do it:


a. without recursion: meCombinationEnumerationUtilNoRecursion(“123”, output}
loop all char in “123”
when we have i==3, all combinations of “12” is already in output, including {} {1} {1,2}…
in addition to all in current output, we grow output by adding tempItemAdded.
tempItemAdded is made by adding 3 to each one in current output, we got e.g. {3} {1,3}…
one thing to note is output initially have {}, so that tempItemAdded can have {1} on first loop, and we remove it at end.

b with recurstion: meCombinationEnumerationUtilRecursionInput(“123”, output)
we first call it with “23”, so that output, inputRest, will have all combinations, e.g. {} {2} {2,3}…
similar to case a above, we add 1 to each one in inputRest, and these become part of final output too.
at exit: output has all combinations of 123

Download: http://riowing.net/p/wp/Combination.cpp

char8_t


char8_t: new in c++20, holds utf8 encoded byte.
char8_t*: e.g PChar8 = u8″ChineseCharsInUnicode” with u8 converted to utf8
Here Chinese charactor ZHONG, meaning Center, is encode/decoded and printed.

Decode:
Support to decode utf8 like std::wstring_convert was deprecated;
here use platform specific, like windows MultiByteToWideChar(CP_UTF8…, which takes no char8_t
use reinterpret_cast

Printing:
_setmode tells what is going to console, e.g _O_U16TEXT for unicode

Sample data Zhong:
Unicode = 4E2D = binary 0100 111000 101101 (4+6+6 bits)
utf8 = E4B8AD = decimal 228 184 173 = binary 11100100 10111000 10101101
decode by removing the leading 1110 10 10 to get unicode 4+6+6 bits above

Download: http://riowing.net/p/wp/other.cpp