Word search: Difference between revisions

m (→‎{{header|C++}}: increment cnt variable)
Line 587:
liz (9,8)(7,8) dam (2,0)(0,2)
via (8,9)(8,7)</pre>
 
=={{header|Phix}}==
Once it gets stuck it gets really stuck; there are many ways to try cramming 24,000+ words into a hole where they simply cannot fit. <br>
So after (just) 0.1s, it gives up and starts with a clean board, any longer is counterproductive.<br>
Avoids planting eg "rend" in the middle of "trendy", but does not spot the case where the latter completely overwrites the former.
<lang Phix>--
-- demo\rosetta\wordsearch.exw
-- ===========================
--
string message = "ROSETTACODE"
sequence words, solution="", placed
atom t1 = time()+0.1
 
constant grid = split("""
X 0 1 2 3 4 5 6 7 8 9 X
0 X
1 X
2 X
3 X
4 X
5 X
6 X
7 X
8 X
9 X
X X X X X X X X X X X X""",'\n')
 
constant DX = {-1, 0,+1,+1,+1, 0,-1,-1},
DY = {-3,-3,-3, 0,+3,+3,+3, 0}
 
procedure wordsearch(sequence grid, integer rqd, integer left, sequence done)
sequence rw = shuffle(tagset(length(words))),
rd = shuffle(tagset(8)),
rs = shuffle(tagset(100))
for i=1 to length(rs) do
integer sx = floor((rs[i]-1)/10)+2,
sy = remainder(rs[i]-1,10)*3+4
if grid[sx][sy]=' ' then
for w=1 to length(rw) do
string word = words[rw[w]]
if not find(word,done[1]) then
for d=1 to length(rd) do
integer {dx,dy} = {DX[rd[d]],DY[rd[d]]},
{nx,ny} = {sx,sy},
chcount = length(word)
sequence newgrid = grid
for c=1 to length(word) do
integer ch = grid[nx][ny]
if ch!=' ' and ch!=word[c] then
chcount = 0
exit
end if
newgrid[nx][ny] = word[c]
chcount -= ch!=' '
nx += dx
ny += dy
end for
if chcount!=0 then
sequence posinfo = {sx-2,(sy-4)/3,nx-dx-2,(ny-dy-4)/3},
newdone = {append(done[1],word),append(done[2],posinfo)}
if rqd<=1 and left-chcount=length(message) then
{solution, placed} = {newgrid, newdone}
return
elsif left-chcount>length(message) then
wordsearch(newgrid,rqd-1,left-chcount,newdone)
if length(solution) or time()>t1 then return end if
end if
end if
end for
end if
end for
end if
end for
end procedure
 
function valid_word(string word)
if length(word)<3 then return false end if
for i=1 to length(word) do
integer ch = word[i]
if ch<'a'
or ch>'z' then
return false
end if
end for
return true
end function
 
integer fn = open("..\\unixdict.txt","r")
words = get_text(fn,GT_LF_STRIPPED)
close(fn)
for i=length(words) to 1 by -1 do
if not valid_word(words[i]) then
words[i] = words[$]
words = words[1..$-1]
end if
end for
printf(1,"%d words loaded\n",length(words))
 
while 1 do
t1 = time()+0.1
wordsearch(grid,25,100,{{},{}})
if length(solution) then exit end if
?"stuck, retry"
end while
for x=2 to 11 do
for y=4 to 31 by 3 do
if solution[x][y]=' ' then
solution[x][y] = message[1]
message = message[2..$]
end if
end for
end for
if length(message) then ?9/0 end if
puts(1,substitute(join(solution,'\n'),"X"," "))
printf(1,"\n%d words\n",length(placed[1]))
for i=1 to length(placed[1]) do
printf(1,"%10s %10s ",{placed[1][i],sprint(placed[2][i])})
if mod(i,2)=0 then puts(1,"\n") end if
end for</lang>
{{out}}
<pre>
24820 words loaded
"stuck, retry"
"stuck, retry"
"stuck, retry"
0 1 2 3 4 5 6 7 8 9
0 l o v e s d R p u n
1 O S e E n m a c s t
2 T t e b e t T e s p
3 m m v u e A C a r h
4 a a g m z c f m O d
5 r r D i e d u b l n
6 i c E r a l i w a p
7 m f e e l r a s n v
8 b s t e d m c s g c
9 a s b i n v a d e n
 
25 words
dread {4,9,0,5} steadfast {9,1,1,9}
marimba {3,0,9,0} invade {9,3,9,8}
sneeze {0,4,5,4} love {0,0,0,3}
marc {3,1,6,1} lange {5,8,9,8}
pvc {6,9,8,9} craig {8,6,4,2}
bum {2,3,4,3} vee {3,2,1,2}
bellum {9,2,4,7} ceres {4,5,8,1}
scam {1,8,1,5} maw {8,5,6,7}
tee {2,1,0,3} san {7,7,5,9}
pun {0,7,0,9} feel {7,1,7,4}
ned {9,9,9,7} salem {8,7,4,3}
bird {5,7,8,4} phd {2,9,4,9}
tap {2,5,0,7}
</pre>
 
=={{header|zkl}}==
7,794

edits