Хорошо, поэтому я пытаюсь написать алгоритм обратного отслеживания, который может принимать входные данные, например:
0 2 3 1 (top-right location, length, horizontal or vertical)
1 0 4 0
2 2 4 0
1 3 3 1
top (the actual words)
that
toga
cat
И выплюнуть кроссворд, типа:
**c***
that**
**toga
***p**
Код, который у меня есть до сих пор:
//prints the puzzle
let printPuzzle (puzzle : char[,]) =
printfn "%s" ""
printfn "%A" puzzle
printfn "%s" ""
//checks if the words fits
let rec doesItFit place (puzzle : char[,]) (word : seq<char>) =
let (row, col, length, isVertical) = place
if length <> (Seq.length word) then
(puzzle, false)
else
match (Seq.toList word) with
| [] -> (puzzle, true)
| letter::rest ->
printfn "%c" letter
printPuzzle puzzle
if isVertical = 0 then
if puzzle.[row, col] = '*' || puzzle.[row, col] = letter then
puzzle.[row, col] <- letter
doesItFit (row, col+1, length-1, isVertical) puzzle rest
else
(puzzle, false)
else
if puzzle.[row, col] = '*' || puzzle.[row, col] = letter then
puzzle.[row, col] <- letter
doesItFit (row+1, col, length-1, isVertical) puzzle rest
else
(puzzle, false)
//the actual backtracking algorithm... goes through all places and all words
//trying to make stuff fit
let rec sort words places (puzzle : char[,]) =
match places with
| [] -> (puzzle, true)
| place::rest ->
let rec checkWords words place puzzle =
match words with
| [] ->
printfn "%s" "failure, backtracking"
puzzle, false
| word::otherWords ->
let attempt = doesItFit place puzzle word
if snd attempt then
printfn "%s" "success, entering if block"
let nextLevel = sort words rest (fst attempt)
if (snd nextLevel) then
nextLevel
else
checkWords otherWords place puzzle
else
checkWords otherWords place puzzle
checkWords words place puzzle
//line for testing
printPuzzle (fst (sort ["cat"; "that"; "toga"; "top"] [(0, 2, 3, 1); (1, 0, 4, 0); (2, 2, 4, 0); (1, 3, 3, 1)] (Array2D.create 6 6 '*')));;
Вот результат запуска тестовой строки:
c
[['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']]
a
[['*'; '*'; 'c'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']]
t
[['*'; '*'; 'c'; '*'; '*'; '*']
['*'; '*'; 'a'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']]
success, entering if block
t
[['*'; '*'; 'c'; '*'; '*'; '*']
['*'; '*'; 'a'; '*'; '*'; '*']
['*'; '*'; 't'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']]
h
[['*'; '*'; 'c'; '*'; '*'; '*']
['t'; '*'; 'a'; '*'; '*'; '*']
['*'; '*'; 't'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']]
a
[['*'; '*'; 'c'; '*'; '*'; '*']
['t'; 'h'; 'a'; '*'; '*'; '*']
['*'; '*'; 't'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']]
t
[['*'; '*'; 'c'; '*'; '*'; '*']
['t'; 'h'; 'a'; '*'; '*'; '*']
['*'; '*'; 't'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']]
success, entering if block
t
[['*'; '*'; 'c'; '*'; '*'; '*']
['t'; 'h'; 'a'; 't'; '*'; '*']
['*'; '*'; 't'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']]
h
[['*'; '*'; 'c'; '*'; '*'; '*']
['t'; 'h'; 'a'; 't'; '*'; '*']
['*'; '*'; 't'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']]
a
[['*'; '*'; 'c'; '*'; '*'; '*']
['t'; 'h'; 'a'; 't'; '*'; '*']
['*'; '*'; 't'; 'h'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']]
t
[['*'; '*'; 'c'; '*'; '*'; '*']
['t'; 'h'; 'a'; 't'; '*'; '*']
['*'; '*'; 't'; 'h'; 'a'; '*']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']]
success, entering if block
t
[['*'; '*'; 'c'; '*'; '*'; '*']
['t'; 'h'; 'a'; 't'; '*'; '*']
['*'; '*'; 't'; 'h'; 'a'; 't']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']]
o
[['*'; '*'; 'c'; '*'; '*'; '*']
['t'; 'h'; 'a'; 't'; '*'; '*']
['*'; '*'; 't'; 'h'; 'a'; 't']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']]
failure, backtracking
t
[['*'; '*'; 'c'; '*'; '*'; '*']
['t'; 'h'; 'a'; 't'; '*'; '*']
['*'; '*'; 't'; 'h'; 'a'; 't']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']]
o
[['*'; '*'; 'c'; '*'; '*'; '*']
['t'; 'h'; 'a'; 't'; '*'; '*']
['*'; '*'; 't'; 'h'; 'a'; 't']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']]
failure, backtracking
t
[['*'; '*'; 'c'; '*'; '*'; '*']
['t'; 'h'; 'a'; 't'; '*'; '*']
['*'; '*'; 't'; 'h'; 'a'; 't']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']]
o
[['*'; '*'; 'c'; '*'; '*'; '*']
['t'; 'h'; 'a'; 't'; '*'; '*']
['*'; '*'; 't'; 'h'; 'a'; 't']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']]
failure, backtracking
t
[['*'; '*'; 'c'; '*'; '*'; '*']
['t'; 'h'; 'a'; 't'; '*'; '*']
['*'; '*'; 't'; 'h'; 'a'; 't']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']]
failure, backtracking
[['*'; '*'; 'c'; '*'; '*'; '*']
['t'; 'h'; 'a'; 't'; '*'; '*']
['*'; '*'; 't'; 'h'; 'a'; 't']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']
['*'; '*'; '*'; '*'; '*'; '*']]
Я думаю, моя проблема в том, что я не уверен, как работает неизменяемость в F#. Судя по тому, что делает моя программа, когда головоломка передается после неудачной попытки, головоломка была изменена. Для меня это не имеет особого смысла, так как я думал, что F# не позволит его модифицировать. Я хотел бы объяснить, почему этот код использует модифицированную головоломку при проверке слова после возврата, а не исходную, немодифицированную головоломку.